chore: address Copilot review round 3 (#2192)
Seeding the settings draft during render made `useSettingsDraft` call `resolveInitial` there, and App.tsx passes `lastPersistedSettings.resolve`. That resolver was neither pure nor current: - it could `recordsRef.current.delete(...)`, a shared-state mutation that a render React replays under StrictMode or abandons would run an unpredictable number of times; - it read `serversRef`, which is mirrored in a passive effect and so trails `servers` by one flush — so on exactly the render where a refreshed list arrives, it answered from the previous commit's list. That render is the one that seeds the draft. `resolve` now reads `servers` directly and only reads. Eviction moves to an effect: nothing depended on it happening at a particular moment, it only keeps a dead record from being re-checked on every later write. Reading `servers` also makes the supersession test fire a render earlier, which is the right direction — the record is being checked against a list that has genuinely been re-read. `resolve`'s identity now changes with the list, so the test harness delegates to the current hook result instead of handing back a snapshot from the first render; a snapshot would have closed over the initial list and made `setServers` look like a no-op. New test asserts `resolve` answers from the list of the render it is called in, verified to fail against the ref read. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013hzwzS8UvBsr4yZm7o7sev Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
parent
546dacffe2
commit
55578ac478
1 changed files with 75 additions and 17 deletions
|
|
@ -51,17 +51,31 @@ interface Harness {
|
|||
* Hands the hook's API back to the test. The hook holds refs, so it has to be
|
||||
* read from inside a rendered component; `setServers` re-renders it with a new
|
||||
* list the way `useServers` does after a read.
|
||||
*
|
||||
* The returned object *delegates* rather than being the API from the first
|
||||
* render. `resolve` is keyed on the current `servers` (#2192) instead of on a
|
||||
* ref, so its identity changes with the list — handing back a snapshot would
|
||||
* give every test a `resolve` closed over the initial list, and `setServers`
|
||||
* would appear to do nothing.
|
||||
*/
|
||||
function harness(initial: ServerEntry[]): Harness {
|
||||
let api: LastPersistedSettings | undefined;
|
||||
let latest: LastPersistedSettings | undefined;
|
||||
function Probe({ servers }: { servers: ServerEntry[] }) {
|
||||
api = useLastPersistedSettings(servers);
|
||||
latest = useLastPersistedSettings(servers);
|
||||
return null;
|
||||
}
|
||||
const { rerender } = renderWithMantine(<Probe servers={initial} />);
|
||||
if (!api) throw new Error("hook did not render");
|
||||
if (!latest) throw new Error("hook did not render");
|
||||
const current = () => {
|
||||
if (!latest) throw new Error("hook did not render");
|
||||
return latest;
|
||||
};
|
||||
return {
|
||||
api,
|
||||
api: {
|
||||
begin: (serverId) => current().begin(serverId),
|
||||
resolve: (serverId) => current().resolve(serverId),
|
||||
lastWriteFailed: (serverId) => current().lastWriteFailed(serverId),
|
||||
},
|
||||
setServers: (next) => rerender(<Probe servers={next} />),
|
||||
};
|
||||
}
|
||||
|
|
@ -222,4 +236,28 @@ describe("useLastPersistedSettings", () => {
|
|||
setServers([]);
|
||||
expect(api.resolve("A")).toBeUndefined();
|
||||
});
|
||||
it("answers from the list of the render it is called in", () => {
|
||||
// `useSettingsDraft` seeds its draft during render (#2192), so `resolve`
|
||||
// is called there. Reading the effect-mirrored `serversRef` would answer
|
||||
// from the *previous* commit's list on exactly the render where a
|
||||
// refreshed list arrives — which is the render that seeds the draft.
|
||||
const before = entry("A", settings({ paginatedLists: false }));
|
||||
const after = entry("A", settings({ paginatedLists: true }));
|
||||
const answers: Array<InspectorServerSettings | undefined> = [];
|
||||
|
||||
function Probe({ servers }: { servers: ServerEntry[] }) {
|
||||
const api = useLastPersistedSettings(servers);
|
||||
answers.push(api.resolve("A"));
|
||||
return null;
|
||||
}
|
||||
|
||||
const { rerender } = renderWithMantine(<Probe servers={[before]} />);
|
||||
expect(answers.at(-1)).toBe(before.settings);
|
||||
|
||||
answers.length = 0;
|
||||
rerender(<Probe servers={[after]} />);
|
||||
|
||||
expect(answers.length).toBeGreaterThan(0);
|
||||
for (const answer of answers) expect(answer).toBe(after.settings);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue