Skip to content

Commit a6fb68c

Browse files
committed
refactor(web,mobile): name the migration gate by what it stores
Shorten the gate comment to the server constraint it guards, and name the test fixture for the old server it stands for.
1 parent 545027c commit a6fb68c

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

‎apps/mobile/src/features/threads/ThreadSettingsSheet.tsx‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ const EMPTY_MODEL_FAVORITES: ReadonlyArray<{
139139
readonly model: string;
140140
}> = [];
141141
const FAVORITES_PROVIDER_FILTER = "@favorites";
142-
// Migrate once per connection: a server that predates `migrateModelPreferences` drops it and keeps
143-
// reporting null, so retrying on each settings push would save to that server in a loop. A
144-
// reconnect, for example after that server is upgraded, tries again.
145-
const attemptedModelPreferencesMigrations = new Map<EnvironmentId, number>();
142+
// Servers that predate `migrateModelPreferences` drop it and keep reporting null favorites.
143+
const attemptedMigrationGenerations = new Map<EnvironmentId, number>();
146144
/** Provider catalog header with its harness logo and disclosure state. */
147145
function ProviderHeader(props: {
148146
readonly driver: string | undefined;
@@ -404,12 +402,12 @@ function ThreadSettingsSessionProvider(
404402
!props.environmentId ||
405403
!settings ||
406404
!AsyncResult.isSuccess(preferences) ||
407-
attemptedModelPreferencesMigrations.get(props.environmentId) === connectionGeneration
405+
attemptedMigrationGenerations.get(props.environmentId) === connectionGeneration
408406
)
409407
return;
410408
const patch = modelPreferencesMigrationPatch(settings, { favorites: legacyFavorites });
411409
if (!patch) return;
412-
attemptedModelPreferencesMigrations.set(props.environmentId, connectionGeneration);
410+
attemptedMigrationGenerations.set(props.environmentId, connectionGeneration);
413411
void saveSettings({ environmentId: props.environmentId, input: { patch } });
414412
}, [
415413
connectionGeneration,

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { act } from "react";
1414
import { create, type ReactTestRenderer } from "react-test-renderer";
1515
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
1616

17+
const settingsFromServerWithoutMigration = DEFAULT_SERVER_SETTINGS;
18+
1719
const state = vi.hoisted(() => ({
1820
settings: undefined as unknown as Atom.Writable<ServerSettings | null>,
1921
connection: undefined as unknown as Atom.Writable<
@@ -57,12 +59,13 @@ let renderer: ReactTestRenderer | undefined;
5759
beforeEach(() => {
5860
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
5961
registry = AtomRegistry.make();
60-
// A server that predates the migration never reports favorites.
61-
state.settings = Atom.make<ServerSettings | null>(DEFAULT_SERVER_SETTINGS);
62+
state.settings = Atom.make<ServerSettings | null>(settingsFromServerWithoutMigration);
6263
state.connection = Atom.make<AsyncResult.AsyncResult<SupervisorConnectionState>>(
6364
AsyncResult.success(connected(1)),
6465
);
65-
state.persist.mockReset().mockResolvedValue(AsyncResult.success(DEFAULT_SERVER_SETTINGS));
66+
state.persist
67+
.mockReset()
68+
.mockResolvedValue(AsyncResult.success(settingsFromServerWithoutMigration));
6669
});
6770

6871
function connected(generation: number): SupervisorConnectionState {
@@ -81,7 +84,7 @@ async function renderModelPicker(environmentId: EnvironmentId) {
8184

8285
async function pushSettings(count: number) {
8386
for (let push = 0; push < count; push += 1) {
84-
await act(async () => registry.set(state.settings, { ...DEFAULT_SERVER_SETTINGS }));
87+
await act(async () => registry.set(state.settings, { ...settingsFromServerWithoutMigration }));
8588
}
8689
}
8790

@@ -92,7 +95,6 @@ afterEach(async () => {
9295
});
9396

9497
it("migrates once when the server drops the migration and keeps pushing settings", async () => {
95-
// Attempts are module state, so each test uses its own environment.
9698
const environmentId = EnvironmentId.make("remote");
9799
await renderModelPicker(environmentId);
98100
await pushSettings(3);

‎apps/web/src/hooks/useSettings.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,8 @@ export function mergeEnvironmentSettings(
331331
};
332332
}
333333

334-
// Migrate once per connection: a server that predates `migrateModelPreferences` drops it and keeps
335-
// reporting null, so retrying on each settings push would save to that server in a loop. A
336-
// reconnect, for example after that server is upgraded, tries again.
337-
const attemptedModelPreferencesMigrations = new Map<EnvironmentId, number>();
334+
// Servers that predate `migrateModelPreferences` drop it and keep reporting null favorites.
335+
const attemptedMigrationGenerations = new Map<EnvironmentId, number>();
338336
const connectionGenerationAtom = Atom.family((environmentId: EnvironmentId | null) =>
339337
Atom.make((get) =>
340338
environmentId === null
@@ -360,12 +358,12 @@ export function useMigrateModelPreferences(
360358
!environmentId ||
361359
!settings ||
362360
!hydrated ||
363-
attemptedModelPreferencesMigrations.get(environmentId) === connectionGeneration
361+
attemptedMigrationGenerations.get(environmentId) === connectionGeneration
364362
)
365363
return;
366364
const patch = modelPreferencesMigrationPatch(settings, clientSettings);
367365
if (!patch) return;
368-
attemptedModelPreferencesMigrations.set(environmentId, connectionGeneration);
366+
attemptedMigrationGenerations.set(environmentId, connectionGeneration);
369367
void migrate({ environmentId, input: { patch } });
370368
}, [clientSettings, connectionGeneration, environmentId, hydrated, migrate, settings]);
371369
}

0 commit comments

Comments
 (0)