Files
glenloch-media/e2e/publish.spec.ts
2026-11-20 04:07:02 +00:00

122 lines
4.6 KiB
TypeScript

import { test, expect, type Page } from "@playwright/test";
import { signInAs, resetDb, BASE } from "./dev";
/**
* Publish / unpublish — unit D10 (ARCHITECTURE §5.5).
*
* The seed leaves the second Sunday's instances in draft, so this suite has a
* week it can actually publish. The properties pinned here:
* - publishing a week with empty covering seats is blocked (409) with the
* empty seats listed, unless the admin passes `allowGaps`,
* - re-publishing the same week is an idempotent no-op,
* - unpublishing is refused while a volunteer has confirmed,
* - a volunteer cannot publish or unpublish at all (403).
*
* D10 sends nothing: no notifications exist yet, by design.
*/
/** The seed's draft instance (second Sunday). */
async function draftInstanceId(page: Page): Promise<string> {
const res = await page.request.get(`${BASE}/api/dev/schedule/instances`);
expect(res.ok(), "instances route should succeed").toBe(true);
const body = (await res.json()) as {
instances: { id: string; status: string }[];
};
const draft = body.instances.find((i) => i.status === "draft");
expect(draft, "the seed should include a draft instance").toBeTruthy();
return draft!.id;
}
let instanceId: string;
test.beforeAll(async ({ browser }) => {
const context = await browser.newContext();
const page = await context.newPage();
await resetDb(page);
// Resolve the draft instance while the admin session (which holds
// `schedule.publish`) is live; the volunteer test cannot read this route.
instanceId = await draftInstanceId(page);
await context.close();
});
test.describe("publish", () => {
test("open seats block publish, allowGaps publishes, re-publish is a no-op", async ({
page,
}) => {
const errors: string[] = [];
page.on("console", (msg) => {
if (msg.type() === "error") errors.push(msg.text());
});
page.on("pageerror", (err) => errors.push(`page error: ${err.message}`));
await page.setViewportSize({ width: 1024, height: 768 });
await signInAs(page, "coordinator@glenloch.local");
const url = `${BASE}/api/dev/schedule/${instanceId}/publish`;
// Blocked: the seed fills only Audio, so Livestream and Slides are empty.
const blocked = await page.request.post(url, { data: JSON.stringify({}) });
expect(blocked.status(), "publish with empty seats is refused").toBe(409);
const blockedBody = (await blocked.json()) as {
code: string;
gaps: { slotId: string }[];
};
expect(blockedBody.code).toBe("open_slots");
expect(blockedBody.gaps.length, "both empty seats are listed").toBe(2);
// The explicit override publishes the week with those gaps.
const published = await (
await page.request.post(url, {
data: JSON.stringify({ allowGaps: true }),
})
).json() as {
published: boolean;
alreadyPublished: boolean;
gaps: unknown[];
publishedAt: string;
};
expect(published.published).toBe(true);
expect(published.alreadyPublished).toBe(false);
expect(published.gaps).toHaveLength(2);
// Re-publish is idempotent: nothing changes, including the publish instant.
const again = await (
await page.request.post(url, {
data: JSON.stringify({ allowGaps: true }),
})
).json() as {
published: boolean;
alreadyPublished: boolean;
publishedAt: string;
};
expect(again.published).toBe(false);
expect(again.alreadyPublished).toBe(true);
expect(again.publishedAt).toBe(published.publishedAt);
// A volunteer confirmed the Audio seat, so the week cannot revert to draft.
const unpublish = await page.request.delete(url);
expect(unpublish.status(), "confirmed volunteer blocks unpublish").toBe(409);
const unpublishBody = (await unpublish.json()) as { code: string };
expect(unpublishBody.code).toBe("unpublish_blocked");
expect(errors, `unexpected console/page errors: ${errors.join("; ")}`).toEqual(
[],
);
});
test("volunteer is rejected by publish and unpublish with 403", async ({
page,
}) => {
await page.setViewportSize({ width: 1024, height: 768 });
await signInAs(page, "avery@glenloch.local");
await page.goto(`${BASE}/schedule`);
await expect(page).toHaveURL(`${BASE}/schedule`);
const url = `${BASE}/api/dev/schedule/${instanceId}/publish`;
const post = await page.request.post(url, { data: JSON.stringify({}) });
expect(post.status(), "volunteer publish must be rejected").toBe(403);
const del = await page.request.delete(url);
expect(del.status(), "volunteer unpublish must be rejected").toBe(403);
});
});