Files
spoon/packages/backend/tests/unit/harness.test.ts
T
Gabriel Brown 750c3e7923
Build and Push Next App / quality (push) Failing after 57s
Build and Push Next App / build-next (push) Has been skipped
Update stuff so we can pass build hopefully
2026-06-21 21:28:57 -05:00

101 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { convexTest } from 'convex-test';
import { makeFunctionReference } from 'convex/server';
import { describe, expect, test } from 'vitest';
import schema from '../../convex/schema';
const createManualSpoon = makeFunctionReference<'mutation'>(
'spoons:createManual',
);
const listMySpoons = makeFunctionReference<'query'>('spoons:listMine');
const getSpoon = makeFunctionReference<'query'>('spoons:get');
const createAgentRequest = makeFunctionReference<'mutation'>(
'agentRequests:create',
);
const modules = import.meta.glob('../../convex/**/*.*s');
const createUser = async (t: ReturnType<typeof convexTest>, email: string) =>
await t.mutation(async (ctx) => {
return await ctx.db.insert('users', { email, name: email });
});
const authed = (t: ReturnType<typeof convexTest>, userId: string) =>
t.withIdentity({
subject: `${userId}|session`,
issuer: 'https://convex.test',
});
const spoonInput = {
name: 'Editor Spoon',
provider: 'gitea' as const,
upstreamOwner: 'upstream',
upstreamRepo: 'editor',
upstreamDefaultBranch: 'main',
upstreamUrl: 'https://git.example.com/upstream/editor',
forkOwner: 'team',
forkRepo: 'editor-spoon',
forkUrl: 'https://git.example.com/team/editor-spoon',
visibility: 'private' as const,
maintenanceMode: 'watch' as const,
syncCadence: 'daily' as const,
productionRefStrategy: 'default_branch' as const,
};
describe('convex-test harness', () => {
test('boots and executes against the project schema', async () => {
const t = convexTest(schema, modules);
expect(await t.run(() => Promise.resolve(42))).toBe(42);
});
test('requires authentication to create a Spoon', async () => {
const t = convexTest(schema, modules);
await expect(t.mutation(createManualSpoon, spoonInput)).rejects.toThrow(
'Not authenticated.',
);
});
test('creates and lists Spoons for the current user', async () => {
const t = convexTest(schema, modules);
const userId = await createUser(t, 'one@example.com');
const session = authed(t, userId);
const spoonId = await session.mutation(createManualSpoon, spoonInput);
const spoons = await session.query(listMySpoons, {});
expect(spoons).toHaveLength(1);
expect(spoons[0]?._id).toBe(spoonId);
expect(spoons[0]?.ownerId).toBe(userId);
});
test('does not allow reading another users Spoon', async () => {
const t = convexTest(schema, modules);
const ownerId = await createUser(t, 'owner@example.com');
const otherId = await createUser(t, 'other@example.com');
const spoonId = await authed(t, ownerId).mutation(
createManualSpoon,
spoonInput,
);
await expect(
authed(t, otherId).query(getSpoon, { spoonId }),
).rejects.toThrow('Spoon not found.');
});
test('requires Spoon ownership for agent requests', async () => {
const t = convexTest(schema, modules);
const ownerId = await createUser(t, 'owner@example.com');
const otherId = await createUser(t, 'other@example.com');
const spoonId = await authed(t, ownerId).mutation(
createManualSpoon,
spoonInput,
);
await expect(
authed(t, otherId).mutation(createAgentRequest, {
spoonId,
prompt: 'Add a settings page',
}),
).rejects.toThrow('Spoon not found.');
});
});