Files
Jyotisha/frontend/tests/release-notes-331.test.tsx
T
ClaudeandClaude Opus 5.5 7ffd9fd8c6 feat(release-notes): admin writes 「更新说明」; each published version shows once per user; settings lists them (#331)
- Migration 20261122130000_release_notes.sql: release_notes (version, title,
  grouped items, draft/published/withdrawn) and release_note_reads; RLS on,
  no table privilege for app/service roles; users go through service-role
  functions list_release_notes_for_user / mark_release_note_read (a version
  published before the account existed counts as seen); the admin writes
  through audited admin_save_release_note / admin_set_release_note_status
  gated by the new content.release_notes.write (owner, operations,
  content_editor) and reads via an admin_runtime select policy.
- APIs: GET/POST /api/release-notes (signed-in user only) and GET/POST
  /api/admin/release-notes.
- Admin page 更新说明: table + editor drawer with 新功能/修复/改进 lists and a
  live preview; 保存草稿 / 发布 / 撤回.
- User side: 「本次更新」 dialog once per version (知道了 records the read);
  settings gets an 更新说明 pane (phone segmented nav becomes four).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-22 17:12:54 +08:00

128 lines
7.7 KiB
TypeScript

// Issue #331 「更新说明」 (owner 2026-11-22): each published version shows once to every user;
// account settings lists all published versions; the admin writes, publishes and withdraws them.
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import { renderToStaticMarkup } from "react-dom/server";
import { ReleaseNoteBody } from "../src/components/release-notes.tsx";
import {
groupedReleaseNoteItems,
releaseNoteDate,
releaseNoteDraftSchema,
releaseNoteFromRow,
unseenReleaseNote,
type ReleaseNote,
} from "../src/lib/release-notes.ts";
const source = (path: string) => readFileSync(new URL(`../${path}`, import.meta.url), "utf8");
function note(overrides: Partial<ReleaseNote> = {}): ReleaseNote {
return {
id: "33100000-0000-4000-8000-000000000001",
version: "2026.11.23",
title: "正式开放内测",
items: [
{ group: "fix", text: "打开报告不再空白" },
{ group: "feature", text: "报告里可以选中一段直接提问" },
{ group: "improve", text: "报告打开更快" },
{ group: "feature", text: "首页新增三个常见问题" },
],
publishedAt: "2026-11-23T02:00:00.000Z",
seen: false,
...overrides,
};
}
test("a draft needs a version, a title and at least one item with a known group", () => {
assert.equal(releaseNoteDraftSchema.safeParse({ version: "2026.11.23", title: "上线", items: [{ group: "feature", text: "新功能" }] }).success, true);
assert.equal(releaseNoteDraftSchema.safeParse({ version: "2026.11.23-2", title: "上线", items: [{ group: "fix", text: "修复" }] }).success, true);
assert.equal(releaseNoteDraftSchema.safeParse({ version: "2026 11 23", title: "上线", items: [{ group: "fix", text: "修复" }] }).success, false);
assert.equal(releaseNoteDraftSchema.safeParse({ version: "2026.11.23", title: " ", items: [{ group: "fix", text: "修复" }] }).success, false);
assert.equal(releaseNoteDraftSchema.safeParse({ version: "2026.11.23", title: "上线", items: [] }).success, false);
assert.equal(releaseNoteDraftSchema.safeParse({ version: "2026.11.23", title: "上线", items: [{ group: "news", text: "x" }] }).success, false);
});
test("items are shown in the fixed order 新功能 / 修复 / 改进, empty groups left out", () => {
const groups = groupedReleaseNoteItems(note().items);
assert.deepEqual(groups.map((group) => group.label), ["新功能", "修复", "改进"]);
assert.deepEqual(groups[0]!.texts, ["报告里可以选中一段直接提问", "首页新增三个常见问题"]);
assert.deepEqual(groupedReleaseNoteItems([{ group: "fix", text: "只有修复" }]).map((group) => group.label), ["修复"]);
});
test("the pop-up is the newest version this user has not closed; none when all are seen", () => {
const newest = note({ id: "a", version: "2026.11.30", seen: false });
const older = note({ id: "b", version: "2026.11.23", seen: false });
assert.equal(unseenReleaseNote([newest, older])?.id, "a");
assert.equal(unseenReleaseNote([note({ id: "a", seen: true }), older])?.id, "b");
assert.equal(unseenReleaseNote([note({ seen: true })]), null);
assert.equal(unseenReleaseNote([]), null);
});
test("a stored row is read only when its items have the editor's shape", () => {
const row = { id: "x", version: "1", title: "t", items: [{ group: "fix", text: "a" }], published_at: new Date("2026-11-23T00:00:00Z"), seen: true };
assert.equal(releaseNoteFromRow(row)?.publishedAt, "2026-11-23T00:00:00.000Z");
assert.equal(releaseNoteFromRow({ ...row, items: [{ group: "fix" }] }), null);
assert.equal(releaseNoteFromRow({ ...row, published_at: null }), null);
});
test("the publish date is written out in China time, without dots", () => {
assert.equal(releaseNoteDate("2026-11-22T17:00:00.000Z"), "2026 年 11 月 23 日");
assert.equal(releaseNoteDate("not a date"), "");
});
test("the body renders each group as a heading and a list", () => {
const markup = renderToStaticMarkup(<ReleaseNoteBody note={note()} />);
assert.match(markup, /<h3 class="release-note-group-title">新功能<\/h3><ul><li>报告里可以选中一段直接提问<\/li><li>首页新增三个常见问题<\/li><\/ul>/);
assert.ok(markup.indexOf("新功能") < markup.indexOf("修复") && markup.indexOf("修复") < markup.indexOf("改进"));
assert.doesNotMatch(markup, /·/);
});
test("the dialog checks once per tab, shows one version, and 「知道了」 records the read", () => {
const component = source("src/components/release-notes.tsx");
assert.match(component, /let checkedThisTab = false;/);
assert.match(component, /if \(!signedIn \|\| checkedThisTab\) return;\s*checkedThisTab = true;/);
assert.match(component, /setNote\(unseenReleaseNote\(notes\)\)/);
assert.match(component, /<Button type="button" onClick=\{close\}>知道了<\/Button>/);
assert.match(component, /method: "POST",[\s\S]*?body: JSON\.stringify\(\{ id \}\)/);
assert.match(component, /<p className="release-note-meta">本次更新 \{note\.version\}<\/p>/);
const layout = source("src/app/(app)/layout.tsx");
assert.match(layout, /<ReleaseNoteDialog signedIn=\{Boolean\(account\) && !list\.signedOut\} \/>/);
});
test("account settings has an 「更新说明」 pane listing published versions", () => {
const overlay = source("src/components/account-dialog-overlay.tsx");
assert.match(overlay, /\{ dialog: "updates", label: "更新说明", icon: Megaphone \}/);
assert.match(overlay, /if \(dialog === "updates"\) return model\.renderUpdates\(\);/);
const provider = source("src/components/account-dialog-provider.tsx");
assert.match(provider, /renderUpdates: \(\) => <ReleaseNotesPanel \/>/);
});
test("the user API acts for the signed-in user only, through the service functions", () => {
const route = source("src/app/api/release-notes/route.ts");
assert.match(route, /rpc\("list_release_notes_for_user", \{ p_user_id: user\.id \}\)/);
assert.match(route, /rpc\("mark_release_note_read", \{ p_user_id: user\.id, p_release_note_id: parsed\.data\.id \}\)/);
assert.match(route, /status: 401/);
});
test("the admin API writes only with content.release_notes.write, through the audited functions", () => {
const route = source("src/app/api/admin/release-notes/route.ts");
assert.match(route, /requireAdminMutation\(request, "content\.release_notes\.write"\)/);
assert.match(route, /public\.admin_save_release_note\(/);
assert.match(route, /public\.admin_set_release_note_status\(/);
assert.doesNotMatch(route, /insert into public\.release_notes|update public\.release_notes/);
assert.match(source("src/lib/admin/auth-policy.ts"), /"content\.release_notes\.write",/);
assert.match(source("src/lib/admin/providers.ts"), /"release-notes": \{ read: "admin\.access", write: "content\.release_notes\.write" \}/);
assert.match(source("src/components/admin/admin-app.tsx"), /name: "release-notes", list: "\/admin\/release-notes", meta: \{ label: "更新说明"/);
const editor = source("src/components/admin/release-notes-management.tsx");
for (const label of ["保存草稿", "发布", "撤回", "用户看到的样子"]) assert.ok(editor.includes(label), label);
});
test("the migration keeps both tables closed to the app and service roles and grants by function", () => {
const migration = source("supabase/migrations/20261122130000_release_notes.sql");
assert.match(migration, /revoke all on table public\.release_notes from public, anon, authenticated, service_role;/);
assert.match(migration, /grant execute on function public\.list_release_notes_for_user\(uuid\) to service_role;/);
assert.match(migration, /create policy release_notes_admin_read on public\.release_notes\s+for select to admin_runtime using \(true\);/);
assert.match(migration, /n\.published_at <= v_joined/);
});