Skip to content

Commit b5ced43

Browse files
authored
fix(web): local visit migration preserves mark unread (#25746)
1 parent 213b391 commit b5ced43

2 files changed

Lines changed: 105 additions & 4 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
2+
import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
3+
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4+
import { EnvironmentId } from "@t3tools/contracts";
5+
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
6+
import { describe, expect, it, vi } from "vite-plus/test";
7+
8+
import { environmentThreadShells } from "../state/threads";
9+
import { makeThreadFixture } from "../test-fixtures";
10+
import { startThreadVisitedMigration } from "./useThreadVisitedMigration";
11+
12+
const state = vi.hoisted(() => ({
13+
localVisits: {} as Record<string, string>,
14+
}));
15+
16+
vi.mock("@t3tools/client-runtime/state/runtime", () => ({ runAtomCommand: vi.fn() }));
17+
vi.mock("../uiStateStore", () => ({
18+
useUiStateStore: { getState: () => ({ threadLastVisitedAtById: state.localVisits }) },
19+
}));
20+
vi.mock("../state/threads", async () => {
21+
const { Atom } = await import("effect/reactivity");
22+
return {
23+
environmentThreadShells: {
24+
threadShellsAtom: Atom.make<ReadonlyArray<EnvironmentThreadShell>>([]),
25+
},
26+
threadEnvironment: { visit: {} },
27+
};
28+
});
29+
30+
const localVisit = "2026-10-27T10:00:00.000Z";
31+
32+
function setup(threads: ReadonlyArray<EnvironmentThreadShell>, local = localVisit) {
33+
state.localVisits = Object.fromEntries(
34+
threads.map((thread) => [
35+
scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
36+
local,
37+
]),
38+
);
39+
vi.mocked(runAtomCommand).mockReset().mockResolvedValue(AsyncResult.success(undefined));
40+
const tasks: Array<() => void> = [];
41+
const registry = AtomRegistry.make({
42+
scheduleTask: (task) => {
43+
tasks.push(task);
44+
return () => {};
45+
},
46+
});
47+
const shells = environmentThreadShells.threadShellsAtom;
48+
if (!Atom.isWritable(shells)) throw new Error("Expected writable test shell state");
49+
registry.set(shells, threads);
50+
const stop = startThreadVisitedMigration(registry);
51+
return {
52+
setThreads(next: ReadonlyArray<EnvironmentThreadShell>) {
53+
registry.set(shells, next);
54+
for (let task = tasks.shift(); task !== undefined; task = tasks.shift()) task();
55+
},
56+
[Symbol.dispose]() {
57+
stop();
58+
registry.dispose();
59+
},
60+
};
61+
}
62+
63+
describe("startThreadVisitedMigration", () => {
64+
it("preserves mark unread when a client with a newer local visit reloads", () => {
65+
using _migration = setup([makeThreadFixture({ lastVisitedAt: "2026-10-27T09:59:59.999Z" })]);
66+
67+
expect(runAtomCommand).not.toHaveBeenCalled();
68+
});
69+
70+
it("seeds unset visits once per environment and thread", () => {
71+
const thread = makeThreadFixture({ lastVisitedAt: null });
72+
const remote = { ...thread, environmentId: EnvironmentId.make("remote") };
73+
using migration = setup([thread, remote]);
74+
75+
expect(vi.mocked(runAtomCommand).mock.calls.map((call) => call[2])).toEqual([
76+
{
77+
environmentId: thread.environmentId,
78+
input: { threadId: thread.id, visitedAt: localVisit },
79+
},
80+
{
81+
environmentId: remote.environmentId,
82+
input: { threadId: remote.id, visitedAt: localVisit },
83+
},
84+
]);
85+
migration.setThreads([{ ...thread }, { ...remote }]);
86+
expect(runAtomCommand).toHaveBeenCalledTimes(2);
87+
});
88+
89+
it("waits for older servers to support visits before migrating", () => {
90+
const thread = makeThreadFixture();
91+
expect(thread.lastVisitedAt).toBeUndefined();
92+
using migration = setup([thread]);
93+
expect(runAtomCommand).not.toHaveBeenCalled();
94+
95+
migration.setThreads([{ ...thread, lastVisitedAt: null }]);
96+
expect(runAtomCommand).toHaveBeenCalledTimes(1);
97+
});
98+
99+
it.each(["", "not-a-date"])("ignores invalid local visits: %s", (local) => {
100+
using _migration = setup([makeThreadFixture({ lastVisitedAt: null })], local);
101+
expect(runAtomCommand).not.toHaveBeenCalled();
102+
});
103+
});

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ export function startThreadVisitedMigration(registry: AtomRegistry.AtomRegistry)
1515
const migratedThreadKeys = new Set<string>();
1616
const migrate = (threads: ReadonlyArray<EnvironmentThreadShell>) => {
1717
for (const thread of threads) {
18-
// Missing means unsupported; retain the local value until the server upgrades.
19-
if (thread.lastVisitedAt === undefined) continue;
18+
// An established server watermark may be deliberately older after Mark unread.
19+
if (thread.lastVisitedAt !== null) continue;
2020
const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
2121
if (migratedThreadKeys.has(threadKey)) continue;
2222
migratedThreadKeys.add(threadKey);
2323
const local = useUiStateStore.getState().threadLastVisitedAtById[threadKey];
2424
if (!local) continue;
2525
const localMs = Date.parse(local);
2626
if (!Number.isFinite(localMs)) continue;
27-
const serverMs = thread.lastVisitedAt === null ? null : Date.parse(thread.lastVisitedAt);
28-
if (serverMs !== null && Number.isFinite(serverMs) && serverMs >= localMs) continue;
2927
void runAtomCommand(
3028
registry,
3129
threadEnvironment.visit,

0 commit comments

Comments
 (0)