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:
yusufipk
2026-07-26 13:25:11 +07:00
parent fe42c0836f
commit 0187db5dc7
55 changed files with 17028 additions and 166 deletions
+74 -46
View File
@@ -215,7 +215,69 @@ export type EnrichedProjectForAccess = {
};
/**
* Pure access computation — no DB queries.
* The project permission formulas, in one place.
*
* Two functions resolve the same six inputs by different routes:
* `computeProjectAccess` reads them off a project that was fetched with
* `projectAccessInclude()`, and `checkProjectAccess` queries for each relation.
* They then have to agree on what those inputs mean. Both used to carry a
* verbatim copy of the three formulas below, which is a silent-divergence
* hazard rather than a style complaint: change an authorization rule in one
* copy and not the other and a page renders for somebody the API would refuse.
*/
function resolveProjectPermissions(input: {
isOwner: boolean;
isPublic: boolean;
isProjectMember: boolean;
isProjectAdmin: boolean;
workspaceRole: WorkspaceMemberRole | 'OWNER' | null;
ownerBillingActive: boolean;
}) {
const { isOwner, isPublic, isProjectMember, isProjectAdmin, workspaceRole, ownerBillingActive } =
input;
const isWorkspaceMember = !!workspaceRole;
const isWorkspaceAdmin = workspaceRole === WorkspaceMemberRole.ADMIN || workspaceRole === 'OWNER';
return {
isOwner,
isProjectMember,
isProjectAdmin,
isWorkspaceMember,
isWorkspaceAdmin,
hasAccess: ownerBillingActive && (isOwner || isProjectMember || isPublic || isWorkspaceMember),
canEdit: ownerBillingActive && (isOwner || isProjectAdmin || isWorkspaceAdmin),
canDelete: ownerBillingActive && (isOwner || workspaceRole === 'OWNER'),
ownerBillingActive,
};
}
/**
* The workspace permission formulas. Only one caller today, but it is kept
* beside its project twin and exported so it can be tested directly rather
* than only through whichever route happens to exercise it.
*/
export function resolveWorkspacePermissions(input: {
isOwner: boolean;
isMember: boolean;
isAdmin: boolean;
ownerBillingActive: boolean;
}) {
const { isOwner, isMember, isAdmin, ownerBillingActive } = input;
return {
isOwner,
isMember,
isAdmin,
hasAccess: ownerBillingActive && (isOwner || isMember),
canEdit: ownerBillingActive && (isOwner || isAdmin),
canDelete: ownerBillingActive && isOwner,
ownerBillingActive,
};
}
/**
* Pure access computation, no DB queries.
* Use after fetching a project with `projectAccessInclude(userId)`.
*/
export function computeProjectAccess(
@@ -241,25 +303,14 @@ export function computeProjectAccess(
if (wsMember) workspaceRole = wsMember.role;
}
const isWorkspaceMember = !!workspaceRole;
const isWorkspaceAdmin = workspaceRole === WorkspaceMemberRole.ADMIN || workspaceRole === 'OWNER';
const hasAccess =
workspaceOwnerBillingAccess && (isOwner || isProjectMember || isPublic || isWorkspaceMember);
const canEdit = workspaceOwnerBillingAccess && (isOwner || isProjectAdmin || isWorkspaceAdmin);
const canDelete = workspaceOwnerBillingAccess && (isOwner || workspaceRole === 'OWNER');
return {
return resolveProjectPermissions({
isOwner,
isPublic,
isProjectMember,
isProjectAdmin,
isWorkspaceMember,
isWorkspaceAdmin,
hasAccess,
canEdit,
canDelete,
workspaceRole,
ownerBillingActive: workspaceOwnerBillingAccess,
};
});
}
// Helper to check project access including workspace membership
@@ -284,8 +335,8 @@ export async function checkProjectAccess(
// The workspace role decides `canEdit`/`isWorkspaceMember`, not just whether the viewer
// gets in at all, so it has to be resolved for every signed-in non-owner. Skipping it
// once access was already granted some other way (public project, or an existing project
// membership) silently downgraded workspace admins to read-only on `intent: 'view'`
// the intent pages and GET routes use to decide which actions to render.
// membership) silently downgraded workspace admins to read-only on `intent: 'view'`,
// the intent that pages and GET routes use to decide which actions to render.
// Owners pass every check on their own; resolve their role only when they mutate.
const shouldLoadWorkspaceRole = !!userId && (!isOwner || intent !== 'view');
@@ -338,25 +389,14 @@ export async function checkProjectAccess(
});
workspaceOwnerBillingAccess = wsOwner?.owner ? hasBillingAccess(wsOwner.owner) : false;
}
const isWorkspaceMember = !!workspaceRole;
const isWorkspaceAdmin = workspaceRole === WorkspaceMemberRole.ADMIN || workspaceRole === 'OWNER';
const hasAccess =
workspaceOwnerBillingAccess && (isOwner || isProjectMember || isPublic || isWorkspaceMember);
const canEdit = workspaceOwnerBillingAccess && (isOwner || isProjectAdmin || isWorkspaceAdmin);
const canDelete = workspaceOwnerBillingAccess && (isOwner || workspaceRole === 'OWNER');
return {
return resolveProjectPermissions({
isOwner,
isPublic,
isProjectMember,
isProjectAdmin,
isWorkspaceMember,
isWorkspaceAdmin,
hasAccess,
canEdit,
canDelete,
workspaceRole,
ownerBillingActive: workspaceOwnerBillingAccess,
};
});
}
// Helper to check workspace access
@@ -386,17 +426,5 @@ export async function checkWorkspaceAccess(
});
const ownerBillingActive = owner ? hasBillingAccess(owner) : false;
const hasAccess = ownerBillingActive && (isOwner || isMember);
const canEdit = ownerBillingActive && (isOwner || isAdmin);
const canDelete = ownerBillingActive && isOwner;
return {
isOwner,
isMember,
isAdmin,
hasAccess,
canEdit,
canDelete,
ownerBillingActive,
};
return resolveWorkspacePermissions({ isOwner, isMember, isAdmin, ownerBillingActive });
}