mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
test: close the coverage gaps the first round left
Second pass over the suite, driven by the inventory in the gaps document. Nine agents wrote suites in parallel against private databases, then a tenth read all of it adversarially and five of its findings were fixed. unit + component 2076 -> 2079 (+888 over the round) api 647 -> 1015 e2e 18 -> 29 What was closed: - lib/route-access.ts, the page-level authorization layer, went from zero tests to 48. Every API route was guarded and none of the pages were. - The five media proxy routes now have a real 2xx beside every 403. The blocker was the positive control, solved by stubbing r2Client.send() and leaving lib/r2-media-proxy.ts itself real. - Every remaining server-side lib module: invitations, email verification, the upload tokens, the logger, request origin, the whole R2 and Bunny lifecycle, notifications and admin stats. - Six video-page hooks, and the chunking arithmetic extracted out of lib/client/r2-video-upload.ts as a pure module. - Five end-to-end flows: workspace members, bulk operations, the admin area, player interaction and failure recovery. Three things about the harness itself turned out to be wrong: - Two @/lib/r2 stubs in tests/setup/api.ts had the wrong return shape, so every route reaching finalizeR2VideoUpload silently took the "not a valid video" branch and no test noticed. - The auth matrix asserted only "not 2xx", which two entries satisfied without their guard existing. It now requires 401 or 403, which makes both load-bearing, and all 60 routes pass the stricter form. - Both admin API routes had no positive control anywhere: replacing their guard with an unconditional refusal left the entire suite green. Found by the adversarial review, now covered. Process: - bun run test:mutation runs StrykerJS over the authorization and validation modules. Diagnostic, not a gate, weekly in CI rather than on a push. - playwright.config.ts gains an opt-in webkit project for the player spec. - AGENTS.md now requires a batch of new tests to be reviewed by somebody who did not write them. Only two production files change, both deliberate: lib/auth.ts loses a verbatim copy of its own permission formulas, and lib/client/r2-video-upload.ts calls the extracted arithmetic. No behaviour change in either.
This commit is contained in:
+37
-10
@@ -74,16 +74,43 @@ vi.mock('@/lib/r2', async (importOriginal) => {
|
||||
uploadAudio: vi.fn(async (key: string) => `https://r2.test/object/${key}`),
|
||||
deleteVideoObject: vi.fn(async () => undefined),
|
||||
deleteR2Object: vi.fn(async () => undefined),
|
||||
headVideoObject: vi.fn(async () => ({
|
||||
contentLength: 1024,
|
||||
contentType: 'video/mp4',
|
||||
etag: 'test-etag',
|
||||
})),
|
||||
readVideoObjectBytes: vi.fn(async () => ({
|
||||
body: new Uint8Array(0),
|
||||
contentLength: 0,
|
||||
contentType: 'video/mp4',
|
||||
})),
|
||||
// These two must match the real return shapes exactly, and for a while they
|
||||
// did not. `headVideoObject` really answers `contentLength: bigint`, and
|
||||
// `readVideoObjectBytes` really answers `Uint8Array | null`, not an object
|
||||
// wrapping one.
|
||||
//
|
||||
// The wrong shapes were not inert. `finalizeR2VideoUpload` passes the head
|
||||
// result straight through as `sizeBytes`, so a `number` reached a BigInt
|
||||
// column and only survived on Prisma's coercion. Worse, the old
|
||||
// `readVideoObjectBytes` stub returned a truthy object with no `.length`,
|
||||
// so `hasKnownVideoMagicBytes()` saw zero bytes and every route reaching
|
||||
// finalize took the "Uploaded file is not a valid video" branch, cancelled
|
||||
// the session and deleted both objects. Nothing caught it because no test
|
||||
// drove that path to success until tests/api/lib-r2-video-finalize.test.ts.
|
||||
// Both keep the guards the real functions apply before they ever speak to
|
||||
// S3: neither will touch a key outside the `videos/` prefix, and
|
||||
// readVideoObjectBytes also refuses a non-positive length (lib/r2.ts:407 and
|
||||
// :440). A stub that answers for any key disarms those guards for every api
|
||||
// suite at once, so a route that heads or reads the wrong object key would
|
||||
// look perfectly healthy. The prefix is written out here rather than imported
|
||||
// from lib/video-upload-validation, so that changing it fails loudly instead
|
||||
// of the stub agreeing with the change.
|
||||
headVideoObject: vi.fn(async (key: string) => {
|
||||
if (!key.startsWith('videos/')) return null;
|
||||
return { contentLength: BigInt(1024), contentType: 'video/mp4' };
|
||||
}),
|
||||
// 64 bytes with an `ftyp` box at offset 4, the ISO base media signature the
|
||||
// first branch of hasKnownVideoMagicBytes() looks for, trimmed to the length
|
||||
// the caller asked for the way a real ranged GET would be. A suite that needs
|
||||
// a rejected upload overrides this per test.
|
||||
readVideoObjectBytes: vi.fn(async (key: string, byteLength: number) => {
|
||||
if (!key.startsWith('videos/') || byteLength <= 0) return null;
|
||||
const header = new Uint8Array(64);
|
||||
header.set([0x00, 0x00, 0x00, 0x20], 0);
|
||||
header.set([0x66, 0x74, 0x79, 0x70], 4); // 'ftyp'
|
||||
header.set([0x69, 0x73, 0x6f, 0x6d], 8); // 'isom'
|
||||
return header.slice(0, Math.min(header.length, byteLength));
|
||||
}),
|
||||
ensureR2BucketExists: vi.fn(async () => undefined),
|
||||
ensureR2UploadCors: vi.fn(async () => []),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user