import { afterEach, describe, expect, it, vi } from "vitest";
import { createMemoryAdapter, openComposition } from "@hyperframes/sdk";
import {
listStudioProjectFiles,
persistSdkCandidateMutation,
type CutoverDeps,
} from "./sdkEditTransaction";
afterEach(() => {
vi.unstubAllGlobals();
});
const LEAF_HTML = `
`;
const INDEX_MOUNTING_LEAF = ``;
describe("persistSdkCandidateMutation live cross-file delete guard", () => {
// fails without the fix: candidate transaction inherited stale open-time graph and committed the destructive delete.
it("refuses deleting a wrapper when a sibling file adds a reference after the session opened (listProjectFiles)", async () => {
const files = new Map([["compositions/leaf.html", LEAF_HTML]]);
const live = await openComposition(LEAF_HTML, { history: false });
const nested = live.getElements().find((el) => el.attributes.id === "nested");
expect(nested).toBeDefined();
files.set("index.html", INDEX_MOUNTING_LEAF);
const writeProjectFile = vi.fn(async (path: string, content: string) => {
files.set(path, content);
});
const deps: CutoverDeps = {
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
writeProjectFile,
reloadPreview: vi.fn(),
readProjectFile: async (path) => {
const value = files.get(path);
if (value === undefined) throw new Error(`missing ${path}`);
return value;
},
listProjectFiles: async () => [...files.keys()],
publishSession: vi.fn(() => "published" as const),
};
const result = await persistSdkCandidateMutation(
live,
"compositions/leaf.html",
LEAF_HTML,
deps,
(candidate) => candidate.removeElement(nested!.id),
);
expect(result.status).toBe("declined");
expect(writeProjectFile).not.toHaveBeenCalled();
expect(files.get("compositions/leaf.html")).toContain('id="child-template"');
live.dispose();
});
// fails without the fix: candidate transaction cloned live.externalRefs instead of querying live's PersistAdapter.
it("refuses deleting a wrapper when a sibling adds a reference on the live session's PersistAdapter", async () => {
const persist = createMemoryAdapter();
await persist.write("compositions/leaf.html", LEAF_HTML);
const live = await openComposition(LEAF_HTML, {
history: false,
persist,
persistPath: "compositions/leaf.html",
});
const nested = live.getElements().find((el) => el.attributes.id === "nested");
expect(nested).toBeDefined();
await persist.write("index.html", INDEX_MOUNTING_LEAF);
const writeProjectFile = vi.fn().mockResolvedValue(undefined);
const deps: CutoverDeps = {
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
writeProjectFile,
reloadPreview: vi.fn(),
readProjectFile: async () => (await persist.read("compositions/leaf.html")) ?? LEAF_HTML,
publishSession: vi.fn(() => "published" as const),
};
const result = await persistSdkCandidateMutation(
live,
"compositions/leaf.html",
LEAF_HTML,
deps,
(candidate) => candidate.removeElement(nested!.id),
);
expect(result.status).toBe("declined");
expect(writeProjectFile).not.toHaveBeenCalled();
live.dispose();
});
// fails without the fix: HTTP 503 on the project manifest became an empty file list and committed the delete.
it("refuses deleting a wrapper when the Studio project manifest returns 503", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response("Service Unavailable", { status: 503 })),
);
const live = await openComposition(LEAF_HTML, { history: false });
const nested = live.getElements().find((el) => el.attributes.id === "nested");
expect(nested).toBeDefined();
const writeProjectFile = vi.fn().mockResolvedValue(undefined);
const recordEdit = vi.fn().mockResolvedValue(undefined);
const publishSession = vi.fn(() => "published" as const);
const deps: CutoverDeps = {
editHistory: { recordEdit },
writeProjectFile,
reloadPreview: vi.fn(),
readProjectFile: async () => LEAF_HTML,
listProjectFiles: () => listStudioProjectFiles("demo"),
publishSession,
};
const result = await persistSdkCandidateMutation(
live,
"compositions/leaf.html",
LEAF_HTML,
deps,
(candidate) => candidate.removeElement(nested!.id),
);
expect(result.status).toBe("failed");
expect(writeProjectFile).not.toHaveBeenCalled();
expect(recordEdit).not.toHaveBeenCalled();
expect(publishSession).not.toHaveBeenCalled();
live.dispose();
});
});