import { test, expect } from "@playwright/test"; import { createHash } from "node:crypto"; import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { DIGEST_NOTES, makeDigestContextFixture } from "./fixtures/digest-context-fixture.mjs"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // The channel context notes' review (/tools/digest-context; // lib/digestContextReview.mjs, plans/digest-context-review.md): each candidate // digest-context.suggested.md with what it asserts and the plan's evidence, // and the operator's Yes (it becomes the channel's digest-context.md, asked // twice) or No (a refusal recorded beside it, the file kept). Fixture: // e2e/fixtures/digest-context-fixture.mjs -- the fixture's own channels and // plan, put back before and after. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const CHANNELS = path.join(FIXTURE, "channels"); const file = (slug: string, name: string) => path.join(CHANNELS, slug, name); const reset = () => makeDigestContextFixture({ dest: FIXTURE, channels: CHANNELS }); test.beforeAll(reset); test.afterAll(reset); test("each note with its evidence; No refuses it and keeps the file; Yes, asked twice, makes it the channel's digest-context.md", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 900 }); // Found from Tools. await page.goto(urls.toolsHref()); await expect(page.locator("[data-tool-entry=context] a")).toHaveAttribute("href", urls.digestContextHref()); await page.goto(urls.digestContextHref()); const note = (slug: string) => page.locator(`[data-digest-note="${slug}"]`); await expect(page.locator("[data-digest-pending]")).toHaveAttribute("data-digest-pending", "2"); // In the sweep's queue order, the identity claim to check first flagged. expect(await page.locator("[data-digest-note]").evaluateAll((els) => els.map((e) => e.getAttribute("data-digest-note")))).toEqual(["testchan", "testchan-odysee"]); await expect(note("testchan")).toContainText("#1 in the sweep's queue ยท 1,234 chunks to generate"); await expect(note("testchan").locator("[data-digest-flag]")).toHaveText("check this one first"); // What it asserts (the note, as it would reach the prompt) and what it rests on (the plan's bullets). await expect(note("testchan").locator("[data-digest-text]")).toContainText("This channel belongs to Test Host, who streams alone."); await expect(note("testchan").locator("[data-digest-evidence] li")).toHaveCount(2); await expect(note("testchan").locator("[data-digest-evidence] li").first().locator("strong")).toHaveText("Claims the host streams alone."); await expect(note("testchan-odysee").locator("[data-digest-evidence]")).toContainText("40 of 50 titles match"); await expect(page.locator("[data-digest-review]")).toContainText("every digest already generated for that channel becomes stale and regenerates"); // It fits a desk without scrolling sideways. expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 1)).toBe(true); await page.screenshot({ path: test.info().outputPath("digest-context-1366.png"), fullPage: true }); // No: refused, listed as such, the note still on disk, the refusal beside it. await note("testchan-odysee").locator("[data-digest-no]").click(); await expect(note("testchan-odysee")).toHaveAttribute("data-digest-state", "refused"); await expect(note("testchan-odysee").locator("[data-digest-status]")).toContainText("refused"); await expect(note("testchan-odysee").locator("[data-digest-no]")).toHaveCount(0); expect(readFileSync(file("testchan-odysee", "digest-context.suggested.md"), "utf8")).toBe(DIGEST_NOTES["testchan-odysee"]); const refusal = JSON.parse(readFileSync(file("testchan-odysee", "digest-context.suggested.refused.json"), "utf8")); expect(refusal).toMatchObject({ by: "operator", sha256: createHash("sha256").update(DIGEST_NOTES["testchan-odysee"]).digest("hex") }); expect(existsSync(file("testchan-odysee", "digest-context.md"))).toBe(false); await expect(page.locator("[data-digest-pending]")).toHaveAttribute("data-digest-pending", "1"); // Yes asks first, with what it costs; Cancel writes nothing. // The confirming button lands where the first press was (a second click in place confirms), the sentence after it. const yesBox = await note("testchan").locator("[data-digest-yes]").boundingBox(); await note("testchan").locator("[data-digest-yes]").click(); const confirmBox = await note("testchan").locator("[data-digest-confirm-yes]").boundingBox(); expect(Math.abs((confirmBox?.x ?? -999) - (yesBox?.x ?? 0))).toBeLessThanOrEqual(2); expect(Math.abs((confirmBox?.y ?? -999) - (yesBox?.y ?? 0))).toBeLessThanOrEqual(2); await expect(note("testchan").locator("[data-digest-confirm]")).toContainText("every digest already made for this channel goes stale and regenerates"); await page.screenshot({ path: test.info().outputPath("digest-context-confirm.png") }); await note("testchan").getByRole("button", { name: "Cancel" }).click(); expect(existsSync(file("testchan", "digest-context.md"))).toBe(false); await note("testchan").locator("[data-digest-yes]").click(); await note("testchan").locator("[data-digest-confirm-yes]").click(); await expect(note("testchan")).toHaveAttribute("data-digest-state", "active"); expect(readFileSync(file("testchan", "digest-context.md"), "utf8")).toBe(DIGEST_NOTES.testchan); expect(existsSync(file("testchan", "digest-context.suggested.md"))).toBe(false); // Read again: nothing pending; the refused one and the active one stay listed. await page.reload(); await expect(page.locator("[data-digest-none]")).toHaveText("No note is waiting on you."); await expect(note("testchan")).toHaveAttribute("data-digest-state", "active"); await expect(note("testchan-odysee")).toHaveAttribute("data-digest-state", "refused"); await page.screenshot({ path: test.info().outputPath("digest-context-decided.png"), fullPage: true }); // And at a phone's width, no sideways scroll. await page.setViewportSize({ width: 390, height: 844 }); expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 1)).toBe(true); }); test("the route: an agent's request (any Authorization header) is refused, a note changed since it was shown is a 409", async ({ request }) => { reset(); const sha = createHash("sha256").update(DIGEST_NOTES.testchan).digest("hex"); const asAgent = await request.post("/api/digest-context", { data: { slug: "testchan", decision: "yes", sha256: sha }, headers: { authorization: "Bearer umtool-e2e-token" } }); expect(asAgent.status()).toBe(403); expect(existsSync(file("testchan", "digest-context.md"))).toBe(false); const stale = await request.post("/api/digest-context", { data: { slug: "testchan", decision: "yes", sha256: "0".repeat(64) } }); expect(stale.status()).toBe(409); expect(existsSync(file("testchan", "digest-context.suggested.md"))).toBe(true); const list = await (await request.get("/api/digest-context")).json(); expect(list.notes.map((n: { slug: string; state: string }) => `${n.slug}:${n.state}`)).toEqual(["testchan:pending", "testchan-odysee:pending"]); });