Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions crates/ironclaw_webui_v2_static/static/dist/app.js

Large diffs are not rendered by default.

73 changes: 66 additions & 7 deletions crates/ironclaw_webui_v2_static/static/js/pages/chat/chat-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,73 @@ export function ChatPage() {
const navigate = useNavigate();
const location = useLocation();
const composerDraft = location.state?.composerDraft || "";
const threads = threadsState.threads || [];
const activeThreadId = threadsState.activeThreadId;
const setActiveThreadId = threadsState.setActiveThreadId;
const threadsLoaded = Boolean(threadsState.isLoaded);
const threadsFetching = Boolean(threadsState.isFetching);
const threadListSettled = threadsLoaded && !threadsFetching;
const isReservedThreadRoute = urlThreadId === "newchat";
const isMalformedThreadRoute = Boolean(urlThreadId && urlThreadId.trim() === "");
const routeThreadSelectedLocally = Boolean(
urlThreadId &&
!isReservedThreadRoute &&
!isMalformedThreadRoute &&
activeThreadId === urlThreadId &&
!threadListSettled
);
const routeThreadExistsInList = Boolean(
urlThreadId &&
!isReservedThreadRoute &&
!isMalformedThreadRoute &&
threadsLoaded &&
threads.some((thread) => thread.id === urlThreadId)
);
const routeThreadCanOpen = routeThreadSelectedLocally || routeThreadExistsInList;
const routeActiveThreadId = routeThreadCanOpen ? urlThreadId : null;
Comment thread
flyagents marked this conversation as resolved.
Resolved

React.useEffect(() => {
if (urlThreadId && urlThreadId !== threadsState.activeThreadId) {
threadsState.setActiveThreadId(urlThreadId);
} else if (!urlThreadId) {
threadsState.setActiveThreadId(null);
if (!urlThreadId) {
if (activeThreadId !== null) {
setActiveThreadId(null);
}
return;
}

if (isReservedThreadRoute || isMalformedThreadRoute) {
if (activeThreadId !== null) {
setActiveThreadId(null);
}
navigate("/chat", { replace: true });
return;
}

if (!threadListSettled && !routeThreadCanOpen) {
return;
}

if (!routeThreadCanOpen) {
if (activeThreadId !== null) {
setActiveThreadId(null);
}
navigate("/chat", { replace: true });
return;
}

if (urlThreadId !== activeThreadId) {
setActiveThreadId(urlThreadId);
}
}, [urlThreadId]);
}, [
activeThreadId,
isMalformedThreadRoute,
isReservedThreadRoute,
navigate,
routeThreadCanOpen,
routeThreadSelectedLocally,
setActiveThreadId,
threadListSettled,
urlThreadId,
]);
Comment thread
flyagents marked this conversation as resolved.
Resolved

const handleSelectThread = React.useCallback(
(id, options = {}) => {
Expand All @@ -32,8 +91,8 @@ export function ChatPage() {

return html`
<${Chat}
threads=${threadsState.threads}
activeThreadId=${threadsState.activeThreadId}
threads=${threads}
activeThreadId=${routeActiveThreadId}
onSelectThread=${handleSelectThread}
isCreatingThread=${threadsState.isCreating}
composerDraft=${composerDraft}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import vm from "node:vm";

function chatPageSourceForTest() {
const source = readFileSync(new URL("./chat-page.js", import.meta.url), "utf8");
const lines = [];
let skippingImport = false;
for (const line of source.split("\n")) {
if (!skippingImport && line.startsWith("import ")) {
skippingImport = !line.trimEnd().endsWith(";");
continue;
}
if (skippingImport) {
skippingImport = !line.trimEnd().endsWith(";");
continue;
}
lines.push(line.replace("export function ChatPage", "function ChatPage"));
}
return `${lines.join("\n")}\nglobalThis.__testExports = { ChatPage };`;
}

function html(strings, ...values) {
return { strings: Array.from(strings), values };
}

function componentProps(node, component) {
const props = {};
const start = node.values.indexOf(component);
for (let index = start + 1; index < node.values.length; index += 1) {
const name = node.strings[index]?.match(/([A-Za-z][A-Za-z0-9]*)=\s*$/)?.[1];
if (name) props[name] = node.values[index];
}
return props;
}

function plain(value) {
return JSON.parse(JSON.stringify(value));
}

function renderChatPage({
activeThreadId = null,
isFetching = false,
threads = [{ id: "thread-1", title: "Existing chat" }],
isLoaded = true,
urlThreadId = null,
} = {}) {
const navigateCalls = [];
const setActiveThreadIdCalls = [];
const threadsState = {
activeThreadId,
createThread: async () => null,
deleteThread: async () => {},
isCreating: false,
isFetching,
isLoaded,
setActiveThreadId: (id) => {
threadsState.activeThreadId = id;
setActiveThreadIdCalls.push(id);
},
threads,
};
const context = {
Chat() {},
React: {
useCallback: (fn) => fn,
useEffect: (fn) => fn(),
},
globalThis: {},
html,
useLocation: () => ({ key: "location-key", state: null }),
useNavigate: () => (path, options) => {
navigateCalls.push([path, options]);
},
useOutletContext: () => ({
gatewayStatus: { ok: true },
threadsState,
}),
useParams: () => (urlThreadId ? { threadId: urlThreadId } : {}),
};

vm.runInNewContext(chatPageSourceForTest(), context);
const tree = context.globalThis.__testExports.ChatPage();
return {
chatProps: componentProps(tree, context.Chat),
navigateCalls,
setActiveThreadIdCalls,
};
}

test("ChatPage redirects the reserved newchat route to blank chat", () => {
const result = renderChatPage({
activeThreadId: "thread-1",
urlThreadId: "newchat",
});

assert.deepEqual(plain(result.navigateCalls), [["/chat", { replace: true }]]);
assert.deepEqual(result.setActiveThreadIdCalls, [null]);
assert.equal(result.chatProps.activeThreadId, null);
});

test("ChatPage redirects a loaded but missing thread route to blank chat", () => {
const result = renderChatPage({
activeThreadId: "thread-1",
threads: [{ id: "thread-1", title: "Existing chat" }],
urlThreadId: "missing-thread",
});

assert.deepEqual(plain(result.navigateCalls), [["/chat", { replace: true }]]);
assert.deepEqual(result.setActiveThreadIdCalls, [null]);
assert.equal(result.chatProps.activeThreadId, null);
});

test("ChatPage redirects a stale active thread when the loaded thread list is settled", () => {
const result = renderChatPage({
activeThreadId: "missing-thread",
threads: [{ id: "thread-1", title: "Existing chat" }],
urlThreadId: "missing-thread",
});

assert.deepEqual(plain(result.navigateCalls), [["/chat", { replace: true }]]);
assert.deepEqual(result.setActiveThreadIdCalls, [null]);
assert.equal(result.chatProps.activeThreadId, null);
});

test("ChatPage keeps a valid loaded thread route active", () => {
const result = renderChatPage({
activeThreadId: null,
threads: [{ id: "thread-1", title: "Existing chat" }],
urlThreadId: "thread-1",
});

assert.deepEqual(result.navigateCalls, []);
assert.deepEqual(result.setActiveThreadIdCalls, ["thread-1"]);
assert.equal(result.chatProps.activeThreadId, "thread-1");
});

test("ChatPage keeps a locally selected route active while the thread list catches up", () => {
const result = renderChatPage({
activeThreadId: "thread-new",
isFetching: true,
threads: [{ id: "thread-1", title: "Existing chat" }],
urlThreadId: "thread-new",
});

assert.deepEqual(result.navigateCalls, []);
assert.deepEqual(result.setActiveThreadIdCalls, []);
assert.equal(result.chatProps.activeThreadId, "thread-new");
});

test("ChatPage waits for the thread list before redirecting a deep link", () => {
const result = renderChatPage({
activeThreadId: null,
isLoaded: false,
threads: [],
urlThreadId: "thread-1",
});

assert.deepEqual(result.navigateCalls, []);
assert.deepEqual(result.setActiveThreadIdCalls, []);
assert.equal(result.chatProps.activeThreadId, "thread-1");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Expected value is inverted for the “wait for thread list” case.

Line 162 currently asserts "thread-1", but this scenario (isLoaded: false, no local selection) should keep activeThreadId unset until the list settles. This assertion contradicts the route-sync contract and the test intent.

Proposed fix
-  assert.equal(result.chatProps.activeThreadId, "thread-1");
+  assert.equal(result.chatProps.activeThreadId, null);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert.equal(result.chatProps.activeThreadId, "thread-1");
assert.equal(result.chatProps.activeThreadId, null);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/ironclaw_webui_v2_static/static/js/pages/chat/chat-page.test.mjs` at
line 162, In the test case for the "wait for thread list" scenario in
chat-page.test.mjs, the assertion on line 162 for
result.chatProps.activeThreadId currently expects "thread-1", but this
contradicts the test scenario where isLoaded is false and no local selection
exists. Update the assertion to expect an unset value (such as undefined, null,
or empty string depending on the implementation) instead of "thread-1", as
activeThreadId should remain unset until the thread list finishes loading and
resolves the route-sync contract.

});
Comment thread
flyagents marked this conversation as resolved.
Resolved

test("ChatPage keeps bare chat route in blank chat state", () => {
const result = renderChatPage({
activeThreadId: "thread-1",
urlThreadId: null,
});

assert.deepEqual(result.navigateCalls, []);
assert.deepEqual(result.setActiveThreadIdCalls, [null]);
assert.equal(result.chatProps.activeThreadId, null);
});
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export function useThreads() {
activeThreadId,
setActiveThreadId,
isLoading: query.isLoading,
isLoaded: query.isSuccess,
isFetching: query.isFetching,
isCreating,
createThread: handleCreateThread,
deleteThread: handleDeleteThread,
Expand Down
Loading