Files
archie/ui/test/chat-composer-completion.test.ts
T
archie 09c2c55762
ci/woodpecker/pr/quality Pipeline was successful
Quality / check (pull_request) Successful in 4m18s
Chat composer: '#' context dropdown for issues (#1861) (archie)
2026-11-12 14:54:01 +11:00

267 lines
6.7 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
applyCommand,
applyCompletion,
commandCompletionSource,
completionsFor,
detectTrigger,
isExactCommand,
issueCompletionSource,
matchesFor,
MAX_MATCHES,
moveSelection,
resolveCompletionKey,
type ChatIssue,
type CompletionSource,
} from "../src/chat/commands.ts";
import type { ChatCommandSpec } from "../src/chat/state.ts";
const sampleCommands: ChatCommandSpec[] = [
{ command: "/help", description: "List available commands" },
{ command: "/model", description: "Switch active model" },
{ command: "/new", description: "Start a fresh conversation" },
];
const sampleIssues: ChatIssue[] = [
{
owner: "sam",
repository: "archie",
number: 1861,
title: "Chat composer: '#' context dropdown for issues",
labels: ["ui", "chat"],
},
{
owner: "sam",
repository: "archie",
number: 1842,
title: "Retry stalled task runs from the board",
labels: ["tasks"],
},
{
owner: "acme",
repository: "ops",
number: 99,
title: "Rotate webhook signing secret",
},
];
test("detectTrigger matches trigger characters at start of input or after whitespace", () => {
assert.deepEqual(detectTrigger("#", 1), {
trigger: "#",
query: "",
tokenStart: 0,
tokenEnd: 1,
});
assert.deepEqual(detectTrigger("#1861", 5), {
trigger: "#",
query: "1861",
tokenStart: 0,
tokenEnd: 5,
});
assert.deepEqual(detectTrigger("Implement #186", 14), {
trigger: "#",
query: "186",
tokenStart: 10,
tokenEnd: 14,
});
assert.deepEqual(detectTrigger("First line\n#dropdown", 20), {
trigger: "#",
query: "dropdown",
tokenStart: 11,
tokenEnd: 20,
});
assert.deepEqual(detectTrigger("Fix #18 in composer", 7), {
trigger: "#",
query: "18",
tokenStart: 4,
tokenEnd: 7,
});
assert.deepEqual(detectTrigger("/mod", 4), {
trigger: "/",
query: "mod",
tokenStart: 0,
tokenEnd: 4,
});
// Mid-word '#' or closed tokens followed by whitespace do not trigger.
assert.equal(detectTrigger("issue#1861", 10), null);
assert.equal(detectTrigger("#1861 ", 6), null);
assert.equal(detectTrigger("", 0), null);
// Pluggable custom triggers work with the same detector.
assert.deepEqual(detectTrigger("assign @arc", 11, ["/", "#", "@"]), {
trigger: "@",
query: "arc",
tokenStart: 7,
tokenEnd: 11,
});
});
test("completionsFor and matchesFor filter open issues and slash commands", () => {
const allIssues = matchesFor(sampleCommands, "Check #", 7, sampleIssues);
assert.deepEqual(
allIssues.map((item) => [item.label, item.detail, item.insertText]),
[
["#1861", "Chat composer: '#' context dropdown for issues", "#1861"],
["#1842", "Retry stalled task runs from the board", "#1842"],
["#99", "Rotate webhook signing secret", "#99"],
],
);
const byNumber = matchesFor(sampleCommands, "See #186", 8, sampleIssues);
assert.deepEqual(
byNumber.map((item) => item.label),
["#1861"],
);
const byTitle = matchesFor(
sampleCommands,
"Investigate #DROPDOWN",
21,
sampleIssues,
);
assert.deepEqual(
byTitle.map((item) => item.label),
["#1861"],
);
const byRepo = matchesFor(sampleCommands, "In #acme/ops", 12, sampleIssues);
assert.deepEqual(
byRepo.map((item) => item.label),
["#99"],
);
const commands = matchesFor(sampleCommands, "/mo", 3, sampleIssues);
assert.deepEqual(
commands.map((item) => item.label),
["/model"],
);
const manyIssues: ChatIssue[] = Array.from({ length: 12 }, (_, index) => ({
number: index + 1,
title: `Issue ${index + 1}`,
}));
const capped = matchesFor(sampleCommands, "#", 1, manyIssues);
assert.equal(capped.length, MAX_MATCHES);
// Pluggable completion source alongside '/' and '#'
const workflowSource: CompletionSource = {
trigger: "@",
label: "Workflows",
items: [
{
key: "workflow:triage",
trigger: "@",
label: "@triage",
detail: "Triage incoming issues",
insertText: "@triage",
searchText: "@triage triage incoming issues",
command: "@triage",
},
],
};
const custom = completionsFor(
[
commandCompletionSource(sampleCommands),
issueCompletionSource(sampleIssues),
workflowSource,
],
"Run @tri",
8,
);
assert.equal(custom.trigger, "@");
assert.equal(custom.label, "Workflows");
assert.deepEqual(
custom.items.map((item) => item.insertText),
["@triage"],
);
});
test("applyCompletion replaces the active trigger token and places the caret after the trailing space", () => {
assert.deepEqual(applyCompletion("#18", 3, "#1861"), {
text: "#1861 ",
cursor: 6,
});
assert.deepEqual(applyCompletion("Implement #186 now", 14, "#1861"), {
text: "Implement #1861 now",
cursor: 16,
});
assert.deepEqual(applyCompletion("Line one\n#drop", 14, "#1861"), {
text: "Line one\n#1861 ",
cursor: 15,
});
assert.deepEqual(applyCommand("/he", 3, "/help"), {
text: "/help ",
cursor: 6,
});
});
test("keyboard navigation wraps selection, accepts with Enter or Tab, and dismisses with Escape", () => {
assert.equal(moveSelection(0, 1, 3), 1);
assert.equal(moveSelection(2, 1, 3), 0);
assert.equal(moveSelection(0, -1, 3), 2);
assert.equal(moveSelection(0, 1, 0), 0);
const baseInput = {
menuOpen: true,
matchCount: 3,
selection: 1,
activeTrigger: "#" as const,
composerText: "Implement #18",
commandSpecs: sampleCommands,
};
assert.deepEqual(
resolveCompletionKey({ ...baseInput, key: "ArrowDown" }),
{ type: "move", delta: 1, selection: 2 },
);
assert.deepEqual(
resolveCompletionKey({ ...baseInput, selection: 0, key: "ArrowUp" }),
{ type: "move", delta: -1, selection: 2 },
);
assert.deepEqual(
resolveCompletionKey({ ...baseInput, key: "Enter" }),
{ type: "select", index: 1 },
);
assert.deepEqual(
resolveCompletionKey({ ...baseInput, key: "Tab" }),
{ type: "select", index: 1 },
);
assert.deepEqual(
resolveCompletionKey({ ...baseInput, key: "Escape" }),
{ type: "dismiss" },
);
// Shift+Enter or IME composition should not select an item.
assert.equal(
resolveCompletionKey({ ...baseInput, key: "Enter", shiftKey: true }),
null,
);
assert.equal(
resolveCompletionKey({ ...baseInput, key: "Enter", isComposing: true }),
null,
);
// Exact slash command submits on Enter instead of re-completing.
assert.equal(isExactCommand(sampleCommands, "/help"), true);
assert.deepEqual(
resolveCompletionKey({
...baseInput,
activeTrigger: "/",
composerText: "/help",
key: "Enter",
}),
{ type: "submit" },
);
});