Skip to content

Commit 29a5c91

Browse files
test(web): consolidate model preference migration coverage
1 parent 0595715 commit 29a5c91

2 files changed

Lines changed: 133 additions & 149 deletions

File tree

‎apps/web/src/hooks/useSettings.favorites.browser.test.tsx‎

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {
66
type ServerSettings,
77
type ServerSettingsPatch,
88
} from "@t3tools/contracts";
9+
import {
10+
AVAILABLE_CONNECTION_STATE,
11+
type SupervisorConnectionState,
12+
} from "@t3tools/client-runtime/connection";
913
import * as Cause from "effect/Cause";
1014
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
1115
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
@@ -15,9 +19,31 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
1519

1620
const state = vi.hoisted(() => ({
1721
settings: new Map<string, unknown>(),
22+
connections: new Map<
23+
EnvironmentId,
24+
Atom.Writable<AsyncResult.AsyncResult<SupervisorConnectionState>>
25+
>(),
1826
persist: vi.fn(),
1927
}));
2028

29+
vi.mock(import("~/connection/catalog"), async (original) => {
30+
const actual = await original();
31+
return {
32+
...actual,
33+
environmentCatalog: {
34+
...actual.environmentCatalog,
35+
stateAtom: ((id: EnvironmentId) => state.connections.get(id)) as never,
36+
},
37+
};
38+
});
39+
vi.mock("~/localApi", () => ({
40+
ensureLocalApi: () => ({
41+
persistence: {
42+
getClientSettings: async () => ({ favorites: [{ provider, model }] }),
43+
},
44+
}),
45+
}));
46+
2147
vi.mock(import("~/state/server"), async (original) => {
2248
const actual = await original();
2349
return {
@@ -33,7 +59,7 @@ vi.mock(import("~/state/use-atom-command"), async (original) => ({
3359
useAtomCommand: (() => state.persist) as never,
3460
}));
3561

36-
import { useToggleEnvironmentModelFavorite } from "./useSettings";
62+
import { useEnvironmentModelPreferences, useToggleEnvironmentModelFavorite } from "./useSettings";
3763

3864
const primaryId = EnvironmentId.make("primary");
3965
const remoteId = EnvironmentId.make("remote");
@@ -85,6 +111,7 @@ beforeEach(() => {
85111
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
86112
registry = AtomRegistry.make();
87113
state.settings.clear();
114+
state.connections.clear();
88115
durable = new Map();
89116
toggles = [];
90117
writes = [];
@@ -260,3 +287,108 @@ describe("environment favorite toggles", () => {
260287
expect(durable.get(primaryId)!.favorites).toEqual([]);
261288
});
262289
});
290+
291+
function ModelPreferencesReader({ environmentId }: { environmentId: EnvironmentId }) {
292+
useEnvironmentModelPreferences(environmentId);
293+
return null;
294+
}
295+
296+
describe("legacy model preferences migration", () => {
297+
it.each(["succeeds", "fails"] as const)(
298+
"waits for connection and retries only after reconnect when the first save %s",
299+
async (outcome) => {
300+
const environmentId = EnvironmentId.make(`migration-${outcome}`);
301+
const otherId = EnvironmentId.make(`other-${outcome}`);
302+
for (const id of [environmentId, otherId]) {
303+
state.settings.set(
304+
id,
305+
Atom.keepAlive(Atom.make<ServerSettings | null>(DEFAULT_SERVER_SETTINGS)),
306+
);
307+
state.connections.set(
308+
id,
309+
Atom.keepAlive(
310+
Atom.make<AsyncResult.AsyncResult<SupervisorConnectionState>>(
311+
AsyncResult.success(AVAILABLE_CONNECTION_STATE),
312+
),
313+
),
314+
);
315+
}
316+
let finishFirstSave = () => {};
317+
const firstSave = new Promise((resolve) => {
318+
finishFirstSave = () =>
319+
resolve(
320+
outcome === "succeeds"
321+
? AsyncResult.success(DEFAULT_SERVER_SETTINGS)
322+
: AsyncResult.failure(Cause.fail(new Error("save failed"))),
323+
);
324+
});
325+
state.persist
326+
.mockReturnValueOnce(firstSave)
327+
.mockResolvedValue(AsyncResult.success(DEFAULT_SERVER_SETTINGS));
328+
const renderReader = (id = environmentId) =>
329+
act(() =>
330+
root.render(
331+
<RegistryContext.Provider value={registry}>
332+
<ModelPreferencesReader key={id} environmentId={id} />
333+
</RegistryContext.Provider>,
334+
),
335+
);
336+
const connect = (id: EnvironmentId, generation: number) =>
337+
act(() =>
338+
registry.set(
339+
state.connections.get(id)!,
340+
AsyncResult.success({
341+
...AVAILABLE_CONNECTION_STATE,
342+
desired: true,
343+
generation,
344+
phase: "connected",
345+
}),
346+
),
347+
);
348+
const pushSettings = (id = environmentId) =>
349+
act(() => registry.set(settingsAtom(id), { ...DEFAULT_SERVER_SETTINGS }));
350+
const migration = (id: EnvironmentId) => ({
351+
environmentId: id,
352+
input: { patch: { migrateModelPreferences: { favorites: [{ provider, model }] } } },
353+
});
354+
355+
await renderReader();
356+
await pushSettings();
357+
expect(state.persist).not.toHaveBeenCalled();
358+
359+
await connect(environmentId, 1);
360+
expect(state.persist).toHaveBeenCalledExactlyOnceWith(migration(environmentId));
361+
await pushSettings();
362+
expect(state.persist).toHaveBeenCalledTimes(1);
363+
await act(finishFirstSave);
364+
await pushSettings();
365+
await pushSettings();
366+
expect(state.persist).toHaveBeenCalledTimes(1);
367+
368+
await connect(otherId, 7);
369+
await pushSettings(otherId);
370+
expect(state.persist).toHaveBeenCalledTimes(1);
371+
await renderReader(otherId);
372+
expect(state.persist).toHaveBeenLastCalledWith(migration(otherId));
373+
await renderReader();
374+
await pushSettings();
375+
expect(state.persist).toHaveBeenCalledTimes(2);
376+
377+
await act(() =>
378+
registry.set(
379+
state.connections.get(environmentId)!,
380+
AsyncResult.success({ ...AVAILABLE_CONNECTION_STATE, generation: 1 }),
381+
),
382+
);
383+
await pushSettings();
384+
expect(state.persist).toHaveBeenCalledTimes(2);
385+
await connect(environmentId, 2);
386+
await pushSettings();
387+
expect(state.persist.mock.calls).toEqual([
388+
[migration(environmentId)],
389+
[migration(otherId)],
390+
[migration(environmentId)],
391+
]);
392+
},
393+
);
394+
});

‎apps/web/src/hooks/useSettings.migration.browser.test.tsx‎

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)