Skip to content

Commit 38f126f

Browse files
committed
test(web): retain file close save coverage
1 parent e63c0de commit 38f126f

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

‎apps/web/src/components/files/fileSaveCoordinator.test.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,98 @@ describe("FileSaveCoordinator", () => {
7373
expect(onPendingChange.mock.calls.at(-1)).toEqual([false]);
7474
});
7575

76+
it("saves an edit made inside the debounce window when the editor closes", async () => {
77+
vi.useFakeTimers();
78+
const persist = vi
79+
.fn<(contents: string) => Promise<AtomCommandResult<void, never>>>()
80+
.mockResolvedValue(AsyncResult.success(undefined));
81+
const coordinator = new FileSaveCoordinator({
82+
debounceMs: 500,
83+
persist,
84+
onPendingChange: vi.fn(),
85+
onConfirmed: vi.fn(),
86+
});
87+
88+
coordinator.change("unsaved");
89+
coordinator.dispose();
90+
await vi.runAllTimersAsync();
91+
92+
expect(persist).toHaveBeenCalledOnce();
93+
expect(persist).toHaveBeenCalledWith("unsaved");
94+
});
95+
96+
it("flushes an edit made while a write was in flight when the editor closes", async () => {
97+
vi.useFakeTimers();
98+
const inFlight = deferred();
99+
const persist = vi
100+
.fn<(contents: string) => Promise<AtomCommandResult<void, never>>>()
101+
.mockReturnValueOnce(inFlight.promise)
102+
.mockResolvedValue(AsyncResult.success(undefined));
103+
const coordinator = new FileSaveCoordinator({
104+
debounceMs: 500,
105+
persist,
106+
onPendingChange: vi.fn(),
107+
onConfirmed: vi.fn(),
108+
});
109+
110+
coordinator.change("first");
111+
await vi.advanceTimersByTimeAsync(500);
112+
coordinator.change("latest");
113+
coordinator.dispose();
114+
inFlight.resolve(AsyncResult.success(undefined));
115+
await vi.runAllTimersAsync();
116+
117+
expect(persist).toHaveBeenCalledTimes(2);
118+
expect(persist).toHaveBeenLastCalledWith("latest");
119+
});
120+
121+
it("does not rewrite a write that lands while the editor closes", async () => {
122+
vi.useFakeTimers();
123+
const inFlight = deferred();
124+
const persist = vi
125+
.fn<(contents: string) => Promise<AtomCommandResult<void, never>>>()
126+
.mockReturnValueOnce(inFlight.promise)
127+
.mockResolvedValue(AsyncResult.success(undefined));
128+
const coordinator = new FileSaveCoordinator({
129+
debounceMs: 500,
130+
persist,
131+
onPendingChange: vi.fn(),
132+
onConfirmed: vi.fn(),
133+
});
134+
135+
coordinator.change("only");
136+
await vi.advanceTimersByTimeAsync(500);
137+
coordinator.dispose();
138+
inFlight.resolve(AsyncResult.success(undefined));
139+
await vi.runAllTimersAsync();
140+
141+
expect(persist).toHaveBeenCalledOnce();
142+
});
143+
144+
it("retries a failed write when the editor closes", async () => {
145+
vi.useFakeTimers();
146+
const persist = vi
147+
.fn()
148+
.mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("write failed"))))
149+
.mockResolvedValue(AsyncResult.success(undefined));
150+
const coordinator = new FileSaveCoordinator({
151+
debounceMs: 500,
152+
persist,
153+
onPendingChange: vi.fn(),
154+
onConfirmed: vi.fn(),
155+
});
156+
157+
coordinator.change("latest");
158+
await vi.advanceTimersByTimeAsync(500);
159+
expect(persist).toHaveBeenCalledOnce();
160+
161+
coordinator.dispose();
162+
await vi.runAllTimersAsync();
163+
164+
expect(persist).toHaveBeenCalledTimes(2);
165+
expect(persist).toHaveBeenLastCalledWith("latest");
166+
});
167+
76168
it("leaves the file pending when the latest write fails", async () => {
77169
vi.useFakeTimers();
78170
const onPendingChange = vi.fn();

0 commit comments

Comments
 (0)