Skip to content

Commit 86521b9

Browse files
fix(studio): an edit the history could not record says why in the log (#5489)
1 parent 2905162 commit 86521b9

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎packages/studio/src/hooks/usePersistentEditHistory.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,9 @@ it("a claim the server refused is logged, and the write stays undoable as a chan
561561
files: { "index.html": { before: "A", after: "B" } },
562562
}),
563563
);
564-
expect(logged).toHaveBeenCalledWith('"Moved Title" was not recorded as your edit: disk full');
564+
expect(logged).toHaveBeenCalledWith(
565+
'[history] 500: "Moved Title" was not recorded as your edit: disk full',
566+
);
565567
restoreFetch();
566568
expect(await act(() => hook().undo({ readFile }))).toMatchObject({
567569
ok: true,
@@ -570,6 +572,46 @@ it("a claim the server refused is logged, and the write stays undoable as a chan
570572
expect(file()).toBe("A");
571573
});
572574

575+
it.each([
576+
[
577+
"the server refused",
578+
409,
579+
() => answer("/history/claim", 409, { error: "newer edit" }),
580+
"newer edit",
581+
],
582+
[
583+
"could not reach the server",
584+
0,
585+
() => {
586+
const real = globalThis.fetch;
587+
vi.stubGlobal("fetch", (url: string, init?: RequestInit) =>
588+
url.endsWith("/history/claim")
589+
? Promise.reject(new TypeError("fetch failed"))
590+
: real(url, init),
591+
);
592+
},
593+
"Studio could not reach its server.",
594+
],
595+
] as const)(
596+
"a claim that %s logs under [history] with its status",
597+
async (_, status, stub, reason) => {
598+
const { hook, save } = await studio();
599+
const logged = vi.spyOn(console, "error").mockImplementation(() => {});
600+
cleanup.push(() => logged.mockRestore());
601+
stub();
602+
save("B");
603+
await act(() =>
604+
hook().recordEdit({
605+
label: "Moved Title",
606+
files: { "index.html": { before: "A", after: "B" } },
607+
}),
608+
);
609+
expect(logged).toHaveBeenCalledWith(
610+
`[history] ${status}: "Moved Title" was not recorded as your edit: ${reason}`,
611+
);
612+
},
613+
);
614+
573615
it("a step whose reply cannot be read says so, instead of throwing", async () => {
574616
const { hook, readFile } = await studio();
575617
answer("/history/step", 200, "<html>");

‎packages/studio/src/hooks/usePersistentEditHistory.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ function claimHeld(reply: Awaited<ReturnType<typeof post>>, label: string): stri
154154
if (reply.ok)
155155
return (reply.body as { claimed: { id: string } | null } | null)?.claimed?.id ?? null;
156156
if (reply.status !== 404)
157-
console.error(`"${label}" was not recorded as your edit: ${reply.error}`);
157+
console.error(
158+
`[history] ${reply.status}: "${label}" was not recorded as your edit: ${reply.error}`,
159+
);
158160
return null;
159161
}
160162

0 commit comments

Comments
 (0)