Skip to content

Commit 2dde9e9

Browse files
committed
fix(studio): honor explicit file preconditions
1 parent 7a1eec8 commit 2dde9e9

3 files changed

Lines changed: 66 additions & 9 deletions

File tree

‎packages/studio/src/hooks/useDomEditCommits.test.tsx‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface RenderedDomEditCommits {
3434

3535
interface RenderDomEditCommitsOptions {
3636
importedFontAssets?: ImportedFontAsset[];
37-
writeProjectFile?: (path: string, content: string) => Promise<void>;
37+
writeProjectFile?: (path: string, content: string, expectedContent?: string) => Promise<void>;
3838
}
3939

4040
type FetchHandler = (
@@ -1050,6 +1050,46 @@ describe("useDomEditCommits style persist handling", () => {
10501050
}
10511051
});
10521052

1053+
it("uses the patched server content as the custom-font write precondition", async () => {
1054+
const patchedContent =
1055+
'<!doctype html><html><head></head><body><div data-hf-id="hf-card">Card</div></body></html>';
1056+
stubPatchFetch(
1057+
{ ok: true, changed: true, matched: true, content: patchedContent },
1058+
patchedContent,
1059+
);
1060+
const { iframe, element } = createPreviewElement();
1061+
const selection = createSelection(element, {
1062+
textFields: [textField({ key: "self", value: "Card", source: "self", tagName: "div" })],
1063+
});
1064+
const writeProjectFile = vi.fn(async () => {});
1065+
const rendered = renderDomEditCommits(selection, iframe, { writeProjectFile });
1066+
1067+
try {
1068+
await act(async () => {
1069+
await rendered.hook.commitDomTextFields(
1070+
selection,
1071+
[textField({ key: "self", value: "Card", source: "self", tagName: "div" })],
1072+
{
1073+
importedFont: {
1074+
family: "Imported",
1075+
path: "fonts/Imported.woff2",
1076+
url: "/api/projects/p1/preview/fonts/Imported.woff2",
1077+
},
1078+
},
1079+
);
1080+
});
1081+
1082+
expect(writeProjectFile).toHaveBeenCalledWith(
1083+
"index.html",
1084+
expect.stringContaining("@font-face"),
1085+
patchedContent,
1086+
);
1087+
expect(rendered.showToast).not.toHaveBeenCalled();
1088+
} finally {
1089+
rendered.cleanup();
1090+
}
1091+
});
1092+
10531093
it("keeps a rejected patch request (HTTP error) to one toast", async () => {
10541094
const { rendered, cleanup } = renderStyleCommitWithFetch(async (input) => {
10551095
const url = requestUrl(input);

‎packages/studio/src/utils/studioFileVersion.test.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,29 @@ describe("studioFileContentVersion", () => {
88
);
99
});
1010

11-
it("keeps a known-missing version distinct from known empty content", async () => {
12-
const versions = new Map<string, string | null>([["missing.html", null]]);
11+
it("keeps an explicit content precondition authoritative over cached state", async () => {
12+
const versions = new Map<string, string | null>([
13+
["stale.html", await studioFileContentVersion("stale")],
14+
["newer.html", await studioFileContentVersion("newer")],
15+
["missing.html", null],
16+
]);
17+
const expectedVersion = await studioFileContentVersion("expected");
1318

14-
expect(await studioExpectedFileVersion(versions, "missing.html", "")).toBeNull();
15-
expect(await studioExpectedFileVersion(versions, "empty.html", "")).toBe(
16-
await studioFileContentVersion(""),
19+
expect(await studioExpectedFileVersion(versions, "stale.html", "expected")).toBe(
20+
expectedVersion,
21+
);
22+
expect(await studioExpectedFileVersion(versions, "newer.html", "expected")).toBe(
23+
expectedVersion,
1724
);
25+
expect(await studioExpectedFileVersion(versions, "missing.html", "expected")).toBe(
26+
expectedVersion,
27+
);
28+
});
29+
30+
it("keeps known-missing and untracked files distinct without explicit content", async () => {
31+
const versions = new Map<string, string | null>([["missing.html", null]]);
32+
33+
expect(await studioExpectedFileVersion(versions, "missing.html")).toBeNull();
34+
expect(await studioExpectedFileVersion(versions, "untracked.html")).toBeUndefined();
1835
});
1936
});

‎packages/studio/src/utils/studioFileVersion.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ export async function studioFileContentVersion(content: string): Promise<string>
88
return `"sha256:${hex}"`;
99
}
1010

11-
/** Prefer the version observed during the read, including a known-missing file. */
11+
/** Prefer an explicit content precondition, then the version observed during the read. */
1212
export async function studioExpectedFileVersion(
1313
versions: ReadonlyMap<string, string | null>,
1414
path: string,
1515
expectedContent?: string,
1616
): Promise<string | null | undefined> {
17-
if (versions.has(path)) return versions.get(path);
18-
return expectedContent === undefined ? undefined : studioFileContentVersion(expectedContent);
17+
if (expectedContent !== undefined) return studioFileContentVersion(expectedContent);
18+
return versions.get(path);
1919
}
2020

2121
export function createStudioWriteToken(): string {

0 commit comments

Comments
 (0)