Skip to content
TypeScript

fix(web): local visit migration preserves mark unread (#25746)

Cementb5ced4383357
diff --git a/apps/web/src/hooks/useThreadVisitedMigration.test.ts b/apps/web/src/hooks/useThreadVisitedMigration.test.ts
new file mode 100644
index 00000000000..e0bd84855cc
--- /dev/null
+++ b/apps/web/src/hooks/useThreadVisitedMigration.test.ts
@@ -0,0 +1,103 @@
+import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
+import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
+import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
+import { EnvironmentId } from "@t3tools/contracts";
+import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
+import { describe, expect, it, vi } from "vite-plus/test";
+
+import { environmentThreadShells } from "../state/threads";
+import { makeThreadFixture } from "../test-fixtures";
+import { startThreadVisitedMigration } from "./useThreadVisitedMigration";
+
+const state = vi.hoisted(() => ({
+ localVisits: {} as Record<string, string>,
+}));
+
+vi.mock("@t3tools/client-runtime/state/runtime", () => ({ runAtomCommand: vi.fn() }));
+vi.mock("../uiStateStore", () => ({
+ useUiStateStore: { getState: () => ({ threadLastVisitedAtById: state.localVisits }) },
+}));
+vi.mock("../state/threads", async () => {
+ const { Atom } = await import("effect/reactivity");
+ return {
+ environmentThreadShells: {
+ threadShellsAtom: Atom.make<ReadonlyArray<EnvironmentThreadShell>>([]),
+ },
+ threadEnvironment: { visit: {} },
+ };
+});
+
+const localVisit = "2026-10-27T10:00:00.000Z";
+
+function setup(threads: ReadonlyArray<EnvironmentThreadShell>, local = localVisit) {
+ state.localVisits = Object.fromEntries(
+ threads.map((thread) => [
+ scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
+ local,
+ ]),
+ );
+ vi.mocked(runAtomCommand).mockReset().mockResolvedValue(AsyncResult.success(undefined));
+ const tasks: Array<() => void> = [];
+ const registry = AtomRegistry.make({
+ scheduleTask: (task) => {
+ tasks.push(task);
+ return () => {};
+ },
+ });
+ const shells = environmentThreadShells.threadShellsAtom;
+ if (!Atom.isWritable(shells)) throw new Error("Expected writable test shell state");
+ registry.set(shells, threads);
+ const stop = startThreadVisitedMigration(registry);
+ return {
+ setThreads(next: ReadonlyArray<EnvironmentThreadShell>) {
+ registry.set(shells, next);
+ for (let task = tasks.shift(); task !== undefined; task = tasks.shift()) task();
+ },
+ [Symbol.dispose]() {
+ stop();
+ registry.dispose();
+ },
+ };
+}
+
+describe("startThreadVisitedMigration", () => {
+ it("preserves mark unread when a client with a newer local visit reloads", () => {
+ using _migration = setup([makeThreadFixture({ lastVisitedAt: "2026-10-27T09:59:59.999Z" })]);
+
+ expect(runAtomCommand).not.toHaveBeenCalled();
+ });
+
+ it("seeds unset visits once per environment and thread", () => {
+ const thread = makeThreadFixture({ lastVisitedAt: null });
+ const remote = { ...thread, environmentId: EnvironmentId.make("remote") };
+ using migration = setup([thread, remote]);
+
+ expect(vi.mocked(runAtomCommand).mock.calls.map((call) => call[2])).toEqual([
+ {
+ environmentId: thread.environmentId,
+ input: { threadId: thread.id, visitedAt: localVisit },
+ },
+ {
+ environmentId: remote.environmentId,
+ input: { threadId: remote.id, visitedAt: localVisit },
+ },
+ ]);
+ migration.setThreads([{ ...thread }, { ...remote }]);
+ expect(runAtomCommand).toHaveBeenCalledTimes(2);
+ });
+
+ it("waits for older servers to support visits before migrating", () => {
+ const thread = makeThreadFixture();
+ expect(thread.lastVisitedAt).toBeUndefined();
+ using migration = setup([thread]);
+ expect(runAtomCommand).not.toHaveBeenCalled();
+
+ migration.setThreads([{ ...thread, lastVisitedAt: null }]);
+ expect(runAtomCommand).toHaveBeenCalledTimes(1);
+ });
+
+ it.each(["", "not-a-date"])("ignores invalid local visits: %s", (local) => {
+ using _migration = setup([makeThreadFixture({ lastVisitedAt: null })], local);
+ expect(runAtomCommand).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/src/hooks/useThreadVisitedMigration.ts b/apps/web/src/hooks/useThreadVisitedMigration.ts
index 2d01d800360..fb90b6d4f2e 100644
--- a/apps/web/src/hooks/useThreadVisitedMigration.ts
+++ b/apps/web/src/hooks/useThreadVisitedMigration.ts
@@ -15,8 +15,8 @@ export function startThreadVisitedMigration(registry: AtomRegistry.AtomRegistry)
const migratedThreadKeys = new Set<string>();
const migrate = (threads: ReadonlyArray<EnvironmentThreadShell>) => {
for (const thread of threads) {
- // Missing means unsupported; retain the local value until the server upgrades.
- if (thread.lastVisitedAt === undefined) continue;
+ // An established server watermark may be deliberately older after Mark unread.
+ if (thread.lastVisitedAt !== null) continue;
const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
if (migratedThreadKeys.has(threadKey)) continue;
migratedThreadKeys.add(threadKey);
@@ -24,8 +24,6 @@ export function startThreadVisitedMigration(registry: AtomRegistry.AtomRegistry)
if (!local) continue;
const localMs = Date.parse(local);
if (!Number.isFinite(localMs)) continue;
- const serverMs = thread.lastVisitedAt === null ? null : Date.parse(thread.lastVisitedAt);
- if (serverMs !== null && Number.isFinite(serverMs) && serverMs >= localMs) continue;
void runAtomCommand(
registry,
threadEnvironment.visit,