Files
cv/scripts/smoke-draft.ts
T

259 lines
15 KiB
TypeScript
Raw Normal View History

// Smoke test for the draft store (spec-v4 §5.1, §5.5, §5.6; registry per
// spec-v4-data-organization §6.1). Id unions are no longer the store's
// business — scripts/generate-content-types.ts derives them from these very
// documents (spec-v5), so what is checked here is the draft itself: text
// writes, record add/delete/rename, conflicts and rebase, the validator rules
// a rejected edit is reported through, and the scoped-baseline rules of
// spec-v4-live-edit §5.1 (adoptBase, and a save that must not PUT a
// bundle-seeded document), and the Contents API conflict answer of spec-v6 D5.
// Run: pnpm tsx scripts/smoke-draft.ts
import { DraftStore, bundledBase, PROXY_BASE, saveDraft } from '../src/i18n/draft';
import type { CategoryLabels } from '../src/i18n/validate';
import type { DocBase } from '../src/i18n/draft';
import projectsEnUiJson from '../src/i18n/locales/en/ui/projects.json';
import projectsViUiJson from '../src/i18n/locales/vi/ui/projects.json';
const fail = (msg: string): never => {
console.error(`FAIL: ${msg}`);
process.exit(1);
};
// Rule 7's read-only input, exactly as the edit session supplies it.
const LABELS: CategoryLabels = {
vi: projectsViUiJson.categories as Record<string, unknown>,
en: projectsEnUiJson.categories as Record<string, unknown>,
};
const bases = bundledBase();
const store = new DraftStore(structuredClone(bases), LABELS);
const projects = store.dataOf('projects') as { id: string }[];
const firstProject = projects[0].id;
// 1. setValue on a text path is accepted and tracked in the entity document
// of the edited language; the diff stores the document-relative pointer.
const rejected = store.setValue('vi', `projects.${firstProject}.summary`, 'SUMMARY CHANGED');
if (rejected !== null) fail(`valid edit rejected: ${JSON.stringify(rejected)}`);
if (!store.isDirty()) fail('store not dirty after accepted edit');
const paths = store.changedPaths('projects.vi');
if (!paths.includes(`${firstProject}.summary`)) fail(`path not diffed: ${paths}`);
// 2. Empty required value is rejected and leaves the draft untouched.
const before = structuredClone(store.dataOf('profile.vi'));
const empty = store.setValue('vi', 'profile.bio', ' ');
if (empty === null) fail('empty required value accepted');
if (JSON.stringify(store.dataOf('profile.vi')) !== JSON.stringify(before))
fail('rejected edit mutated draft');
// 3. Delete removes the structure record AND both text entries — no orphan.
const del = store.deleteRecord('project', { id: firstProject });
if (del !== null) fail(`delete rejected: ${JSON.stringify(del)}`);
const stillListed = (store.dataOf('projects') as { id: string }[]).some((p) => p.id === firstProject);
if (stillListed) fail('project still in structure after delete');
const viText = store.dataOf('projects.vi') as Record<string, unknown>;
const enText = store.dataOf('projects.en') as Record<string, unknown>;
if (viText[firstProject]) fail('vi text orphan after delete');
if (enText[firstProject]) fail('en text orphan after delete');
const issues = store.validate();
if (issues.length > 0) fail(`validate after delete: ${JSON.stringify(issues)}`);
// 4. addRecord creates a complete record in every document at once.
const add = store.addRecord('project');
if (add !== null) fail(`add rejected: ${JSON.stringify(add)}`);
const baseIds = new Set((bases.projects.data as { id: string }[]).map((p) => p.id));
const newIds = (store.dataOf('projects') as { id: string }[]).map((p) => p.id).filter((id) => !baseIds.has(id));
if (newIds.length !== 1) fail(`expected exactly one new project, got ${newIds}`);
const issuesAfterAdd = store.validate();
if (issuesAfterAdd.length > 0) fail(`validate after add: ${JSON.stringify(issuesAfterAdd)}`);
// 5. snapshot / applySaved round-trip reproduces the same changed paths.
const snapshot = store.snapshot();
const restored = new DraftStore(structuredClone(bases));
const drifted = restored.applySaved(snapshot);
if (drifted.length > 0) fail(`clean rebase reported drift: ${drifted}`);
const same =
JSON.stringify(restored.changedDocs()) === JSON.stringify(store.changedDocs());
if (!same) fail('restored draft differs from saved snapshot');
// 6. Conflict: base changed at a path the draft also touched → rebase reports
// a conflict; take-theirs (discardDocument) clears it.
const store2 = new DraftStore(structuredClone(bases), LABELS);
store2.setValue('vi', `projects.${firstProject}.summary`, 'MINE');
const driftedBase = structuredClone(bases);
(driftedBase['projects.vi'].data as Record<string, Record<string, string>>)[firstProject].summary = 'THEIRS';
store2.resetBase('projects.vi', driftedBase['projects.vi']);
const rebaseIssues = store2.rebase('projects.vi');
if (rebaseIssues === null) fail('rebase silently took one side on a real conflict');
store2.discardDocument('projects.vi');
if (store2.changedPaths('projects.vi').includes(`${firstProject}.summary`))
fail('take-theirs left draft residue');
// 7. Clean rebase: base changed at a DIFFERENT path → draft edit survives.
const store3 = new DraftStore(structuredClone(bases), LABELS);
store3.setValue('vi', `projects.${firstProject}.summary`, 'MINE');
const otherBase = structuredClone(bases);
const otherId = projects[1].id;
(otherBase['projects.vi'].data as Record<string, Record<string, string>>)[otherId].summary = 'THEIRS';
store3.resetBase('projects.vi', otherBase['projects.vi']);
const clean = store3.rebase('projects.vi');
if (clean !== null) fail(`clean rebase rejected: ${JSON.stringify(clean)}`);
const mergedText = store3.dataOf('projects.vi') as Record<string, Record<string, string>>;
if (mergedText[firstProject].summary !== 'MINE') fail('rebase lost the draft edit');
if (mergedText[otherId].summary !== 'THEIRS') fail('rebase lost the base edit');
// 8. A text document that does not hold an array field yet (an older target
// branch — the field still lives in the structure document) must gain a real
// array when a [0] path is committed; an object-keyed pseudo-array crashed
// TagListEditor's items.map on the re-render after the first tag edit.
const store4 = new DraftStore(structuredClone(bases), LABELS);
const baseVi = store4.dataOf('profile.vi') as Record<string, Record<string, unknown>>;
delete baseVi.currentInterests;
const tagEdit = store4.setValue('vi', 'profile.currentInterests[0]', 'NEW TAG');
if (tagEdit !== null) fail(`tag edit rejected: ${JSON.stringify(tagEdit)}`);
const arr = (store4.dataOf('profile.vi') as Record<string, Record<string, unknown>>).currentInterests;
if (!Array.isArray(arr)) fail(`setPath made ${JSON.stringify(arr)}, not an array`);
if (arr[0] !== 'NEW TAG') fail('array element not set');
// 9. A skill-group delete removes the record and its text in both languages,
// and the draft still validates.
const store5 = new DraftStore(structuredClone(bases), LABELS);
const groupIds = (store5.dataOf('skills') as { id: string }[]).map((g) => g.id);
const victim = groupIds[groupIds.length - 1];
const delGroup = store5.deleteRecord('skill-group', { id: victim });
if (delGroup !== null) fail(`skill-group delete rejected: ${JSON.stringify(delGroup)}`);
const afterDelete = store5.validate();
if (afterDelete.length > 0) fail(`validate after delete: ${JSON.stringify(afterDelete)}`);
if ((store5.dataOf('skills.vi') as Record<string, unknown>)[victim])
fail('vi skill-group text orphan after delete');
if ((store5.dataOf('skills.en') as Record<string, unknown>)[victim])
fail('en skill-group text orphan after delete');
// 10. A created record keeps following its title: renaming re-sluges the id in
// the structure document and both text documents at once (E3). 'Home' → 'home'
// is the case a naive substring replacement would break.
const store6 = new DraftStore(structuredClone(bases), LABELS);
const addGroup = store6.addRecord('skill-group');
if (addGroup !== null) fail(`skill-group add rejected: ${JSON.stringify(addGroup)}`);
const known = new Set(groupIds);
const created = (store6.dataOf('skills') as { id: string }[]).find((g) => !known.has(g.id));
if (!created) fail('no new skill group created');
const rename = store6.renameRecord('skill-group', { id: created.id }, 'Home');
if (rename !== null) fail(`rename rejected: ${JSON.stringify(rename)}`);
const renamed = (store6.dataOf('skills') as { id: string }[]).map((g) => g.id);
if (!renamed.includes('home')) fail('rename did not apply');
if (renamed.includes(created.id)) fail('the pre-rename id survived');
const renamedText = store6.dataOf('skills.vi') as Record<string, unknown>;
if (renamedText[created.id]) fail('text kept the pre-rename id');
if (!renamedText.home) fail('text did not follow the renamed id');
const afterRename = store6.validate();
if (afterRename.length > 0) fail(`validate after rename: ${JSON.stringify(afterRename)}`);
// 11. Rule 7 (spec-v5 D4): a category a project uses must have a label in
// both languages — the badge would render empty. The category union is
// generated from the documents, so a value written straight into
// src/data/projects.json is exactly what this catches.
const store7 = new DraftStore(structuredClone(bases), LABELS);
const rejectedCategory = store7.setStructured('projects', '[0].category', 'not-a-category');
if (rejectedCategory === null) fail('a label-less category was accepted');
const issuePaths = rejectedCategory.map((issue) => issue.path).sort();
const expectedPaths = [
'src/i18n/locales/en/ui/projects.json: categories.not-a-category',
'src/i18n/locales/vi/ui/projects.json: categories.not-a-category',
].sort();
if (JSON.stringify(issuePaths) !== JSON.stringify(expectedPaths))
fail(`rule 7 reported the wrong issues: ${JSON.stringify(issuePaths)}`);
if (store7.isDirty()) fail('a rejected structure write mutated the draft');
const acceptedCategory = store7.setStructured('projects', '[0].category', 'logistics');
if (acceptedCategory !== null) fail(`labeled category rejected: ${JSON.stringify(acceptedCategory)}`);
// 12. adoptBase (spec-v4-live-edit §5.1): a document the draft has not touched
// accepts the live baseline a later navigation read brings; a document it has
// already changed keeps its draft and its origin — a late read never silently
// replaces work.
const store8 = new DraftStore(structuredClone(bases), LABELS);
// The live copy keeps the bundled ids: an adopted structure document whose ids
// have no text entries would be a real validation failure, not a probe.
const liveGroups = (structuredClone(bases.skills.data) as { id: string; skills: string[] }[]).map(
(group) => ({ ...group, skills: [...group.skills, 'LIVE SKILL'] }),
);
const liveSkills: DocBase = { data: liveGroups, sha: 'live-sha' };
store8.adoptBase('skills', liveSkills);
if (store8.shaOf('skills') !== 'live-sha') fail('adoptBase kept the bundled sha');
if (JSON.stringify(store8.dataOf('skills')) !== JSON.stringify(liveSkills.data))
fail('adoptBase did not replace the bundled data');
if (store8.setValue('vi', 'profile.bio', 'KEEP MINE') !== null)
fail('the touched-document edit was rejected');
const liveProfile: DocBase = { data: { bio: 'THEIR BIO' }, sha: 'other-sha' };
store8.adoptBase('profile.vi', liveProfile);
if (store8.shaOf('profile.vi') !== '') fail('a touched document adopted a live sha');
if ((store8.dataOf('profile.vi') as Record<string, unknown>).bio !== 'KEEP MINE')
fail('a late read replaced drafted work');
// 13. A baseline that came from the bundle carries no blob sha, so the save
// pipeline must not PUT it over the branch (the API reads an empty sha as a
// create). It reads the branch first — with no proxy reachable here that read
// fails, so the document reports a failure, the save stops before any write,
// and the draft survives.
const store9 = new DraftStore(structuredClone(bases), LABELS);
if (store9.setValue('vi', 'profile.bio', 'SCOPED BASELINE') !== null)
fail('the scoped-save probe edit was rejected');
const saveProbe = await saveDraft(store9, () => {}, 'master');
const firstResult = saveProbe.results[0];
if (!firstResult || firstResult.id !== 'profile.vi')
fail(`unexpected save results: ${JSON.stringify(saveProbe.results)}`);
if (firstResult.status !== 'failed')
fail(`a bundle-seeded draft was not read from the branch first: ${firstResult.status}`);
if (!saveProbe.stoppedEarly) fail('the save continued after the branch read failed');
if (saveProbe.results.length !== 1) fail('the save kept going past the stopped document');
if ((store9.dataOf('profile.vi') as Record<string, unknown>).bio !== 'SCOPED BASELINE')
fail('a failed save lost the draft');
// 14. A blob-sha conflict belongs to the conflict flow, not to a hard stop.
// GitHub answers 409 Conflict with a body the Gitea-era message matcher would
// not recognise — the status alone must drive the flow: refresh that document's
// base from the branch, report a conflict, and keep the sequence going.
const store10 = new DraftStore(structuredClone(bases), LABELS);
for (const id of ['profile.vi', 'profile.en'] as const) {
const branchCopy = structuredClone(bases[id]);
branchCopy.sha = `branch-${id}`;
store10.resetBase(id, branchCopy);
}
if (store10.setValue('vi', 'profile.bio', 'CONFLICT MINE') !== null)
fail('the conflict-probe edit was rejected');
if (store10.setValue('en', 'profile.bio', 'CONFLICT MINE EN') !== null)
fail('the second conflict-probe edit was rejected');
const branchDoc = (id: 'profile.vi' | 'profile.en', bio: string) => {
const data = structuredClone(bases[id].data) as Record<string, unknown>;
data.bio = bio;
return { encoded: Buffer.from(JSON.stringify(data, null, 2)).toString('base64'), sha: `moved-${id}` };
};
const viBranch = branchDoc('profile.vi', 'THEIR BIO');
const enBranch = branchDoc('profile.en', 'THEIR BIO EN');
const realFetch = globalThis.fetch;
globalThis.fetch = (async (input: unknown, init?: { method?: string }) => {
const url = String(input);
if (!url.startsWith(PROXY_BASE)) fail(`the save left the proxy: ${url}`);
const branch = url.includes('/vi/content/') ? viBranch : enBranch;
if ((init?.method ?? 'GET') === 'PUT')
return new Response(JSON.stringify({ message: 'Conflict', documentation_url: 'https://docs.github.com/rest' }), { status: 409 });
return new Response(JSON.stringify({ content: branch.encoded, sha: branch.sha }), { status: 200 });
}) as unknown as typeof fetch;
const conflictSave = await saveDraft(store10, () => {}, 'master');
globalThis.fetch = realFetch;
if (conflictSave.results.length !== 2)
fail(`a 409 cut the save short: ${JSON.stringify(conflictSave.results)}`);
if (!conflictSave.results.every((r) => r.status === 'conflict' && r.http === 409))
fail(`a 409 was not reported as a conflict: ${JSON.stringify(conflictSave.results)}`);
if (conflictSave.stoppedEarly) fail('a conflict stopped the save sequence');
if (store10.shaOf('profile.vi') !== 'moved-profile.vi') fail('the conflict did not refresh the base from the branch');
if (store10.shaOf('profile.en') !== 'moved-profile.en') fail('the second conflict did not refresh its base');
if ((store10.dataOf('profile.vi') as Record<string, unknown>).bio !== 'CONFLICT MINE')
fail('a conflict dropped the draft');
// The refreshed base is what the conflict UI resolves against: my path and the
// branch path both moved, so the store must hand the field over, not pick a side.
if (store10.rebase('profile.vi') === null) fail('a 409 left nothing for the conflict UI to resolve');
console.log('draft store smoke: all 14 checks passed');