Skip to content

Commit d38ea20

Browse files
committed
fix(mobile): distinguish provisional thread labels from confirmed links
1 parent b79df9f commit d38ea20

3 files changed

Lines changed: 93 additions & 8 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { EnvironmentId, ProjectId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
2+
import * as Cause from "effect/Cause";
3+
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
4+
import { expect, it, vi } from "vite-plus/test";
5+
6+
const state = vi.hoisted(() => ({ reply: null as unknown }));
7+
vi.mock(import("./threads"), async () => {
8+
const { Atom } = await import("effect/reactivity");
9+
return {
10+
environmentThreadDetails: {} as never,
11+
environmentThreadShells: {
12+
threadShellAtom: () => Atom.make(null),
13+
threadRefsAtom: Atom.make([{ environmentId: "remote", threadId: "older-thread" }]),
14+
} as never,
15+
};
16+
});
17+
vi.mock(import("./orchestration"), () => ({
18+
orchestrationEnvironment: { threadLinkMetadata: () => state.reply } as never,
19+
}));
20+
vi.mock(import("./atom-registry"), () => ({ appAtomRegistry: {} as never }));
21+
vi.mock(import("./projects"), () => ({ environmentProjects: {} as never }));
22+
vi.mock(import("./server"), () => ({
23+
environmentServerConfigsAtom: {} as never,
24+
serverEnvironment: {} as never,
25+
}));
26+
vi.mock(import("./assets"), () => ({ projectFaviconUrlAtom: (() => null) as never }));
27+
vi.mock(import("../native/ProjectChipIconImages"), () => ({
28+
projectGlyphImageAtom: (() => null) as never,
29+
projectGlyphKey: (() => "") as never,
30+
}));
31+
32+
import { resolveThreadLink } from "./entities";
33+
34+
function setup() {
35+
const reply = Atom.make<
36+
AsyncResult.AsyncResult<Record<string, { title: string; projectId: ProjectId | null }>, string>
37+
>(AsyncResult.initial());
38+
state.reply = reply;
39+
return { registry: AtomRegistry.make(), reply };
40+
}
41+
42+
it("uses confirmed matches instead of provisional titles when resolving legacy links", () => {
43+
const { registry, reply } = setup();
44+
using _registry = { [Symbol.dispose]: () => registry.dispose() };
45+
const local: ScopedThreadRef = {
46+
environmentId: EnvironmentId.make("local"),
47+
threadId: ThreadId.make("older-thread"),
48+
};
49+
const remote: ScopedThreadRef = { ...local, environmentId: EnvironmentId.make("remote") };
50+
const link = { environmentId: null, threadId: local.threadId };
51+
const resolve = () => resolveThreadLink(registry.get.bind(registry), link, local.environmentId);
52+
const release = registry.mount(reply);
53+
try {
54+
expect(resolve()).toEqual(remote);
55+
registry.set(reply, AsyncResult.failure(Cause.fail("offline")));
56+
expect(resolve()).toEqual(remote);
57+
expect(resolveThreadLink(registry.get.bind(registry), local, remote.environmentId)).toEqual(
58+
local,
59+
);
60+
registry.set(
61+
reply,
62+
AsyncResult.success({
63+
[local.threadId]: { title: "Thread", projectId: ProjectId.make("project") },
64+
}),
65+
);
66+
expect(resolve()).toEqual(local);
67+
registry.set(reply, AsyncResult.success({}));
68+
expect(resolve()).toEqual(remote);
69+
} finally {
70+
release();
71+
}
72+
});

‎apps/mobile/src/state/entities.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,20 @@ export function resolveThreadLink(
244244
link: ThreadLink,
245245
messageEnvironmentId: EnvironmentId,
246246
): ScopedThreadRef | null {
247-
const known = threadLinkCandidates(link, messageEnvironmentId).find(
248-
(ref) => read(threadTitleAtom(threadKey(ref))) !== null,
249-
);
247+
const known = threadLinkCandidates(link, messageEnvironmentId).find((ref) => {
248+
if (read(environmentThreadShells.threadShellAtom(ref)) !== null) return true;
249+
const metadata = read(
250+
orchestrationEnvironment.threadLinkMetadata({
251+
environmentId: ref.environmentId,
252+
input: { threadIds: [ref.threadId] },
253+
}),
254+
);
255+
return AsyncResult.getOrElse(metadata, () => null)?.[ref.threadId] !== undefined;
256+
});
250257
if (known) return known;
251258
return link.environmentId === null
252259
? guessThreadLinkRef(link, read(environmentThreadShells.threadRefsAtom))
253-
: null;
260+
: scopeThreadRef(link.environmentId, link.threadId);
254261
}
255262

256263
export function useThreadChipIcons(

‎apps/web/src/components/chat/MarkdownThreadLink.browser.test.tsx‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ it.each([
9696
await expect.element(link).toBeVisible();
9797
await expect.element(screen.getByRole("img", { name: "Essays project icon" })).toBeVisible();
9898
await link.click();
99-
expect(screen.router?.state.location.pathname).toBe(
100-
`/environment/${encodeURIComponent(threadId)}`,
99+
await vi.waitFor(() =>
100+
expect(screen.router?.state.location.pathname).toBe(
101+
`/environment/${encodeURIComponent(threadId)}`,
102+
),
101103
);
102104
if (encoded) {
103105
await act(() =>
@@ -111,14 +113,18 @@ it.each([
111113
const decoded = screen.getByRole("link", { name: "Thread, Decoded essay" });
112114
await expect.element(decoded).toBeVisible();
113115
await decoded.click();
114-
expect(screen.router?.state.location.pathname).toBe("/environment/older-thread");
116+
await vi.waitFor(() =>
117+
expect(screen.router?.state.location.pathname).toBe("/environment/older-thread"),
118+
);
115119
}
116120
await act(() => registry.set(reply, AsyncResult.success({})));
117121
if (legacy) {
118122
const remote = screen.getByRole("link", { name: "Thread, Remote thread" });
119123
await expect.element(remote).toBeVisible();
120124
await remote.click();
121-
expect(screen.router?.state.location.pathname).toBe("/remote/older-thread");
125+
await vi.waitFor(() =>
126+
expect(screen.router?.state.location.pathname).toBe("/remote/older-thread"),
127+
);
122128
} else {
123129
await expect
124130
.element(screen.getByText("Thread no longer available", { exact: true }))

0 commit comments

Comments
 (0)