mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
test: add unit, API, component and end-to-end test suites
The repo had no automated tests. Every change was verified by hand. Adds four layers, 2023 tests in total, runnable with one command: - 1191 unit tests over the pure logic in lib/, including the full computeProjectAccess permission matrix and the billing gate - 167 component and hook tests in jsdom, covering the hooks that hold real logic rather than presentational wrappers - 647 API integration tests against a real Postgres, with only auth() mocked, including a data-driven sweep asserting that none of the 60 route modules answers 2xx to an unauthenticated caller - 18 Playwright specs driving a real browser against a real build Infrastructure: vitest.config.ts with three projects, a disposable Postgres and MinIO in docker-compose.test.yml, factories and helpers under tests/, scripts/test.sh as the single entry point, a pre-push hook running bun run verify, and CI split into check, test and e2e jobs. The test database is built with prisma db push plus a replay of the hand-written SQL, because prisma migrate deploy cannot build this schema from empty: the migration history has no captured baseline. This mirrors what scripts/docker-db-bootstrap.ts already does in production, and tests/setup/db-global.ts carries a drift guard so a new migration fails the run until someone reviews it. Production code is unchanged apart from one pure-function extraction out of use-video-player.ts, which was too large to test in jsdom. Several tests pin behaviour that looks wrong, each marked KNOWN BUG in place. TESTING.md section 12 records where the plan turned out to be wrong, and AGENTS.md now states which layer a change needs a test in.
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { getApprovalCandidatesForProject } from '@/lib/approval-workflow';
|
||||
|
||||
const dbMock = vi.hoisted(() => ({
|
||||
project: { findUnique: vi.fn() },
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/db', () => ({ db: dbMock, default: dbMock, disconnectDb: vi.fn() }));
|
||||
|
||||
interface Candidate {
|
||||
id: string;
|
||||
name: string | null;
|
||||
email: string | null;
|
||||
image: string | null;
|
||||
}
|
||||
|
||||
function user(id: string, name: string | null, email: string | null = `${id}@example.com`) {
|
||||
return { id, name, email, image: null };
|
||||
}
|
||||
|
||||
function mockProject(options: {
|
||||
owner?: Candidate | null;
|
||||
members?: Array<{ user: Candidate | null }>;
|
||||
workspaceOwner?: Candidate | null;
|
||||
workspaceMembers?: Array<{ user: Candidate | null }>;
|
||||
}) {
|
||||
dbMock.project.findUnique.mockResolvedValue({
|
||||
owner: options.owner ?? null,
|
||||
members: options.members ?? [],
|
||||
workspace: {
|
||||
owner: options.workspaceOwner ?? null,
|
||||
members: options.workspaceMembers ?? [],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
dbMock.project.findUnique.mockReset();
|
||||
});
|
||||
|
||||
describe('getApprovalCandidatesForProject', () => {
|
||||
it('returns null when the project does not exist', async () => {
|
||||
dbMock.project.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(getApprovalCandidatesForProject('missing')).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('collects the project owner, the workspace owner and both member lists', async () => {
|
||||
mockProject({
|
||||
owner: user('u-owner', 'Owner'),
|
||||
workspaceOwner: user('u-ws-owner', 'Workspace Owner'),
|
||||
members: [{ user: user('u-pm', 'Project Member') }],
|
||||
workspaceMembers: [{ user: user('u-wm', 'Workspace Member') }],
|
||||
});
|
||||
|
||||
const candidates = await getApprovalCandidatesForProject('p1');
|
||||
|
||||
expect(candidates?.map((c) => c.id).sort()).toEqual(['u-owner', 'u-pm', 'u-wm', 'u-ws-owner']);
|
||||
});
|
||||
|
||||
it('deduplicates a user who owns both the project and the workspace', async () => {
|
||||
const owner = user('u-owner', 'Owner');
|
||||
mockProject({ owner, workspaceOwner: owner });
|
||||
|
||||
const candidates = await getApprovalCandidatesForProject('p1');
|
||||
|
||||
expect(candidates).toHaveLength(1);
|
||||
expect(candidates?.[0].id).toBe('u-owner');
|
||||
});
|
||||
|
||||
it('deduplicates a user listed as both a project and a workspace member', async () => {
|
||||
const member = user('u-both', 'Both');
|
||||
mockProject({
|
||||
owner: user('u-owner', 'Owner'),
|
||||
members: [{ user: member }],
|
||||
workspaceMembers: [{ user: member }],
|
||||
});
|
||||
|
||||
const candidates = await getApprovalCandidatesForProject('p1');
|
||||
|
||||
expect(candidates?.filter((c) => c.id === 'u-both')).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('keeps the last record seen for a duplicated id', async () => {
|
||||
mockProject({
|
||||
owner: user('u-dup', 'Stale Name'),
|
||||
workspaceMembers: [{ user: user('u-dup', 'Fresh Name') }],
|
||||
});
|
||||
|
||||
const candidates = await getApprovalCandidatesForProject('p1');
|
||||
|
||||
expect(candidates?.[0].name).toBe('Fresh Name');
|
||||
});
|
||||
|
||||
it('sorts by display name case-insensitively', async () => {
|
||||
mockProject({
|
||||
owner: user('u1', 'zoe'),
|
||||
workspaceOwner: user('u2', 'Adam'),
|
||||
members: [{ user: user('u3', 'mike') }],
|
||||
workspaceMembers: [{ user: user('u4', 'Bella') }],
|
||||
});
|
||||
|
||||
const candidates = await getApprovalCandidatesForProject('p1');
|
||||
|
||||
expect(candidates?.map((c) => c.name)).toEqual(['Adam', 'Bella', 'mike', 'zoe']);
|
||||
});
|
||||
|
||||
it('sorts by email when a candidate has no display name', async () => {
|
||||
mockProject({
|
||||
owner: user('u1', null, '[email protected]'),
|
||||
workspaceOwner: user('u2', 'Bella', '[email protected]'),
|
||||
});
|
||||
|
||||
const candidates = await getApprovalCandidatesForProject('p1');
|
||||
|
||||
expect(candidates?.map((c) => c.id)).toEqual(['u1', 'u2']);
|
||||
});
|
||||
|
||||
it('sorts a candidate with neither name nor email first', async () => {
|
||||
mockProject({
|
||||
owner: user('u-blank', null, null),
|
||||
workspaceOwner: user('u-named', 'Adam'),
|
||||
});
|
||||
|
||||
const candidates = await getApprovalCandidatesForProject('p1');
|
||||
|
||||
expect(candidates?.map((c) => c.id)).toEqual(['u-blank', 'u-named']);
|
||||
});
|
||||
|
||||
it('skips null owner and null member user rows without throwing', async () => {
|
||||
mockProject({
|
||||
owner: null,
|
||||
workspaceOwner: null,
|
||||
members: [{ user: null }, { user: user('u-real', 'Real') }],
|
||||
workspaceMembers: [{ user: null }],
|
||||
});
|
||||
|
||||
const candidates = await getApprovalCandidatesForProject('p1');
|
||||
|
||||
expect(candidates).toEqual([
|
||||
{ id: 'u-real', name: 'Real', email: '[email protected]', image: null },
|
||||
]);
|
||||
});
|
||||
|
||||
it('returns an empty list when the project has no people attached at all', async () => {
|
||||
mockProject({});
|
||||
|
||||
await expect(getApprovalCandidatesForProject('p1')).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('queries by project id', async () => {
|
||||
mockProject({ owner: user('u1', 'Owner') });
|
||||
|
||||
await getApprovalCandidatesForProject('project-42');
|
||||
|
||||
expect(dbMock.project.findUnique).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ where: { id: 'project-42' } })
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user