Files
archie/ui/test/chat-retry.test.js
T
sam dafbcb0f5c fix(webui): Send button no longer passes its click event to sendMessage
send.onclick = sendMessage bound the DOM click handler directly, so every
click invoked sendMessage(event) -- the MouseEvent became `retry`, which
is truthy but has no .turn, and retry?.turn.text threw immediately,
before any UI state changed. The click silently did nothing.

Extracted the retry/composer-text resolution into chat-retry.js's new
resolveTurn(), which only treats an argument as a genuine retry when it
actually carries a turn.text string -- a stray event (or any other
non-conforming truthy value) now falls back to the composer text
instead of crashing. Fixed the call site itself
(send.onclick = () => sendMessage()) as the actual root cause.

Fixes #447
2026-08-09 02:34:55 +10:00

45 lines
1.8 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { newChatTurn, retryChatTurn, resolveTurn } from "../src/chat/chat-retry.js";
test("retry preserves the logical turn identity and text", () => {
const turn = newChatTurn("keep this message");
const retry = retryChatTurn(turn);
assert.notEqual(turn.sourceID, undefined);
assert.deepEqual(retry, turn);
});
test("new turns receive distinct identities", () => {
const first = newChatTurn("same text");
const second = newChatTurn("same text");
assert.notEqual(first.sourceID, second.sourceID);
});
test("resolveTurn with no retry arg builds a fresh turn from composer text", () => {
const { text, turn, isRetry } = resolveTurn(null, " hello there ");
assert.equal(text, "hello there");
assert.equal(turn.text, "hello there");
assert.equal(isRetry, false);
});
test("resolveTurn with a real retry descriptor reuses its turn", () => {
const original = newChatTurn("retry me");
const { text, turn, isRetry } = resolveTurn({ turn: original, replyBubble: {} }, "");
assert.equal(text, "retry me");
assert.equal(turn.sourceID, original.sourceID);
assert.equal(isRetry, true);
});
test("resolveTurn does not throw when passed a DOM event instead of a retry descriptor", () => {
// send.onclick = sendMessage (not () => sendMessage()) hands the click's
// MouseEvent to sendMessage as `retry`. It's truthy and has no .turn, so
// this must be treated as "not a retry", not crash on retry.turn.text.
const clickEvent = { type: "click", isTrusted: true };
const { text, turn, isRetry } = resolveTurn(clickEvent, "typed while a stray event arrived");
assert.equal(text, "typed while a stray event arrived");
assert.equal(turn.text, text);
assert.equal(isRetry, false);
});