Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

54.4 KB
Raw Blame
import { randomUUID } from "node:crypto";
import { readFile } from "node:fs/promises";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { createRouterClient } from "@orpc/server";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import { createCoverLetterResumeData } from "@reactive-resume/resume/cover-letter";
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
import { coverLettersRouter } from "./router";
import { coverLetterService as service } from "./service";

const fixture = vi.hoisted(() => ({
	db: undefined as ReturnType<typeof drizzle> | undefined,
	pool: undefined as Pool | undefined,
}));
vi.mock("@reactive-resume/db/client", () => ({
	get db() {
		return fixture.db;
	},
	getPool: () => fixture.pool,
}));
vi.mock("../../context", async () => {
	const { os } = await vi.importActual<typeof import("@orpc/server")>("@orpc/server");
	return { protectedProcedure: os.$context<{ user: { id: string } }>() };
});

function getPool(): Pool {
	if (!fixture.pool) throw new Error("Test database is not initialized");
	return fixture.pool;
}

// Opt-in real PostgreSQL tests. Use a disposable database; all tables live in an isolated schema.
describe.skipIf(!process.env.COVER_LETTER_TEST_DATABASE_URL)("cover-letter owned persistence", () => {
	const schemaName = `cover_letter_test_${randomUUID().replaceAll("-", "")}`;
	let admin: Pool;

	beforeAll(async () => {
		admin = new Pool({ connectionString: process.env.COVER_LETTER_TEST_DATABASE_URL });
		await admin.query(`CREATE SCHEMA ${schemaName}`);
		fixture.pool = new Pool({
			connectionString: process.env.COVER_LETTER_TEST_DATABASE_URL,
			options: `-c search_path=${schemaName}`,
		});
		fixture.db = drizzle({ client: fixture.pool });
		await fixture.pool.query(
			`CREATE TABLE "user" (id text PRIMARY KEY); CREATE TABLE resume (id text PRIMARY KEY, user_id text, data jsonb,
			name text NOT NULL DEFAULT 'Resume', slug text NOT NULL DEFAULT 'resume', tags text[] NOT NULL DEFAULT '{}',
			is_public boolean NOT NULL DEFAULT false, show_download_buttons boolean NOT NULL DEFAULT true,
			allow_word_download boolean NOT NULL DEFAULT false,
			show_in_search_engines boolean NOT NULL DEFAULT false, hide_contact_details boolean NOT NULL DEFAULT false,
			is_primary boolean NOT NULL DEFAULT false, is_locked boolean NOT NULL DEFAULT false, auto_name boolean NOT NULL DEFAULT false,
			auto_slug boolean NOT NULL DEFAULT false,
			trashed_at timestamptz, password text, application_id text, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), edited_at timestamptz DEFAULT now());
			CREATE TABLE resume_version (id text PRIMARY KEY, resume_id text, user_id text, data jsonb, kind text, session_id text, name text, created_at timestamptz NOT NULL DEFAULT now());
			CREATE UNIQUE INDEX resume_version_session_unique ON resume_version(resume_id,session_id) WHERE kind='auto'; CREATE TABLE application (id text PRIMARY KEY, user_id text, company text NOT NULL DEFAULT '', contacts jsonb NOT NULL DEFAULT '[]', cover_letter_id text, updated_at timestamptz, resume_id text, status text DEFAULT 'saved', sent_resume_version_id text, posting_source jsonb, trashed_at timestamptz);`,
		);
		await fixture.pool.query(
			await readFile(
				new URL("../../../../../migrations/20261103134258_red_korg/migration.sql", import.meta.url),
				"utf8",
			),
		);
		// The migrations that shape the letter tables, in order.
		for (const name of [
			"20260905121445_cover_letter_library",
			"20261001042749_v6_release",
			"20261009132123_left_proteus",
			"20261101050044_rx114_content_edited_at",
			"20261103112850_magenta_shadow_king",
		]) {
			const migration = await readFile(
				new URL(`../../../../../migrations/${name}/migration.sql`, import.meta.url),
				"utf8",
			);
			// This fixture owns only the letter tables; full upgrade coverage lives in the DB package.
			for (const statement of migration.split("--> statement-breakpoint")) {
				const sql = statement.trim().replace(/^(?:--[^\n]*\n)+/, "");
				if (
					/^(?:CREATE TABLE|ALTER TABLE) "cover_letter(?:_version)?"|^CREATE (?:UNIQUE )?INDEX .* ON "cover_letter(?:_version)?"/.test(
						sql,
					) ||
					/^CREATE FUNCTION "touch_edited_at"|^CREATE TRIGGER "cover_letter_edited_at"/.test(sql) ||
					/^ALTER TABLE "application" .*"sent_cover_letter_version_id"/.test(sql)
				) {
					await fixture.pool.query(sql.replaceAll('"public".', ""));
				}
			}
		}
		await fixture.pool
			.query(`CREATE TABLE agent_threads (id text PRIMARY KEY, user_id text, working_resume_id text, cover_letter_id text, edits_accepted integer DEFAULT 0, updated_at timestamptz DEFAULT now());
		CREATE TABLE agent_messages (id text PRIMARY KEY, user_id text, thread_id text, role text DEFAULT 'assistant', status text DEFAULT 'streaming', sequence integer DEFAULT 0, error text, ui_message jsonb, created_at timestamptz DEFAULT now(), updated_at timestamptz DEFAULT now());`);
	});
	afterAll(async () => {
		await fixture.pool?.end();
		await admin?.query(`DROP SCHEMA IF EXISTS ${schemaName} CASCADE`);
		await admin?.end();
	});
	beforeEach(async () => {
		await getPool().query(
			'TRUNCATE "user",resume,resume_version,application,cover_letter,agent_threads,agent_messages CASCADE',
		);
		await getPool().query("INSERT INTO \"user\" VALUES ('alice'),('bob')");
		await getPool().query(
			"INSERT INTO resume (id,user_id,data) VALUES ('alice-resume','alice',$1),('bob-resume','bob',$1)",
			[defaultResumeData],
		);
		await getPool().query("INSERT INTO application VALUES ('alice-app','alice'),('bob-app','bob')");
	});

	// A real proposal: an accepted receipt needs its current wording in the saved document, and a resume set records
	// the job it tailors to (none here).
	const proposing = (status: "accepted" | "pending" | "rejected", kind: "resume" | "letter", after: string) => ({
		id: "message",
		role: "assistant",
		parts: [
			{
				type: "tool-propose_edits",
				toolCallId: "call",
				state: "output-available",
				output: {
					...(kind === "resume" ? { tailoredFor: null } : {}),
					edits: [
						{
							id: "edit",
							status,
							target: { sectionId: kind === "letter" ? "letter" : "summary", field: "content" },
							location: "",
							before: "<p>Original</p>",
							after,
							why: "",
						},
					],
				},
			},
		],
	});
	async function proposalThread(
		id: string,
		owner: string,
		kind: "resume" | "letter",
		documentId: string,
		status: "accepted" | "pending" | "rejected" = "pending",
		after = kind === "letter" ? "<p>Rewritten</p>" : "<p>Original</p><p>Added</p>",
	) {
		await getPool().query(
			`INSERT INTO agent_threads (id,user_id,working_resume_id,cover_letter_id) VALUES ($1,$2,$3,$4)`,
			[id, owner, kind === "resume" ? documentId : null, kind === "letter" ? documentId : null],
		);
		await getPool().query(`INSERT INTO agent_messages (id,user_id,thread_id,ui_message) VALUES ($1,$2,$3,$4)`, [
			id,
			owner,
			id,
			proposing(status, kind, after),
		]);
	}
	async function outcomes() {
		return (
			await getPool().query(
				`SELECT t.id,t.edits_accepted AS accepted,m.ui_message->'parts'->0->'output'->'edits'->0->>'status' AS status FROM agent_threads t JOIN agent_messages m ON m.thread_id=t.id ORDER BY t.id`,
			)
		).rows;
	}

	it("serializes streamed message rewrites with document receipts at the actual PostgreSQL read barrier", async () => {
		await getPool().query(
			"ALTER TABLE agent_messages ADD COLUMN IF NOT EXISTS status text; ALTER TABLE agent_threads ADD COLUMN IF NOT EXISTS last_message_at timestamptz;",
		);
		const letter = await service.create({ userId: "alice", name: "Concurrent receipt", content: "<p>Old</p>" });
		await proposalThread("streaming", "alice", "letter", letter.id, "pending", "<p>New</p>");
		const { upsertAssistantUiMessage } = await import("../agent/messages-persistence");
		let release!: () => void;
		const paused = new Promise<void>((resolve) => {
			release = resolve;
		});
		let read!: () => void;
		const captured = new Promise<void>((resolve) => {
			read = resolve;
		});
		// Pause only after the real SELECT executes. The production transaction must still hold its locks.
		let intercepted = false;
		const instrument = (target: object): object =>
			new Proxy(target, {
				get(target, key, receiver) {
					const value = Reflect.get(target, key, receiver);
					if (key === "transaction")
						return (callback: (tx: object) => Promise<unknown>) =>
							value.call(target, (tx: object) => callback(instrument(tx)));
					if (key === "select" || key === "from" || key === "where" || key === "orderBy" || key === "for")
						return (...args: unknown[]) => instrument(value.apply(target, args));
					if (key === "then")
						return (resolve: (rows: unknown) => void, reject: (error: unknown) => void) =>
							value.call(
								target,
								async (rows: unknown) => {
									if (!intercepted) {
										intercepted = true;
										read();
										await paused;
									}
									resolve(rows);
								},
								reject,
							);
					return typeof value === "function" ? value.bind(target) : value;
				},
			});
		const database = fixture.db;
		if (!database) throw new Error("Missing database");
		const writing = upsertAssistantUiMessage(
			{
				userId: "alice",
				threadId: "streaming",
				rowId: "streaming",
				message: proposing("pending", "letter", "<p>New</p>") as never,
				status: "completed",
			},
			instrument(database) as never,
		);
		await captured;
		let committed = false;
		const saving = service
			.update({
				userId: "alice",
				id: letter.id,
				expectedRevision: letter.revision,
				content: "<p>New</p>",
				proposalApplications: { edit: true },
			})
			.then((result) => {
				committed = true;
				return result;
			});
		let blocked = false;
		try {
			for (let attempt = 0; attempt < 100 && !committed && !blocked; attempt++) {
				const result = await getPool().query(
					"SELECT EXISTS (SELECT 1 FROM pg_stat_activity WHERE datname = current_database() AND cardinality(pg_blocking_pids(pid)) > 0) AS blocked",
				);
				blocked = result.rows[0].blocked;
			}
		} finally {
			release();
		}
		await Promise.all([writing, saving]);
		expect(blocked).toBe(true);
		expect(await outcomes()).toEqual([{ id: "streaming", accepted: 1, status: "accepted" }]);
		expect((await service.getById({ userId: "alice", id: letter.id })).content).toBe("<p>New</p>");
	});

	it("atomically saves letter acceptance/Undo/Redo while isolating users, documents and explicit rejection", async () => {
		const letter = await service.create({ userId: "alice", name: "Assistant letter", content: "<p>Original</p>" });
		await proposalThread("own", "alice", "letter", letter.id);
		await proposalThread("other-document", "alice", "letter", "other");
		await proposalThread("other-user", "bob", "letter", letter.id);
		await proposalThread("resume", "alice", "resume", letter.id);
		await proposalThread("rejected", "alice", "letter", letter.id, "rejected");
		const accepted = await service.update({
			userId: "alice",
			id: letter.id,
			expectedRevision: letter.revision,
			content: "<p>Rewritten</p>",
			proposalApplications: { edit: true },
		});
		expect(await outcomes()).toEqual([
			{ id: "other-document", accepted: 0, status: "pending" },
			{ id: "other-user", accepted: 0, status: "pending" },
			{ id: "own", accepted: 1, status: "accepted" },
			{ id: "rejected", accepted: 0, status: "rejected" },
			{ id: "resume", accepted: 0, status: "pending" },
		]);
		const undone = await service.update({
			userId: "alice",
			id: letter.id,
			expectedRevision: accepted.revision,
			content: "<p>Original</p>",
			proposalApplications: { edit: false },
		});
		expect((await outcomes()).find((row) => row.id === "own")).toMatchObject({ accepted: 0, status: "pending" });
		await service.update({
			userId: "alice",
			id: letter.id,
			expectedRevision: undone.revision,
			content: "<p>Rewritten</p>",
			proposalApplications: { edit: true },
		});
		expect((await outcomes()).find((row) => row.id === "own")).toMatchObject({ accepted: 1, status: "accepted" });
		await expect(
			service.update({
				userId: "alice",
				id: letter.id,
				expectedRevision: accepted.revision,
				content: "<p>Stale</p>",
				proposalApplications: { edit: false },
			}),
		).rejects.toMatchObject({ code: "CONFLICT" });
		expect((await outcomes()).find((row) => row.id === "own")).toMatchObject({ accepted: 1, status: "accepted" });
	});

	it("rolls back letter content and revision if proposal synchronization fails", async () => {
		const letter = await service.create({ userId: "alice", name: "Atomic letter", content: "<p>Original</p>" });
		await proposalThread("own", "alice", "letter", letter.id);
		await getPool()
			.query(`CREATE FUNCTION reject_proposal_update() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'simulated status storage failure'; END $$;
		CREATE TRIGGER reject_proposal_update BEFORE UPDATE ON agent_messages FOR EACH ROW EXECUTE FUNCTION reject_proposal_update();`);
		try {
			await expect(
				service.update({
					userId: "alice",
					id: letter.id,
					expectedRevision: letter.revision,
					content: "<p>Rewritten</p>",
					proposalApplications: { edit: true },
				}),
			).rejects.toThrow();
			expect(await service.getById({ userId: "alice", id: letter.id })).toMatchObject({
				content: "<p>Original</p>",
				revision: letter.revision,
			});
			expect(await outcomes()).toEqual([{ id: "own", accepted: 0, status: "pending" }]);
		} finally {
			await getPool().query(
				"DROP TRIGGER reject_proposal_update ON agent_messages; DROP FUNCTION reject_proposal_update()",
			);
		}
	});

	it("persists resume acceptance, Undo and Redo through the real router with the saved document", async () => {
		const { crudRouter } = await import("../resume/crud");
		const { resumeService } = await import("../resume/service");
		const client = createRouterClient(crudRouter, { context: { user: { id: "alice" } } as never });
		await proposalThread("own", "alice", "resume", "alice-resume");
		let current: Awaited<ReturnType<typeof client.update>> = await resumeService.getById({
			id: "alice-resume",
			userId: "alice",
		});
		for (const [applied, content] of [
			[true, "<p>Original</p><p>Added</p>"],
			[false, "<p>Original</p>"],
			[true, "<p>Original</p><p>Added</p>"],
		] as const) {
			const data = structuredClone(current.data);
			data.summary.content = content;
			const saved = await client.update({
				id: current.id,
				data,
				expectedUpdatedAt: current.updatedAt,
				proposalApplications: { edit: applied },
			});
			current = { ...current, ...saved };
			expect((await resumeService.getById({ id: current.id, userId: "alice" })).data.summary.content).toBe(content);
			expect(await outcomes()).toEqual([
				{ id: "own", accepted: applied ? 1 : 0, status: applied ? "accepted" : "pending" },
			]);
		}
	});

	it("saves structural proposal snapshots and their explicit Undo/Redo through the resume router", async () => {
		const { crudRouter } = await import("../resume/crud");
		const { documentView, loadDocument, resolveDocumentEdits } = await import("../agent/document");
		const { createEntry } = await import("@reactive-resume/resume/entries");
		const { buildAgentTools } = await import("../agent/tools");
		const { pruneAgentModelContext } = await import("../agent/context");
		const { ToolLoopAgent, isStepCount, convertToModelMessages } = await import("ai");
		const { MockLanguageModelV4 } = await import("ai/test");
		const seeded = structuredClone(defaultResumeData);
		seeded.basics.name = "VISIBLE_NAME";
		seeded.summary = { ...seeded.summary, hidden: true, content: "<p>HIDDEN_SUMMARY</p>" };
		seeded.sections.projects = { ...seeded.sections.projects, hidden: true, title: "HIDDEN_SECTION" };
		seeded.sections.skills.items = [
			{ ...createEntry("skills"), id: "secret-skill", hidden: true, name: "HIDDEN_ITEM" },
		] as typeof seeded.sections.skills.items;
		seeded.customSections = [
			{
				...seeded.sections.certifications,
				id: "secret-custom",
				type: "certifications",
				hidden: true,
				title: "HIDDEN_CUSTOM",
			},
		];
		seeded.metadata.notes = "PRIVATE_NOTES";
		seeded.picture.url = "https://fictional.invalid/PRIVATE_PICTURE";
		await getPool().query("UPDATE resume SET data=$1 WHERE id='alice-resume'", [seeded]);
		const { applyResumeProposals } = await import("@reactive-resume/resume/proposals");
		const document = await loadDocument("alice", { kind: "resume", id: "alice-resume" });
		if (!document.resume) throw new Error("Missing resume");
		const original = structuredClone(document.resume);
		const request = {
			title: "Review headline",
			changes: [
				{
					kind: "field",
					target: { sectionId: "basics", field: "headline" },
					before: "",
					value: "Design lead",
					why: "User supplied.",
				},
			],
		};
		for (const change of [
			{
				kind: "entry",
				sectionId: "certifications",
				type: "cover-letter",
				entryJson: JSON.stringify({ ...createEntry("cover-letter"), content: "<p>Fictional letter</p>" }),
				why: "User supplied.",
			},
			{
				kind: "section",
				type: "cover-letter",
				title: "Letter",
				entriesJson: JSON.stringify([{ ...createEntry("cover-letter"), content: "<p>Fictional letter</p>" }]),
				why: "User supplied.",
			},
		]) {
			expect(() => resolveDocumentEdits(document, { title: "Letter", changes: [change] } as never)).toThrow(
				"This suggestion couldn't be prepared",
			);
		}
		let output: ReturnType<typeof resolveDocumentEdits> | undefined;
		const captures: string[] = [];
		let step = 0;
		const tools = buildAgentTools({
			document: "resume",
			externalSearch: false,
			signal: new AbortController().signal,
			handlers: {
				readDocument: async () => documentView(document),
				readAttachment: async () => ({}),
				proposeEdits: async (input) => (output = resolveDocumentEdits(document, input)),
				searchWeb: async () => {
					throw Error("Unexpected search");
				},
				readPage: async () => {
					throw Error("Unexpected page");
				},
			},
		});
		const agent = new ToolLoopAgent({
			tools,
			stopWhen: isStepCount(4),
			prepareStep: ({ messages }) => ({ messages: pruneAgentModelContext(messages) }),
			model: new MockLanguageModelV4({
				doGenerate: async (options) => {
					captures.push(JSON.stringify(options.prompt));
					const current = step++;
					return {
						content:
							current === 0
								? [{ type: "tool-call", toolCallId: "read", toolName: "read_resume", input: "{}" }]
								: current === 1
									? [
											{
												type: "tool-call",
												toolCallId: "call",
												toolName: "propose_edits",
												input: JSON.stringify(request),
											},
										]
									: [{ type: "text", text: "Review changes." }],
						finishReason: { unified: "stop", raw: "stop" },
						usage: {
							inputTokens: { total: 1, noCache: 1, cacheRead: 0, cacheWrite: 0 },
							outputTokens: { total: 1, text: 1, reasoning: 0 },
						},
						warnings: [],
					};
				},
			}),
		});
		await agent.generate({ prompt: "Change my headline." });
		if (!output) throw Error("Missing proposal output");
		expect(captures).toHaveLength(3);
		expect(captures[1]).toContain("VISIBLE_NAME");
		expect((await getPool().query("SELECT data FROM resume WHERE id='alice-resume'")).rows[0].data).toEqual(seeded);

		const edit = output.edits[0];
		if (!edit?.snapshot) throw new Error("Missing structural snapshot");
		await proposalThread("structural", "alice", "resume", "alice-resume");
		const message = proposing("pending", "resume", edit.after);
		const part = message.parts[0];
		if (!part) throw new Error("Missing proposal part");
		part.output = output as never;
		Object.assign(part, { input: request });
		await getPool().query("UPDATE agent_messages SET ui_message=$1 WHERE id='structural'", [message]);
		// Reopen the actual JSONB history; the full snapshot must survive storage but never reach the provider.
		const restored = (await getPool().query("SELECT ui_message FROM agent_messages WHERE id='structural'")).rows[0]
			.ui_message;
		await agent.generate({ messages: await convertToModelMessages([restored]) });
		await agent.generate({
			messages: await convertToModelMessages([
				restored,
				{ id: "answer", role: "user", parts: [{ type: "text", text: "VISIBLE_CONTINUATION" }] },
			]),
		});
		expect(captures.at(-1)).toContain("VISIBLE_CONTINUATION");
		for (const prompt of captures)
			for (const marker of [
				"HIDDEN_SUMMARY",
				"HIDDEN_SECTION",
				"HIDDEN_ITEM",
				"HIDDEN_CUSTOM",
				"PRIVATE_NOTES",
				"PRIVATE_PICTURE",
			])
				expect(prompt).not.toContain(marker);
		for (const marker of ["HIDDEN_SUMMARY", "HIDDEN_ITEM"]) expect(JSON.stringify(restored)).toContain(marker);
		expect((await getPool().query("SELECT count(*)::int AS n FROM cover_letter")).rows[0].n).toBe(0);

		const data = structuredClone(original);
		expect(applyResumeProposals(data, [{ ...edit, source: "assistant" }]).applied).toHaveLength(1);
		const client = createRouterClient(crudRouter, { context: { user: { id: "alice" } } as never });
		let updatedAt = document.updatedAt;
		for (const [applied, content] of [
			[true, data],
			[false, original],
			[true, data],
		] as const) {
			const saved = await client.update({
				id: "alice-resume",
				data: content,
				expectedUpdatedAt: updatedAt,
				proposalApplications: { [edit.id]: applied },
			});
			updatedAt = saved.updatedAt;
			expect(saved.data.basics.headline).toBe(applied ? "Design lead" : "");
			expect(await outcomes()).toEqual([
				{ id: "structural", accepted: applied ? 1 : 0, status: applied ? "accepted" : "pending" },
			]);
			const stored = (await getPool().query("SELECT ui_message FROM agent_messages WHERE id='structural'")).rows[0]
				.ui_message;
			expect(stored.parts[0].output.edits[0].snapshot).toEqual(edit.snapshot);
			expect((await getPool().query("SELECT count(*)::int AS n FROM cover_letter")).rows[0].n).toBe(0);
		}
	});

	it("synchronizes only the owned resume's threads inside the caller transaction", async () => {
		const { saveProposalApplications } = await import("../agent/proposal-applications");
		await proposalThread("own", "alice", "resume", "alice-resume");
		await proposalThread("bob", "bob", "resume", "alice-resume");
		await proposalThread("different", "alice", "resume", "other-resume");
		await proposalThread("letter", "alice", "letter", "alice-resume");
		const data = structuredClone(defaultResumeData);
		data.summary.content = "<p>Original</p><p>Added</p>";
		await getPool().query("UPDATE resume SET data=$1 WHERE id='alice-resume'", [data]);
		const conflict = () => new Error("not placed");
		await fixture.db?.transaction(async (tx) => {
			await saveProposalApplications(tx, {
				userId: "alice",
				document: { kind: "resume", id: "alice-resume" },
				applications: { edit: true },
				conflict,
			});
		});
		expect(await outcomes()).toEqual([
			{ id: "bob", accepted: 0, status: "pending" },
			{ id: "different", accepted: 0, status: "pending" },
			{ id: "letter", accepted: 0, status: "pending" },
			{ id: "own", accepted: 1, status: "accepted" },
		]);
		await expect(
			fixture.db?.transaction(async (tx) => {
				await saveProposalApplications(tx, {
					userId: "alice",
					document: { kind: "resume", id: "alice-resume" },
					applications: { edit: false },
					conflict,
				});
				throw new Error("rollback");
			}),
		).rejects.toThrow("rollback");
		expect((await outcomes()).find((row) => row.id === "own")).toMatchObject({ accepted: 1, status: "accepted" });
	});

	it("persists one shared document and atomically rejects stale writers and deletes", async () => {
		const created = await service.create({ userId: "alice", name: "Draft", content: "<p>First</p>" });
		const saved = await service.update({
			userId: "alice",
			id: created.id,
			expectedRevision: 1,
			content: "<p>Second</p>",
		});
		expect(saved.revision).toBe(2);
		expect((await service.getById({ userId: "alice", id: created.id })).content).toBe("<p>Second</p>");
		await expect(
			service.update({ userId: "alice", id: created.id, expectedRevision: 1, name: "Stale" }),
		).rejects.toMatchObject({ code: "CONFLICT" });
		await expect(service.delete({ userId: "alice", id: created.id, expectedRevision: 1 })).rejects.toMatchObject({
			code: "CONFLICT",
		});
	});

	it("keeps submitted letters and their snapshots intact through every linking path", async () => {
		const original = await service.create({ userId: "alice", name: "Submitted", applicationId: "alice-app" });
		const version = await service.recordSent({ userId: "alice", id: original.id, company: "Lumen" });
		await getPool().query("UPDATE application SET sent_cover_letter_version_id=$1 WHERE id='alice-app'", [version.id]);
		const replacement = await service.create({ userId: "alice", name: "Replacement" });
		const { documentsService } = await import("../documents/service");
		for (const change of [
			() =>
				documentsService.linkApplication({
					userId: "alice",
					type: "letter",
					id: replacement.id,
					applicationId: "alice-app",
				}),
			() => service.update({ userId: "alice", id: replacement.id, expectedRevision: 1, applicationId: "alice-app" }),
			() => service.update({ userId: "alice", id: original.id, expectedRevision: 1, applicationId: null }),
		]) {
			await expect(change()).rejects.toMatchObject({
				code: "BAD_REQUEST",
				data: { reason: "SUBMITTED_DOCUMENT_LOCKED" },
			});
			const application = (
				await getPool().query(
					"SELECT cover_letter_id, sent_cover_letter_version_id FROM application WHERE id='alice-app'",
				)
			).rows[0];
			expect(application).toEqual({ cover_letter_id: original.id, sent_cover_letter_version_id: version.id });
			expect(await service.getById({ userId: "alice", id: replacement.id })).toMatchObject({
				revision: 1,
				sourceApplicationId: null,
			});
		}
		const { getLetterVersion } = await import("./versions");
		expect(
			await getLetterVersion({ userId: "alice", coverLetterId: original.id, versionId: version.id }),
		).toMatchObject({ data: { name: "Submitted" } });
	});

	it("lists permanent checkpoints behind more than 100 recent autosaves", async () => {
		const letter = await service.create({ userId: "alice", name: "Long history" });
		const named = await service.createVersion({ userId: "alice", id: letter.id, name: "Keep forever" });
		const sent = await service.recordSent({ userId: "alice", id: letter.id, company: "Lumen" });
		await getPool().query(
			`INSERT INTO cover_letter_version (id, cover_letter_id, user_id, data, kind, created_at)
			SELECT 'auto-' || n, $1, 'alice', '{}'::jsonb, 'auto', now() + n * interval '1 second' FROM generate_series(1,120) n`,
			[letter.id],
		);
		const { listLetterVersions } = await import("./versions");
		const versions = await listLetterVersions({ userId: "alice", coverLetterId: letter.id });
		expect(versions.map((version) => version.id)).toEqual(expect.arrayContaining([named.id, sent.id]));
		expect(versions.filter((version) => version.kind === "auto")).toHaveLength(100);
		await expect(listLetterVersions({ userId: "bob", coverLetterId: letter.id })).rejects.toMatchObject({
			code: "NOT_FOUND",
		});
	});

	it("accepts procedure defaults, rejects invalid input, and permits only one concurrent writer", async () => {
		const client = createRouterClient(coverLettersRouter, { context: { user: { id: "alice" } } as never });
		const created = await client.create({ name: "Through procedure" });
		expect(created).toMatchObject({ recipient: "", content: "", revision: 1 });
		await expect(client.create({ name: " " })).rejects.toMatchObject({ code: "BAD_REQUEST" });
		await expect(client.list({ limit: 101 })).rejects.toMatchObject({ code: "BAD_REQUEST" });
		const results = await Promise.allSettled([
			client.update({ id: created.id, expectedRevision: 1, content: "First editor" }),
			client.update({ id: created.id, expectedRevision: 1, content: "Second editor" }),
		]);
		expect(results.filter((result) => result.status === "fulfilled")).toHaveLength(1);
		expect(results.find((result) => result.status === "rejected")).toMatchObject({ reason: { code: "CONFLICT" } });
		const persisted = (await getPool().query("SELECT revision, content FROM cover_letter WHERE id=$1", [created.id]))
			.rows[0];
		expect(persisted.revision).toBe(2);
		expect(["First editor", "Second editor"]).toContain(persisted.content);
	});

	it("preserves custom and omitted letter framing through saves, copies, JSON and history", async () => {
		const client = createRouterClient(coverLettersRouter, { context: { user: { id: "alice" } } as never });
		const created = await client.create({ name: "Custom framing", greeting: "Hallo Dana,", signOff: "Vielen Dank!" });
		const custom = { greeting: "Hallo Dana,", signOff: "Vielen Dank!" };
		expect(await client.getById({ id: created.id })).toMatchObject(custom);
		expect(await client.duplicate({ id: created.id })).toMatchObject(custom);
		const document = await client.export({ id: created.id });
		expect(document).toMatchObject(custom);
		expect(await client.import({ document })).toMatchObject(custom);
		const named = await client.createVersion({ id: created.id, name: "Custom" });
		expect((await client.getVersion({ id: created.id, versionId: named.id })).data).toMatchObject(custom);
		const omitted = await client.update({
			id: created.id,
			expectedRevision: created.revision,
			greeting: "",
			signOff: "",
		});
		expect(await client.getById({ id: created.id })).toMatchObject({ greeting: "", signOff: "" });
		const reset = await client.update({
			id: created.id,
			expectedRevision: omitted.revision,
			greeting: null,
			signOff: null,
		});
		expect(reset).toMatchObject({ greeting: null, signOff: null });
		expect(await client.restoreVersion({ id: created.id, versionId: named.id })).toMatchObject(custom);
		// Existing backups and history have no framing fields: restoring them returns to automatic defaults.
		const { greeting: _greeting, signOff: _signOff, ...oldDocument } = document;
		expect(await client.import({ document: oldDocument as typeof document })).toMatchObject({
			greeting: null,
			signOff: null,
		});
		await getPool().query("UPDATE cover_letter_version SET data = data - 'greeting' - 'signOff' WHERE id=$1", [
			named.id,
		]);
		expect(await client.restoreVersion({ id: created.id, versionId: named.id })).toMatchObject({
			greeting: null,
			signOff: null,
		});
	});

	it("saves letters a resume still carries as linked letters, once, and leaves the resume without them", async () => {
		const { adoptEmbeddedLetters } = await import("./embedded");
		const data = structuredClone(defaultResumeData);
		data.basics.name = "Alice Sender";
		data.customSections = [
			{
				id: "embedded",
				title: "Embedded Letter",
				type: "cover-letter",
				icon: "",
				columns: 1,
				hidden: true,
				keepTogether: false,
				startOnNewPage: false,
				items: [{ id: "embedded-item", hidden: true, recipient: "<p>Recipient</p>", content: "<p>Original</p>" }],
			},
		];
		data.metadata.layout.pages = [{ fullWidth: true, main: ["experience", "embedded"], sidebar: [] }];
		const adopt = (input: typeof data) =>
			adoptEmbeddedLetters(fixture.db as never, {
				userId: "alice",
				resumeId: "alice-resume",
				resumeName: "Frontend",
				data: input,
			});

		const oversized = structuredClone(data);
		oversized.picture.url = `https://example.test/${"x".repeat(100_000)}`;
		const section = oversized.customSections[0];
		if (!section) throw new Error("Missing section");
		section.items = Array.from({ length: 60 }, (_, index) => ({
			id: `item-${index}`,
			hidden: false,
			recipient: "",
			content: "<p>Letter</p>",
		}));
		const original = JSON.stringify(oversized);
		await expect(adopt(oversized)).rejects.toMatchObject({ code: "BAD_REQUEST" });
		expect(JSON.stringify(oversized)).toBe(original);
		expect((await service.list({ userId: "alice", limit: 20, offset: 0 })).items).toHaveLength(0);

		const again = structuredClone(data);
		await adopt(data);
		expect(data.customSections).toEqual([]);
		expect(data.metadata.layout.pages).toEqual([{ fullWidth: true, main: ["experience"], sidebar: [] }]);

		const letters = await service.list({ userId: "alice", limit: 20, offset: 0 });
		expect(letters.items).toHaveLength(1);
		const [letter] = letters.items;
		if (!letter) throw new Error("Missing letter.");
		expect(letter).toMatchObject({
			name: "Frontend — Embedded Letter",
			recipient: "<p>Recipient</p>",
			content: "<p>Original</p>",
			layout: "freeform",
			sourceResumeId: "alice-resume",
			senderLinked: true,
			designLinked: true,
			style: { sectionId: "embedded", itemId: "embedded-item" },
		});
		const versions = await getPool().query("SELECT kind FROM cover_letter_version WHERE cover_letter_id=$1", [
			letter.id,
		]);
		expect(versions.rows).toEqual([{ kind: "created" }]);

		// The same section sent again (an older tab still open) doesn't save a second letter.
		await adopt(again);
		expect((await service.list({ userId: "alice", limit: 20, offset: 0 })).items).toHaveLength(1);
	});

	it("serializes relinking before the letter lock so an application submission can finish", async () => {
		const letter = await service.create({ userId: "alice", name: "For application", applicationId: "alice-app" });
		await getPool().query("INSERT INTO application (id, user_id) VALUES ('alice-next','alice')");
		const editor = await getPool().connect();
		let completion: Promise<{ error: unknown }> | undefined;
		try {
			await editor.query("BEGIN");
			await editor.query("SET LOCAL lock_timeout = '750ms'");
			await editor.query('SELECT id FROM "user" WHERE id = $1 FOR NO KEY UPDATE', ["alice"]);
			await editor.query("SELECT id FROM application WHERE id = 'alice-app' FOR UPDATE");
			const pid = (await editor.query<{ pid: number }>("SELECT pg_backend_pid() AS pid")).rows[0]?.pid;
			completion = service
				.update({ userId: "alice", id: letter.id, expectedRevision: 1, applicationId: "alice-next" })
				.then(
					() => ({ error: null }),
					(error: unknown) => ({ error }),
				);
			await vi.waitFor(
				async () => {
					const blocked = await admin.query("SELECT pid FROM pg_stat_activity WHERE $1 = ANY(pg_blocking_pids(pid))", [
						pid,
					]);
					expect(blocked.rows).not.toHaveLength(0);
				},
				{ timeout: 2500, interval: 10 },
			);
			// Submission already owns application and owner; it must be able to snapshot this letter.
			await editor.query("SELECT id FROM cover_letter WHERE id = $1 FOR UPDATE", [letter.id]);
			await editor.query("COMMIT");
			expect(await completion).toEqual({ error: null });
			expect((await service.getById({ userId: "alice", id: letter.id })).sourceApplicationId).toBe("alice-next");
		} finally {
			await editor.query("ROLLBACK");
			editor.release();
			await completion;
		}
	});

	it("isolates every read, mutation, export and context selection by account", async () => {
		const created = await service.create({ userId: "alice", name: "Private" });
		const foreign = { userId: "bob", id: created.id, expectedRevision: 1 };
		for (const operation of [
			() => service.getById(foreign),
			() => service.update({ ...foreign, name: "Stolen" }),
			() => service.delete(foreign),
			() => service.duplicate(foreign),
			() => service.export(foreign),
			() => service.refreshStyle({ ...foreign, resumeId: "bob-resume" }),
		]) {
			await expect(operation()).rejects.toMatchObject({ code: "NOT_FOUND" });
		}
		expect((await service.list({ userId: "bob", limit: 20, offset: 0 })).items).toEqual([]);
		await expect(service.create({ userId: "alice", name: "Invalid", resumeId: "bob-resume" })).rejects.toMatchObject({
			code: "NOT_FOUND",
		});
		await expect(service.create({ userId: "alice", name: "Invalid", applicationId: "bob-app" })).rejects.toMatchObject({
			code: "NOT_FOUND",
		});
	});

	it("starts structured from the application, linked to the resume's details and design", async () => {
		await getPool().query(
			`UPDATE application SET company='Lumen Health', contacts='[{"name":"Dana Reyes"}]' WHERE id='alice-app'`,
		);
		const created = await service.create({
			userId: "alice",
			name: "For Lumen",
			resumeId: "alice-resume",
			applicationId: "alice-app",
		});
		// It becomes the application's letter.
		const linked = await getPool().query("SELECT cover_letter_id FROM application WHERE id='alice-app'");
		expect(linked.rows[0].cover_letter_id).toBe(created.id);
		expect(created).toMatchObject({
			layout: "structured",
			recipientName: "Dana Reyes",
			recipientCompany: "Lumen Health",
			letterDate: new Date().toISOString().slice(0, 10),
			senderLinked: true,
			designLinked: true,
		});
		// A recipient block keeps a letter freeform, and a template of its own leaves the design unlinked.
		const freeform = await service.create({
			userId: "alice",
			name: "Freeform",
			recipient: "<p>Hiring team</p>",
			resumeId: "alice-resume",
			template: "pikachu",
		});
		expect(freeform).toMatchObject({ layout: "freeform", senderLinked: true, designLinked: false });
		expect(freeform.style.metadata.template).toBe("pikachu");
		// Without a resume there's nothing to link to.
		expect(await service.create({ userId: "alice", name: "Plain" })).toMatchObject({
			senderLinked: false,
			designLinked: false,
		});

		// Moving the letter to another application takes it along.
		await getPool().query("INSERT INTO application (id, user_id) VALUES ('alice-app-2','alice')");
		await service.update({ userId: "alice", id: created.id, expectedRevision: 1, applicationId: "alice-app-2" });
		const moved = await getPool().query(
			"SELECT id, cover_letter_id FROM application WHERE user_id='alice' ORDER BY id",
		);
		expect(moved.rows).toEqual([
			{ id: "alice-app", cover_letter_id: null },
			{ id: "alice-app-2", cover_letter_id: created.id },
		]);
	});

	it("starts a new job's letter from an owned earlier letter without carrying its company or history", async () => {
		await getPool().query(
			`UPDATE application SET company='Northwind Labs', contacts='[{"name":"Dana Reyes"}]' WHERE id='alice-app'`,
		);
		await getPool().query(
			`INSERT INTO application (id, user_id, company, contacts) VALUES ('alice-app-2','alice','Fieldnote','[{"name":"Morgan Lee"}]')`,
		);
		const source = await service.create({
			userId: "alice",
			name: "Northwind letter",
			content: "<p>I led delivery for Northwind's platform team.</p>",
			applicationId: "alice-app",
		});
		const { listLetterVersions } = await import("./versions");
		const sourceVersions = await listLetterVersions({ userId: "alice", coverLetterId: source.id });

		const reused = await service.create({
			userId: "alice",
			name: "Fieldnote letter",
			applicationId: "alice-app-2",
			sourceLetterId: source.id,
		});

		expect(reused).toMatchObject({
			content: source.content,
			recipientCompany: "Fieldnote",
			recipientName: "Morgan Lee",
			layout: "structured",
		});
		expect(await service.getById({ userId: "alice", id: source.id })).toMatchObject({
			content: source.content,
			recipientCompany: "Northwind Labs",
		});
		expect(await listLetterVersions({ userId: "alice", coverLetterId: source.id })).toEqual(sourceVersions);
		expect(await listLetterVersions({ userId: "alice", coverLetterId: reused.id })).toHaveLength(1);

		// Another account's letter, and a trashed one, are not usable sources.
		await expect(service.create({ userId: "bob", name: "Borrowed", sourceLetterId: source.id })).rejects.toMatchObject({
			code: "NOT_FOUND",
		});
		await service.delete({ userId: "alice", id: source.id, expectedRevision: source.revision });
		await expect(
			service.create({ userId: "alice", name: "From Trash", sourceLetterId: source.id }),
		).rejects.toMatchObject({ code: "NOT_FOUND" });
	});

	it("starts a letter in the posting's language and keeps its language independent of the design link", async () => {
		await getPool().query(
			`UPDATE application SET company='Lumen Health', posting_source='{"method":"builtin","format":"text","truncated":false,"completeness":"unknown","language":"de-DE"}' WHERE id='alice-app'`,
		);
		const created = await service.create({
			userId: "alice",
			name: "Auf Deutsch",
			resumeId: "alice-resume",
			applicationId: "alice-app",
		});
		expect(created.style.metadata.page.locale).toBe("de-DE");
		expect(created.designLinked).toBe(true);

		// Changing the letter's language is not changing its design: the link and the resume's own locale stay.
		const updated = await service.update({
			userId: "alice",
			id: created.id,
			expectedRevision: 1,
			locale: "fr-FR",
		});
		expect(updated.style.metadata.page.locale).toBe("fr-FR");
		expect(updated.designLinked).toBe(true);
		expect((await service.getById({ userId: "alice", id: created.id })).style.metadata.page.locale).toBe("fr-FR");
		const resume = await getPool().query(
			"SELECT data->'metadata'->'page'->>'locale' AS locale FROM resume WHERE id='alice-resume'",
		);
		expect(resume.rows[0].locale).not.toBe("fr-FR");
	});

	it("reads linked details and design live, keeps them as they read when unlinked, and outlives the resume", async () => {
		const created = await service.create({
			userId: "alice",
			name: "Keep",
			content: "<p>Keep body</p>",
			resumeId: "alice-resume",
		});
		const changed = structuredClone(defaultResumeData);
		changed.basics.name = "New sender";
		changed.metadata.template = "gengar";
		changed.metadata.page.locale = "de-DE";
		await getPool().query("UPDATE resume SET data=$1 WHERE id='alice-resume'", [changed]);
		expect((await service.getById({ userId: "alice", id: created.id })).style).toMatchObject({
			basics: { name: "New sender" },
			// The resume's design arrives; the letter's own language stays its own.
			metadata: { template: "gengar", page: { locale: "en-US" } },
		});

		const unlinked = await service.update({
			userId: "alice",
			id: created.id,
			expectedRevision: 1,
			senderLinked: false,
		});
		expect(unlinked).toMatchObject({
			senderLinked: false,
			designLinked: true,
			style: { basics: { name: "New sender" } },
		});
		changed.basics.name = "Later sender";
		changed.metadata.template = "azurill";
		await getPool().query("UPDATE resume SET data=$1 WHERE id='alice-resume'", [changed]);
		expect((await service.getById({ userId: "alice", id: created.id })).style).toMatchObject({
			basics: { name: "New sender" },
			metadata: { template: "azurill" },
		});

		// Choosing a template of the letter's own ends the design link, keeping the rest of the design as it read.
		const own = await service.update({ userId: "alice", id: created.id, expectedRevision: 2, template: "ditto" });
		expect(own).toMatchObject({
			designLinked: false,
			style: { metadata: { template: "ditto", page: { locale: "en-US" } } },
		});
		changed.metadata.page.locale = "en-US";
		await getPool().query("UPDATE resume SET data=$1 WHERE id='alice-resume'", [changed]);
		expect((await service.getById({ userId: "alice", id: created.id })).style.metadata.page.locale).toBe("en-US");

		await expect(
			service.update({ userId: "alice", id: created.id, expectedRevision: 3, resumeId: null, senderLinked: true }),
		).rejects.toMatchObject({ code: "BAD_REQUEST" });

		// Once the resume is gone, the letter reads from its copies and is no longer linked.
		const relinked = await service.update({
			userId: "alice",
			id: created.id,
			expectedRevision: 3,
			senderLinked: true,
		});
		expect(relinked.style.basics.name).toBe("Later sender");
		await getPool().query("DELETE FROM resume WHERE id='alice-resume'");
		const orphaned = await service.getById({ userId: "alice", id: created.id });
		expect(orphaned).toMatchObject({ sourceResumeId: null, senderLinked: false, content: "<p>Keep body</p>" });
		expect(
			(await service.update({ userId: "alice", id: created.id, expectedRevision: 4, name: "Still editable" })).name,
		).toBe("Still editable");
	});

	it.each([false, true])("refreshes design while respecting sender linking (%s)", async (senderLinked) => {
		const client = createRouterClient(coverLettersRouter, { context: { user: { id: "alice" } } as never });
		const source = structuredClone(defaultResumeData);
		source.basics.name = "Original sender";
		source.picture.url = "https://example.test/original.png";
		await getPool().query("UPDATE resume SET data=$1 WHERE id='alice-resume'", [source]);
		const created = await client.create({
			name: "Refresh design",
			resumeId: "alice-resume",
			template: "pikachu",
			content: "<p>Keep this letter</p>",
		});
		const letter = senderLinked
			? created
			: await client.update({
					id: created.id,
					expectedRevision: created.revision,
					senderLinked: false,
					sender: {
						name: "Independent sender",
						email: "[email protected]",
						phone: "+49 30 5550100",
						location: "Berlin",
					},
				});
		source.basics.name = "Current resume sender";
		source.basics.email = "[email protected]";
		source.picture.url = "https://example.test/current.png";
		source.metadata.page.marginX = 22;
		await getPool().query("UPDATE resume SET data=$1 WHERE id='alice-resume'", [source]);

		const refreshed = await client.refreshStyle({
			id: letter.id,
			expectedRevision: letter.revision,
			resumeId: "alice-resume",
		});
		const basics = senderLinked ? source.basics : letter.style.basics;
		const picture = senderLinked ? source.picture : letter.style.picture;
		expect(refreshed).toMatchObject({
			content: "<p>Keep this letter</p>",
			senderLinked,
			style: { basics, picture, metadata: { template: "pikachu", page: { marginX: 22 } } },
		});
		expect((await client.getById({ id: letter.id })).style).toMatchObject({ basics, picture });
		expect((await client.export({ id: letter.id })).style).toMatchObject({ basics, picture });
		await getPool().query("DELETE FROM resume WHERE id='alice-resume'");
		expect((await client.getById({ id: letter.id })).style).toMatchObject({ basics, picture });
	});

	it("edits standalone sender details and isolates copies, links and account ownership", async () => {
		const client = createRouterClient(coverLettersRouter, { context: { user: { id: "alice" } } as never });
		const created = await client.create({ name: "Standalone" });
		const sender = { name: "Jordan Reyes", email: "[email protected]", phone: "+49 30 5550100", location: "Berlin" };
		const saved = await client.update({ id: created.id, expectedRevision: 1, sender });
		expect(saved.style.basics).toEqual({ ...created.style.basics, ...sender });
		expect(saved.style.metadata).toEqual(created.style.metadata);
		expect((await client.getById({ id: created.id })).style.basics).toMatchObject(sender);
		const document = await client.export({ id: created.id });
		expect(document.style.basics).toMatchObject(sender);
		expect((await client.import({ document })).style.basics).toMatchObject(sender);
		expect((await client.duplicate({ id: created.id })).style.basics).toMatchObject(sender);
		await expect(
			client.update({ id: created.id, expectedRevision: 2, sender: { name: 123 } } as never),
		).rejects.toMatchObject({ code: "BAD_REQUEST" });
		await expect(service.update({ userId: "bob", id: created.id, expectedRevision: 2, sender })).rejects.toMatchObject({
			code: "NOT_FOUND",
		});
		await expect(client.update({ id: created.id, expectedRevision: 1, sender })).rejects.toMatchObject({
			code: "CONFLICT",
		});

		const linked = await client.update({
			id: created.id,
			expectedRevision: 2,
			resumeId: "alice-resume",
			senderLinked: true,
		});
		expect(linked.style.basics).toEqual(defaultResumeData.basics);
		await expect(client.update({ id: created.id, expectedRevision: 3, sender })).rejects.toMatchObject({
			code: "BAD_REQUEST",
		});
		await expect(client.update({ id: created.id, expectedRevision: 3, resumeId: "bob-resume" })).rejects.toMatchObject({
			code: "NOT_FOUND",
		});
		const own = await client.update({
			id: created.id,
			expectedRevision: 3,
			senderLinked: false,
			sender: { email: sender.email },
		});
		expect(own.style.basics).toEqual({ ...defaultResumeData.basics, email: sender.email });
		expect((await getPool().query("SELECT data FROM resume WHERE id='alice-resume'")).rows[0].data).toEqual(
			defaultResumeData,
		);
		const copied = await client.duplicate({ id: created.id });
		expect(copied.style.basics).toEqual(own.style.basics);
		const detached = await client.update({ id: created.id, expectedRevision: 4, resumeId: null });
		expect(detached.style.basics).toEqual(own.style.basics);
		const relinked = await client.update({
			id: created.id,
			expectedRevision: 5,
			resumeId: "alice-resume",
			senderLinked: true,
		});
		expect(relinked.style.basics).toEqual(defaultResumeData.basics);
		const snapshot = await client.update({ id: created.id, expectedRevision: 6, resumeId: null });
		expect(snapshot.style.basics).toEqual(relinked.style.basics);
		await getPool().query("UPDATE cover_letter SET is_locked=true WHERE id=$1", [created.id]);
		await expect(client.update({ id: created.id, expectedRevision: 7, sender })).rejects.toMatchObject({
			code: "DOCUMENT_LOCKED",
		});
	});

	it("lets an unlinked letter edit or hide every sender detail its header prints, without touching the resume", async () => {
		const { coverLettersRouter } = await import("./router");
		const client = createRouterClient(coverLettersRouter, { context: { user: { id: "alice" } } as never });
		const source = structuredClone(defaultResumeData);
		source.basics.name = "David Kowalski";
		source.basics.headline = "Game Developer";
		source.basics.website = { url: "https://davidkowalski.games", label: "" };
		source.basics.customFields = [
			{ id: "github", icon: "github-logo", text: "github.com/dk", link: "https://github.com/dk" },
		];
		source.picture.url = "https://example.test/photo.png";
		await getPool().query("UPDATE resume SET data=$1 WHERE id='alice-resume'", [source]);
		const created = await client.create({ name: "Sender", resumeId: "alice-resume" });

		await expect(
			client.update({ id: created.id, expectedRevision: 1, sender: { headline: "Producer" } }),
		).rejects.toMatchObject({ code: "BAD_REQUEST" });

		const unlinked = await client.update({ id: created.id, expectedRevision: 1, senderLinked: false });
		expect(unlinked.style).toMatchObject({
			basics: source.basics,
			picture: { url: source.picture.url, hidden: false },
		});
		const own = await client.update({
			id: created.id,
			expectedRevision: 2,
			sender: { headline: "", website: { url: "", label: "" }, customFields: [], picture: { hidden: true } },
		});
		expect(own.style.basics).toEqual({
			...source.basics,
			headline: "",
			website: { url: "", label: "" },
			customFields: [],
		});
		expect(own.style.picture).toEqual({ ...source.picture, hidden: true });
		const page = createCoverLetterResumeData(await client.getById({ id: created.id }));
		expect(page.basics).toMatchObject({ headline: "", customFields: [] });
		expect(page.picture.hidden).toBe(true);
		expect((await client.export({ id: created.id })).style.picture.hidden).toBe(true);
		expect((await getPool().query("SELECT data FROM resume WHERE id='alice-resume'")).rows[0].data).toEqual(source);
		// Only the photo's visibility is the letter's to change; its address stays the copy from the resume.
		const shown = await client.update({
			id: created.id,
			expectedRevision: 3,
			sender: { picture: { hidden: false, url: "https://elsewhere.test/photo.png" } },
		} as never);
		expect(shown.style.picture).toEqual(source.picture);

		const relinked = await client.update({ id: created.id, expectedRevision: 4, senderLinked: true });
		expect(relinked.style).toMatchObject({ basics: source.basics, picture: source.picture });
	});

	it("keeps History: one version per session, named and sent versions, and restores with a way back", async () => {
		const created = await service.create({ userId: "alice", name: "History", content: "<p>One</p>" });
		const kinds = async () =>
			(await getPool().query("SELECT kind, name FROM cover_letter_version ORDER BY created_at, id")).rows;
		expect(await kinds()).toEqual([{ kind: "created", name: null }]);

		// Saves in one session share a version.
		const edit = { userId: "alice", id: created.id, sessionId: "session-a" };
		await service.update({ ...edit, expectedRevision: 1, content: "<p>Two</p>" });
		await service.update({ ...edit, expectedRevision: 2, content: "<p>Three</p>" });
		expect(await kinds()).toEqual([
			{ kind: "created", name: null },
			{ kind: "auto", name: null },
		]);

		const named = await service.createVersion({ userId: "alice", id: created.id, name: "Before the rewrite" });
		await service.update({ ...edit, expectedRevision: 3, content: "<p>Rewritten</p>", recipientName: "Dana" });
		await service.recordSent({ userId: "alice", id: created.id, company: "Lumen Health" });

		const restored = await service.restoreVersion({ userId: "alice", id: created.id, versionId: named.id });
		expect(restored).toMatchObject({ content: "<p>Three</p>", recipientName: "", revision: 5 });
		expect((await kinds()).map((row) => row.kind)).toEqual([
			"created",
			"auto",
			"named",
			"sent",
			"before-restore",
			"restored",
		]);
		const [beforeRestore] = (await getPool().query("SELECT data FROM cover_letter_version WHERE kind='before-restore'"))
			.rows;
		expect(beforeRestore.data).toMatchObject({ content: "<p>Rewritten</p>", recipientName: "Dana" });
		const [sent] = (await getPool().query("SELECT name, data FROM cover_letter_version WHERE kind='sent'")).rows;
		expect(sent).toMatchObject({ name: "Lumen Health", data: { content: "<p>Rewritten</p>" } });

		// Only named versions can be renamed or deleted, and only by their owner.
		const { deleteLetterVersion, renameLetterVersion } = await import("./versions");
		await expect(
			renameLetterVersion({ coverLetterId: created.id, userId: "bob", versionId: named.id, name: "Mine" }),
		).rejects.toMatchObject({ code: "NOT_FOUND" });
		await expect(service.restoreVersion({ userId: "bob", id: created.id, versionId: named.id })).rejects.toMatchObject({
			code: "NOT_FOUND",
		});
		await deleteLetterVersion({ coverLetterId: created.id, userId: "alice", versionId: named.id });
		const [created0] = (await getPool().query("SELECT id FROM cover_letter_version WHERE kind='created'")).rows;
		await expect(
			deleteLetterVersion({ coverLetterId: created.id, userId: "alice", versionId: created0.id }),
		).rejects.toMatchObject({ code: "NOT_FOUND" });
	});

	it("round-trips standalone exports without foreign provenance and sanitizes all writes", async () => {
		const created = await service.create({
			userId: "alice",
			name: "Original",
			resumeId: "alice-resume",
			content: '<p onclick="evil()">Safe<script>evil()</script></p>',
		});
		expect(created.content).toBe("<p>Safe</p>");
		const document = await service.export({ userId: "alice", id: created.id });
		const imported = await service.import({ userId: "bob", document });
		expect(imported.id).not.toBe(created.id);
		expect(imported).toMatchObject({ sourceResumeId: null, sourceApplicationId: null, content: "<p>Safe</p>" });
		const copy = await service.duplicate({ userId: "alice", id: created.id });
		await service.delete({ userId: "alice", id: created.id, expectedRevision: 1 });
		expect((await service.getById({ userId: "alice", id: copy.id })).content).toBe("<p>Safe</p>");
	});
});