Files
spoon/packages/backend/tests/unit/git-connection-status.test.ts

66 lines
2.0 KiB
TypeScript

import { convexTest } from 'convex-test';
import { describe, expect, test } from 'vitest';
import { internal } from '../../convex/_generated/api.js';
import schema from '../../convex/schema';
const modules = import.meta.glob('../../convex/**/*.*s');
const seedConnection = async (
t: ReturnType<typeof convexTest>,
installationId: string,
) =>
await t.run(async (ctx) => {
const now = Date.now();
const userId = await ctx.db.insert('users', {
email: 'a@b.c',
name: 'A',
});
// Disable email so a reauth emit does not schedule the best-effort node
// email action, whose async logging would race test teardown.
await ctx.db.insert('notificationPreferences', {
userId,
emailEnabled: false,
updatedAt: now,
});
return await ctx.db.insert('gitConnections', {
userId,
provider: 'github',
displayName: `GitHub installation ${installationId}`,
installationId,
status: 'active',
connectedAt: now,
updatedAt: now,
});
});
describe('setConnectionStatusByInstallation', () => {
test('flips the matching connection status', async () => {
const t = convexTest(schema, modules);
const connectionId = await seedConnection(t, '42');
const result = await t.mutation(
internal.github.setConnectionStatusByInstallation,
{ installationId: '42', status: 'needs_reauth' },
);
expect(result).toEqual({ updated: 1 });
const connection = await t.run(async (ctx) => await ctx.db.get(connectionId));
expect(connection?.status).toBe('needs_reauth');
});
test('is a no-op when no connection matches', async () => {
const t = convexTest(schema, modules);
const connectionId = await seedConnection(t, '42');
const result = await t.mutation(
internal.github.setConnectionStatusByInstallation,
{ installationId: '999', status: 'revoked' },
);
expect(result).toEqual({ updated: 0 });
const connection = await t.run(async (ctx) => await ctx.db.get(connectionId));
expect(connection?.status).toBe('active');
});
});