@akanjs/code 0.0.0 → 3.3.0-rc.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/README.ko.md +31 -0
  2. package/README.md +26 -4
  3. package/agent/AkanCodeCompaction.ts +49 -0
  4. package/agent/AkanCodeServices.ts +122 -0
  5. package/agent/AkanContextFiles.ts +94 -0
  6. package/agent/CodeAgent.ts +598 -0
  7. package/agent/CodeAgentAllowed.ts +35 -0
  8. package/agent/CodeAgentAsks.ts +66 -0
  9. package/agent/CodeAgentCommandLine.ts +372 -0
  10. package/agent/CodeAgentEventMapper.ts +225 -0
  11. package/agent/CodeAgentFrames.ts +57 -0
  12. package/agent/CodeAgentGate.ts +258 -0
  13. package/agent/CodeAgentProxy.ts +83 -0
  14. package/agent/CodeAgentRpcHost.ts +109 -0
  15. package/agent/CodeAgentRpcListener.ts +195 -0
  16. package/agent/CodeAgentStreamPrinter.ts +86 -0
  17. package/agent/CodeAgentSuspended.ts +85 -0
  18. package/agent/CodeAgentUi.ts +47 -0
  19. package/agent/CodeMailbox.ts +148 -0
  20. package/agent/CodeSessionFork.ts +56 -0
  21. package/agent/CodeSessionIndex.ts +78 -0
  22. package/agent/SubagentPool.ts +222 -0
  23. package/agent/WorkspaceTrust.ts +200 -0
  24. package/agent/akanCodeCompaction.test.ts +36 -0
  25. package/agent/akanCodeModel.ts +47 -0
  26. package/agent/akanCodeServices.test.ts +54 -0
  27. package/agent/akanContextFiles.test.ts +116 -0
  28. package/agent/akanSystemPrompt.ts +50 -0
  29. package/agent/codeAgent.test.ts +337 -0
  30. package/agent/codeAgentCommandLine.test.ts +212 -0
  31. package/agent/codeAgentContract.test.ts +757 -0
  32. package/agent/codeAgentHosts.ts +30 -0
  33. package/agent/codeAgentProxy.test.ts +125 -0
  34. package/agent/codeAgentRpcListener.test.ts +215 -0
  35. package/agent/codeAgentSuspended.test.ts +47 -0
  36. package/agent/codeMailbox.test.ts +76 -0
  37. package/agent/codeSessionFork.test.ts +58 -0
  38. package/agent/codeSessionIndex.test.ts +77 -0
  39. package/agent/consoleToStderr.ts +8 -0
  40. package/agent/jsonLines.ts +29 -0
  41. package/agent/stringArg.ts +5 -0
  42. package/bunfig.toml +2 -0
  43. package/feedback/DevLogFeedback.ts +82 -0
  44. package/feedback/PreviewFeedback.ts +46 -0
  45. package/feedback/TurnFeedback.ts +98 -0
  46. package/feedback/VerifyFeedback.ts +39 -0
  47. package/host/AkanCodeRunner.ts +297 -0
  48. package/host/CodeTrustPrompt.ts +57 -0
  49. package/host/codeRunnerPrompt.test.ts +181 -0
  50. package/host/hostGraph.test.ts +33 -0
  51. package/host/index.ts +2 -0
  52. package/index.ts +49 -0
  53. package/package.json +43 -9
  54. package/piDependencyPins.test.ts +45 -0
  55. package/test/preload.ts +3 -0
  56. package/tools/AkanCli.ts +56 -0
  57. package/tools/AkanCodePlugins.ts +145 -0
  58. package/tools/AkanEditScope.ts +53 -0
  59. package/tools/AkanEnvKeys.ts +50 -0
  60. package/tools/AkanToolPack.ts +87 -0
  61. package/tools/AkanVerifier.ts +30 -0
  62. package/tools/AskToolPack.ts +99 -0
  63. package/tools/DeepSeekWeb.ts +127 -0
  64. package/tools/MailToolPack.ts +76 -0
  65. package/tools/McpClient.ts +209 -0
  66. package/tools/McpOAuth.ts +233 -0
  67. package/tools/McpOAuthLoopback.ts +64 -0
  68. package/tools/McpServerConfig.ts +131 -0
  69. package/tools/McpSignIn.ts +158 -0
  70. package/tools/McpTokenStore.ts +128 -0
  71. package/tools/McpToolPack.ts +206 -0
  72. package/tools/SessionToolPack.ts +91 -0
  73. package/tools/WebToolPack.ts +315 -0
  74. package/tools/akanCodePlugins.test.ts +954 -0
  75. package/tools/askToolPack.test.ts +116 -0
  76. package/tools/deepSeekWeb.test.ts +113 -0
  77. package/tools/mcpOAuth.test.ts +648 -0
  78. package/tools/mcpServerConfig.test.ts +341 -0
  79. package/tsconfig.json +29 -0
  80. package/tui/CodeTui.tsx +394 -0
  81. package/tui/CodeTuiAgents.ts +65 -0
  82. package/tui/CodeTuiApp.test.tsx +251 -0
  83. package/tui/CodeTuiApp.tsx +312 -0
  84. package/tui/CodeTuiAutoCompact.ts +33 -0
  85. package/tui/CodeTuiClipboard.ts +94 -0
  86. package/tui/CodeTuiCommandRunner.ts +242 -0
  87. package/tui/CodeTuiCommands.ts +59 -0
  88. package/tui/CodeTuiEditor.ts +168 -0
  89. package/tui/CodeTuiFiles.ts +45 -0
  90. package/tui/CodeTuiFrame.ts +133 -0
  91. package/tui/CodeTuiLines.ts +117 -0
  92. package/tui/CodeTuiMarkdown.ts +159 -0
  93. package/tui/CodeTuiMcp.ts +111 -0
  94. package/tui/CodeTuiModels.ts +82 -0
  95. package/tui/CodeTuiMouse.ts +39 -0
  96. package/tui/CodeTuiOverlay.ts +148 -0
  97. package/tui/CodeTuiParts.ts +168 -0
  98. package/tui/CodeTuiPrompt.ts +271 -0
  99. package/tui/CodeTuiQuestion.ts +170 -0
  100. package/tui/codeTranscript.test.ts +445 -0
  101. package/tui/codeTui.test.tsx +1104 -0
  102. package/tui/codeTuiAgents.test.ts +61 -0
  103. package/tui/codeTuiClipboard.test.ts +16 -0
  104. package/tui/codeTuiCommands.test.ts +38 -0
  105. package/tui/codeTuiEditor.test.ts +122 -0
  106. package/tui/codeTuiFiles.test.ts +43 -0
  107. package/tui/codeTuiMarkdown.test.ts +91 -0
  108. package/tui/codeTuiMcp.test.ts +160 -0
  109. package/tui/codeTuiMouse.test.ts +61 -0
@@ -0,0 +1,61 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { CodeAgentSubagent } from "akanjs/common";
3
+ import { CodeTuiAgents } from "./CodeTuiAgents";
4
+ import { CodeTuiLines } from "./CodeTuiLines";
5
+
6
+ const agent = (patch: Partial<CodeAgentSubagent> = {}): CodeAgentSubagent => ({
7
+ id: "c0",
8
+ kind: "explore",
9
+ description: "Counting untranslated desc strings in store.tsx",
10
+ startedAt: 0,
11
+ tokens: 301_300,
12
+ ...patch,
13
+ });
14
+
15
+ const now = 750_000;
16
+
17
+ describe("sub-agent rail", () => {
18
+ test("nothing is drawn when nothing is running", () => {
19
+ expect(CodeTuiAgents.rows([], "main", 80)).toEqual([]);
20
+ });
21
+
22
+ test("the session heads the rail and every child gets one row", () => {
23
+ const rows = CodeTuiAgents.rows([agent(), agent({ id: "c1", kind: "code" })], "main", 100, now);
24
+ expect(rows.map((row) => CodeTuiLines.text(row))).toEqual([
25
+ "❯ ⏺ main",
26
+ " ◯ explore Counting untranslated desc strings in store.tsx 12m 30s · ↓ 301.3k tokens",
27
+ " ◯ code Counting untranslated desc strings in store.tsx 12m 30s · ↓ 301.3k tokens",
28
+ ]);
29
+ for (const row of rows.slice(1)) expect(CodeTuiLines.width(CodeTuiLines.text(row))).toBe(100);
30
+ });
31
+
32
+ test("a row is exactly one line wide, in columns, whatever the description is", () => {
33
+ for (const width of [40, 61, 80, 120]) {
34
+ const rows = CodeTuiAgents.rows(
35
+ [agent({ description: "한글로 쓴 아주 긴 설명이 여기에 들어간다" })],
36
+ "s",
37
+ width,
38
+ now,
39
+ );
40
+ for (const row of rows) expect(CodeTuiLines.width(CodeTuiLines.text(row))).toBeLessThanOrEqual(width);
41
+ }
42
+ });
43
+
44
+ test("the meta column is dropped rather than squeezed when there is no room for a description", () => {
45
+ const [, row] = CodeTuiAgents.rows([agent()], "s", 30, now);
46
+ expect(CodeTuiLines.text(row ?? { key: "", spans: [] })).not.toContain("tokens");
47
+ });
48
+
49
+ test("elapsed reads in the unit a person reads at that scale", () => {
50
+ expect(CodeTuiAgents.elapsed(47_000)).toBe("47s");
51
+ expect(CodeTuiAgents.elapsed(750_000)).toBe("12m 30s");
52
+ expect(CodeTuiAgents.elapsed(3_720_000)).toBe("1h 2m");
53
+ expect(CodeTuiAgents.elapsed(-5)).toBe("0s");
54
+ });
55
+
56
+ test("tokens carry one decimal past a thousand", () => {
57
+ expect(CodeTuiAgents.tokens(842)).toBe("842");
58
+ expect(CodeTuiAgents.tokens(12_400)).toBe("12.4k");
59
+ expect(CodeTuiAgents.tokens(1_300_000)).toBe("1.3M");
60
+ });
61
+ });
@@ -0,0 +1,16 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { CodeTuiClipboard } from "./CodeTuiClipboard";
3
+
4
+ describe("CodeTuiClipboard", () => {
5
+ test("reads the PNG out of AppleScript's hex literal", () => {
6
+ const bytes = CodeTuiClipboard.fromAppleScript("«data PNGf89504E470D0A1A0A»");
7
+ expect(bytes && [...bytes]).toEqual([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
8
+ });
9
+
10
+ test("text on the clipboard answers with nothing rather than half an image", () => {
11
+ expect(CodeTuiClipboard.fromAppleScript('"just some text"')).toBeUndefined();
12
+ expect(CodeTuiClipboard.fromAppleScript(undefined)).toBeUndefined();
13
+ // An odd digit count is a truncated read; half a byte written to a file is a corrupt PNG.
14
+ expect(CodeTuiClipboard.fromAppleScript("«data PNGf89504E470D0A1A0»")).toBeUndefined();
15
+ });
16
+ });
@@ -0,0 +1,38 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import path from "node:path";
3
+ import { akanCodePaths } from "@akanjs/devkit/codeAgent";
4
+ import { CodeTuiCommands } from "./CodeTuiCommands";
5
+
6
+ describe("CodeTuiCommands", () => {
7
+ test("the menu opens only while a bare command is being typed", () => {
8
+ expect(CodeTuiCommands.prefixOf("/mo", 3)).toBe("mo");
9
+ expect(CodeTuiCommands.prefixOf("/", 1)).toBe("");
10
+ expect(CodeTuiCommands.prefixOf("/model deepseek/x", 17)).toBeUndefined();
11
+ expect(CodeTuiCommands.prefixOf("add a comment module", 5)).toBeUndefined();
12
+ expect(CodeTuiCommands.prefixOf("/model deepseek/x", 3)).toBe("mo");
13
+ });
14
+
15
+ test("a prefix filters, and an unknown one offers nothing rather than everything", () => {
16
+ expect(CodeTuiCommands.matches("co").map((entry) => entry.name)).toEqual(["compact"]);
17
+ expect(CodeTuiCommands.matches("")).toHaveLength(CodeTuiCommands.all.length);
18
+ expect(CodeTuiCommands.matches("zzz")).toEqual([]);
19
+ });
20
+
21
+ test("help is generated from the same list, so it cannot go stale", () => {
22
+ const help = CodeTuiCommands.help();
23
+ for (const command of CodeTuiCommands.all) expect(help).toContain(`/${command.name}`);
24
+ });
25
+
26
+ test("every command the controller handles is offered, and every offered one is handled", async () => {
27
+ const source = await Bun.file(`${import.meta.dir}/CodeTui.tsx`).text();
28
+ const handled = new Set([...source.matchAll(/case "([a-z]+)":/g)].map((match) => match[1] ?? ""));
29
+ for (const command of CodeTuiCommands.all) expect(handled.has(command.name)).toBe(true);
30
+ });
31
+
32
+ test("every slash command the host offers is in the manual", async () => {
33
+ const manual = await Bun.file(path.join(akanCodePaths.builtinSkillsDir() ?? "", "akan-code", "SKILL.md")).text();
34
+ expect(
35
+ [...CodeTuiCommands.all].filter((command) => !manual.includes(`/${command.name}`)).map((c) => c.name),
36
+ ).toEqual([]);
37
+ });
38
+ });
@@ -0,0 +1,122 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { CodeTuiEditor } from "./CodeTuiEditor";
3
+
4
+ const editorOf = (text: string, caret = text.length) => {
5
+ const editor = new CodeTuiEditor();
6
+ editor.set(text, caret);
7
+ return editor;
8
+ };
9
+
10
+ describe("CodeTuiEditor", () => {
11
+ test("typing and deleting move the caret with the text", () => {
12
+ const editor = new CodeTuiEditor();
13
+ editor.insert("hello");
14
+ editor.left();
15
+ editor.insert("X");
16
+ expect(editor.text).toBe("hellXo");
17
+ expect(editor.caret).toBe(5);
18
+ editor.backspace();
19
+ expect(editor.text).toBe("hello");
20
+ });
21
+
22
+ test("a pasted block keeps its newlines and lands as one insert", () => {
23
+ const editor = new CodeTuiEditor();
24
+ editor.insert("fix this:\r at foo\r at bar");
25
+ expect(editor.text).toBe("fix this:\n at foo\n at bar");
26
+ });
27
+
28
+ test("backspace removes one Korean character, not one code unit", () => {
29
+ const editor = editorOf("안녕하세요");
30
+ editor.backspace();
31
+ expect(editor.text).toBe("안녕하세");
32
+ });
33
+
34
+ test("an emoji outside the BMP is one character to the caret", () => {
35
+ const editor = editorOf("hi 🙂");
36
+ editor.backspace();
37
+ expect(editor.text).toBe("hi ");
38
+ });
39
+
40
+ test("wraps by display width, so a Korean line breaks where it is drawn", () => {
41
+ // Ten columns of Korean is five syllables; a length-based wrap would fit ten and overflow the pane.
42
+ const { rows } = editorOf("안녕하세요반갑습니다").layout(10);
43
+ expect(rows.map((row) => row.text)).toEqual(["안녕하세요", "반갑습니다"]);
44
+ });
45
+
46
+ test("every row knows its own offset across both kinds of break", () => {
47
+ // "abcdef" soft-wraps at 4; the newline before "gh" is a hard break that consumes a character.
48
+ const { rows } = editorOf("abcdef\ngh").layout(4);
49
+ expect(rows).toEqual([
50
+ { text: "abcd", at: 0 },
51
+ { text: "ef", at: 4 },
52
+ { text: "gh", at: 7 },
53
+ ]);
54
+ });
55
+
56
+ test("up and down move a screen row, not a stored line", () => {
57
+ const editor = editorOf("abcdefghi", 6);
58
+ expect(editor.layout(4)).toMatchObject({ row: 1, col: 2 });
59
+ editor.move(-1, 4);
60
+ expect(editor.caret).toBe(2);
61
+ expect(editor.move(-1, 4)).toBe(false);
62
+ editor.move(1, 4);
63
+ expect(editor.caret).toBe(6);
64
+ });
65
+
66
+ test("moving up through Korean keeps the column instead of drifting", () => {
67
+ const editor = editorOf("안녕하세요반갑습니다", 8);
68
+ expect(editor.layout(10)).toMatchObject({ row: 1, col: 6 });
69
+ editor.move(-1, 10);
70
+ // Column 6 on the row above is after three syllables, not after six code units.
71
+ expect(editor.caret).toBe(3);
72
+ });
73
+
74
+ test("down from the last row reports that there is nowhere to go", () => {
75
+ const editor = editorOf("one line");
76
+ expect(editor.move(1, 40)).toBe(false);
77
+ });
78
+
79
+ test("home and end act on the visual row", () => {
80
+ const editor = editorOf("abcdefghi", 7);
81
+ editor.homeOfRow(4);
82
+ expect(editor.caret).toBe(4);
83
+ editor.endOfRow(4);
84
+ expect(editor.caret).toBe(8);
85
+ });
86
+
87
+ test("word deletion takes the word and the space it sat behind", () => {
88
+ const editor = editorOf("add a comment module");
89
+ editor.deleteWord();
90
+ expect(editor.text).toBe("add a comment");
91
+ });
92
+
93
+ test("an empty buffer still lays out one row, so the caret has somewhere to sit", () => {
94
+ expect(new CodeTuiEditor().layout(40)).toEqual({ rows: [{ text: "", at: 0 }], row: 0, col: 0 });
95
+ });
96
+ test("the continuation backslash a terminal sends with shift+enter does not land in the prompt", () => {
97
+ const editor = new CodeTuiEditor();
98
+ editor.insert("first\\");
99
+ editor.newline();
100
+ editor.insert("second");
101
+ expect(editor.text).toBe("first\nsecond");
102
+ });
103
+
104
+ test("only the one immediately before the break goes", () => {
105
+ const editor = new CodeTuiEditor();
106
+ editor.insert("a\\\\");
107
+ editor.newline();
108
+ expect(editor.text).toBe("a\\\n");
109
+ });
110
+
111
+ test("a newline typed with nothing before it is still a newline", () => {
112
+ const editor = new CodeTuiEditor();
113
+ editor.newline();
114
+ expect(editor.text).toBe("\n");
115
+ });
116
+
117
+ test("a pasted backslash before a newline is kept", () => {
118
+ const editor = new CodeTuiEditor();
119
+ editor.insert("const re = /\\\n/;");
120
+ expect(editor.text).toBe("const re = /\\\n/;");
121
+ });
122
+ });
@@ -0,0 +1,43 @@
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import { CodeTuiFiles } from "./CodeTuiFiles";
6
+
7
+ //? A repo of its own: CI unpacks the workspace from a tarball without `.git`, so the checkout lists nothing.
8
+ describe("CodeTuiFiles", () => {
9
+ let repoRoot: string;
10
+
11
+ beforeAll(async () => {
12
+ repoRoot = await mkdtemp(path.join(os.tmpdir(), "akan-code-tui-files-"));
13
+ for (const file of ["pkg/code/CodeTuiFiles.ts", "pkg/code/codeTuiFiles.test.ts", "pkg/other.ts"]) {
14
+ await mkdir(path.dirname(path.join(repoRoot, file)), { recursive: true });
15
+ await writeFile(path.join(repoRoot, file), "");
16
+ }
17
+ await Bun.spawn(["git", "init", "--quiet"], { cwd: repoRoot }).exited;
18
+ });
19
+
20
+ afterAll(async () => {
21
+ await rm(repoRoot, { recursive: true, force: true });
22
+ });
23
+
24
+ test("matches a file by any part of its path, shortest first", async () => {
25
+ const files = new CodeTuiFiles(repoRoot);
26
+ await files.load();
27
+ expect(files.ready).toBe(true);
28
+ const hits = files.match("CodeTuiFiles");
29
+ expect(hits).toEqual(["pkg/code/CodeTuiFiles.ts", "pkg/code/codeTuiFiles.test.ts"]);
30
+ });
31
+
32
+ test("paths are relative to the root the agent was given", async () => {
33
+ const files = new CodeTuiFiles(path.join(repoRoot, "pkg"));
34
+ await files.load();
35
+ expect(files.match("CodeTuiFiles")).toContain("code/CodeTuiFiles.ts");
36
+ });
37
+
38
+ test("an untracked directory lists nothing rather than walking the disk", async () => {
39
+ const files = new CodeTuiFiles("/");
40
+ await files.load();
41
+ expect(files.match("a")).toEqual([]);
42
+ });
43
+ });
@@ -0,0 +1,91 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { CodeTuiLines } from "./CodeTuiLines";
3
+ import { CodeTuiMarkdown } from "./CodeTuiMarkdown";
4
+
5
+ const render = (source: string, width = 60) =>
6
+ CodeTuiLines.rows(CodeTuiMarkdown.rows(source, width), width, "m").map((line) => CodeTuiLines.text(line));
7
+
8
+ const spanOf = (source: string, text: string) =>
9
+ CodeTuiLines.rows(CodeTuiMarkdown.rows(source, 60), 60, "m")
10
+ .flatMap((line) => line.spans)
11
+ .find((span) => span.text === text);
12
+
13
+ describe("CodeTuiMarkdown", () => {
14
+ test("a heading is styling, not hashes", () => {
15
+ expect(render("## Results").join("\n")).toBe("Results");
16
+ expect(spanOf("## Results", "Results")).toMatchObject({ bold: true, color: "cyan" });
17
+ });
18
+
19
+ test("inline markers style their text and are not printed", () => {
20
+ const line = render("use **cn** and `Err` here").join("\n");
21
+ expect(line).toBe("use cn and Err here");
22
+ expect(spanOf("use **cn** and `Err` here", "cn")).toMatchObject({ bold: true });
23
+ expect(spanOf("use **cn** and `Err` here", "Err")).toMatchObject({ color: "yellow" });
24
+ });
25
+
26
+ test("a list gets a marker and hangs its wrapped lines under the text", () => {
27
+ const lines = render("- first item that is quite long and will need to wrap somewhere", 30);
28
+ expect(lines[0]).toStartWith("• first item");
29
+ expect(lines[1]).toStartWith(" ");
30
+ expect(lines[1]?.trim()).not.toStartWith("•");
31
+ });
32
+
33
+ test("a nested list is indented by its depth", () => {
34
+ expect(render("- outer\n - inner")).toEqual(["• outer", " • inner"]);
35
+ });
36
+
37
+ test("a numbered list keeps its numbers", () => {
38
+ expect(render("1. one\n2. two")).toEqual(["1. one", "2. two"]);
39
+ });
40
+
41
+ test("a fenced block keeps its own line breaks and is not word-wrapped", () => {
42
+ const lines = render("```ts\nconst a = 1;\n const b = 2;\n```", 20);
43
+ expect(lines).toEqual(["ts", "│ const a = 1;", "│ const b = 2;"]);
44
+ });
45
+
46
+ test("a table aligns its columns by display width, so a Korean cell does not shift the row", () => {
47
+ const lines = render("| name | 값 |\n| --- | --- |\n| a | 가나 |\n| bb | 다 |");
48
+ const second = lines.map((line) => CodeTuiLines.width(line.slice(0, line.length - line.trimStart().length)) + 0);
49
+ expect(second).toBeDefined();
50
+ expect(lines[0]).toBe("name 값 ");
51
+ expect(lines[2]).toBe("a 가나");
52
+ expect(lines[3]).toBe("bb 다 ");
53
+ });
54
+
55
+ test("a table too wide for the pane narrows instead of overflowing", () => {
56
+ const source = [
57
+ "| file | what it does |",
58
+ "| --- | --- |",
59
+ '| minimal.service.ts | serve("minimal") with a batch server mode and one published fanout |',
60
+ ].join("\n");
61
+ const lines = render(source, 40);
62
+ for (const line of lines) expect(CodeTuiLines.width(line)).toBeLessThanOrEqual(40);
63
+ const wrapped = lines.slice(2);
64
+ expect(wrapped.length).toBeGreaterThan(1);
65
+ for (const line of wrapped) expect(line).toStartWith(" ".repeat(0));
66
+ expect(wrapped.every((line) => CodeTuiLines.width(line) <= 40)).toBe(true);
67
+ });
68
+
69
+ test("a link shows both its label and where it goes, because a terminal cannot hide one behind the other", () => {
70
+ expect(render("see [the docs](https://akanjs.com)").join("")).toBe("see the docs (https://akanjs.com)");
71
+ });
72
+
73
+ test("a javascript: url is refused the same way it is in the browser", () => {
74
+ expect(render("[click](javascript:alert(1))").join("")).toBe("click");
75
+ });
76
+
77
+ test("a quote and a rule read as themselves", () => {
78
+ expect(render("> careful")).toEqual(["▌ careful"]);
79
+ expect(render("---")[0]).toMatch(/^─+$/);
80
+ });
81
+
82
+ test("Korean prose wraps at the column it is drawn in, not at its code-unit count", () => {
83
+ const lines = render("안녕하세요 반갑습니다 여기는 터미널입니다", 12);
84
+ for (const line of lines) expect(CodeTuiLines.width(line)).toBeLessThanOrEqual(12);
85
+ expect(lines.length).toBeGreaterThan(1);
86
+ });
87
+
88
+ test("blocks are separated, so a heading does not sit on its paragraph", () => {
89
+ expect(render("# Title\n\nbody text")).toEqual(["Title", "", "body text"]);
90
+ });
91
+ });
@@ -0,0 +1,160 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { CodeAgentMcpServerRef, CodeAgentMcpStatus } from "akanjs/common";
3
+ import type { McpDeclaredServer } from "../tools/McpServerConfig";
4
+ import { CodeTuiMcp, type CodeTuiMcpView } from "./CodeTuiMcp";
5
+
6
+ const file = "/repo/.akan/code/mcp.json";
7
+ const globalFile = "/home/me/.akan/code/mcp.json";
8
+
9
+ const declared = (entry: CodeAgentMcpServerRef, patch: Partial<McpDeclaredServer> = {}): McpDeclaredServer => ({
10
+ ref: entry,
11
+ disabled: false,
12
+ scope: "workspace",
13
+ ...patch,
14
+ });
15
+
16
+ const ref = (name: string, patch: Partial<CodeAgentMcpServerRef> = {}): CodeAgentMcpServerRef => ({
17
+ name,
18
+ transport: "stdio",
19
+ command: "npx",
20
+ args: ["-y", `server-${name}`],
21
+ ...patch,
22
+ });
23
+
24
+ const live = (name: string, patch: Partial<CodeAgentMcpStatus> = {}): CodeAgentMcpStatus => ({
25
+ name,
26
+ transport: "stdio",
27
+ target: `npx -y server-${name}`,
28
+ tools: [`mcp__${name}__search`, `mcp__${name}__read`],
29
+ ...patch,
30
+ });
31
+
32
+ const view = (patch: Partial<CodeTuiMcpView> = {}): CodeTuiMcpView => ({
33
+ status: [],
34
+ declared: [],
35
+ files: [
36
+ { scope: "global", file: globalFile },
37
+ { scope: "workspace", file },
38
+ ],
39
+ ...patch,
40
+ });
41
+
42
+ describe("/mcp", () => {
43
+ test("each server says which file declares it, and both files are named", () => {
44
+ const text = CodeTuiMcp.list(
45
+ view({
46
+ status: [live("office"), live("repobot")],
47
+ declared: [declared(ref("office"), { scope: "global" }), declared(ref("repobot"))],
48
+ }),
49
+ );
50
+ expect(text).toMatch(/office\s+global/);
51
+ expect(text).toMatch(/repobot\s+workspace/);
52
+ expect(text).toContain(globalFile);
53
+ expect(text).toContain(file);
54
+ });
55
+
56
+ test("nothing declared says where to declare one", () => {
57
+ const text = CodeTuiMcp.list(view());
58
+ expect(text).toContain("No MCP server is declared.");
59
+ expect(text).toContain(file);
60
+ expect(text).toContain("/mcp add");
61
+ expect(text).toContain("/mcp login");
62
+ });
63
+
64
+ test("a connected server reads as its transport, its target and its tool count", () => {
65
+ const text = CodeTuiMcp.list(view({ status: [live("github")], declared: [declared(ref("github"))] }));
66
+ expect(text).toContain("1 server");
67
+ expect(text).toMatch(/github\s+workspace\s+stdio\s+npx -y server-github\s+2 tools/);
68
+ });
69
+
70
+ test("one tool is not 'tools'", () => {
71
+ const status = live("solo", { tools: ["mcp__solo__go"] });
72
+ expect(CodeTuiMcp.list(view({ status: [status], declared: [declared(ref("solo"))] }))).toContain("1 tool");
73
+ });
74
+
75
+ test("an unreachable server says why rather than going missing", () => {
76
+ const status = live("linear", { transport: "http", target: "https://x", tools: [], error: "fetch failed" });
77
+ const text = CodeTuiMcp.list(view({ status: [status], declared: [declared(ref("linear"))] }));
78
+ expect(text).toContain("unreachable — fetch failed");
79
+ });
80
+
81
+ // The session's tool allowlist is built once, so a server added to the file afterwards is declared but not connected.
82
+ test("a server the file gained after this session started asks for a reload", () => {
83
+ const text = CodeTuiMcp.list(view({ status: [live("github")], declared: [declared(ref("new"))] }));
84
+ expect(text).toContain("declared · /mcp reload to connect");
85
+ expect(text).toMatch(/github\s+stdio\s+npx -y server-github\s+2 tools · removed from the file/);
86
+ });
87
+
88
+ test("a server this repo declares and nobody approved says so, and a home one is unaffected", () => {
89
+ const text = CodeTuiMcp.list(
90
+ view({
91
+ workspaceApproved: false,
92
+ declared: [declared(ref("repo")), declared(ref("home"), { scope: "global" })],
93
+ }),
94
+ );
95
+ expect(text).toMatch(/repo\s+workspace\s+stdio\s+npx -y server-repo\s+not approved/);
96
+ expect(text).toMatch(/home\s+global\s+stdio\s+npx -y server-home\s+declared · \/mcp reload to connect/);
97
+ });
98
+
99
+ test("a server removed from the file is still the one this session is holding", () => {
100
+ expect(CodeTuiMcp.list(view({ status: [live("github")] }))).toContain("removed from the file");
101
+ });
102
+
103
+ test("a disabled entry is neither connected nor a failure", () => {
104
+ const text = CodeTuiMcp.list(view({ declared: [declared(ref("off"), { disabled: true })] }));
105
+ const row = text.split("\n").find((line) => line.startsWith("off"));
106
+ expect(row).toContain("disabled in the file");
107
+ expect(row).not.toContain("reload");
108
+ });
109
+
110
+ test("a server asking for sign-in is not reported as broken", () => {
111
+ const status = live("linear", { transport: "http", target: "https://x", tools: [], auth: "required" });
112
+ const text = CodeTuiMcp.list(view({ status: [status], declared: [declared(ref("linear"))] }));
113
+ expect(text).toContain("sign-in needed · /mcp login linear");
114
+ expect(text).not.toContain("unreachable");
115
+ });
116
+
117
+ test("a signed-in server says so beside its tool count", () => {
118
+ const status = live("github", { auth: "authorized" });
119
+ const text = CodeTuiMcp.list(view({ status: [status], declared: [declared(ref("github"))] }));
120
+ expect(text).toContain("2 tools · signed in");
121
+ });
122
+
123
+ test("a server needing no credential says nothing about one", () => {
124
+ const status = live("local", { auth: "none" });
125
+ const text = CodeTuiMcp.list(view({ status: [status], declared: [declared(ref("local"))] }));
126
+ expect(text).not.toContain("signed in");
127
+ expect(text).not.toContain("sign-in");
128
+ });
129
+
130
+ test("a broken file is named above the list rather than read as an empty one", () => {
131
+ expect(CodeTuiMcp.list(view({ problem: `${file} is not valid JSON — oops` }))).toContain("is not valid JSON");
132
+ });
133
+
134
+ test("servers are listed by name, so the order does not move between runs", () => {
135
+ const text = CodeTuiMcp.list(
136
+ view({
137
+ status: [live("zulip"), live("github")],
138
+ declared: [declared(ref("zulip")), declared(ref("github"))],
139
+ }),
140
+ );
141
+ expect(text.indexOf("github")).toBeLessThan(text.indexOf("zulip"));
142
+ });
143
+
144
+ test("one server's tools are named the way the model sees them", () => {
145
+ expect(CodeTuiMcp.tools(live("github"))).toContain("mcp__github__search");
146
+ expect(CodeTuiMcp.tools(live("x", { tools: [], error: "no" }))).toContain("unreachable");
147
+ expect(CodeTuiMcp.tools(live("x", { tools: [] }))).toContain("published no tools");
148
+ });
149
+
150
+ test("one server that needs a sign-in says so instead of listing tools that cannot answer", () => {
151
+ const refused = live("linear", { transport: "http", target: "https://x", auth: "required" });
152
+ expect(CodeTuiMcp.tools(refused)).toBe("linear needs signing in — /mcp login linear");
153
+ expect(CodeTuiMcp.tools({ ...refused, tools: [] })).toBe("linear needs signing in — /mcp login linear");
154
+ });
155
+
156
+ test("a name that would collide inside a tool name is refused", () => {
157
+ expect(CodeTuiMcp.namePattern.test("my-server_2")).toBe(true);
158
+ for (const name of ["my server", "a.b", "a/b", ""]) expect(CodeTuiMcp.namePattern.test(name)).toBe(false);
159
+ });
160
+ });
@@ -0,0 +1,61 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { CodeTuiMouse } from "./CodeTuiMouse";
3
+
4
+ /** What Ink hands the input handler for a mouse report: the CSI sequence with its leading escape stripped. */
5
+ const sgr = (button: number, release = false) => `[<${button};12;7${release ? "m" : "M"}`;
6
+
7
+ describe("mouse wheel", () => {
8
+ test("the terminal is asked for the wheel and told to stop again", () => {
9
+ expect(CodeTuiMouse.on).toBe("[?1000h[?1006h");
10
+ expect(CodeTuiMouse.off).toBe("[?1006l[?1000l");
11
+ });
12
+
13
+ test("a notch scrolls up or down by the same step", () => {
14
+ const mouse = new CodeTuiMouse();
15
+ expect(mouse.read(sgr(64))).toEqual({ rows: -CodeTuiMouse.step, rest: "" });
16
+ expect(mouse.read(sgr(65))).toEqual({ rows: CodeTuiMouse.step, rest: "" });
17
+ });
18
+
19
+ test("a modifier held over the wheel still scrolls", () => {
20
+ const mouse = new CodeTuiMouse();
21
+ // shift adds 4, alt 8, ctrl 16 — none of which changes which way the wheel turned.
22
+ expect(mouse.read(sgr(64 + 16))?.rows).toBe(-CodeTuiMouse.step);
23
+ expect(mouse.read(sgr(65 + 4))?.rows).toBe(CodeTuiMouse.step);
24
+ });
25
+
26
+ test("a click is swallowed rather than typed into the prompt", () => {
27
+ const mouse = new CodeTuiMouse();
28
+ for (const button of [0, 1, 2]) {
29
+ expect(mouse.read(sgr(button))).toEqual({ rows: 0, rest: "" });
30
+ expect(mouse.read(sgr(button, true))).toEqual({ rows: 0, rest: "" });
31
+ }
32
+ });
33
+
34
+ test("the horizontal wheel reports nothing to scroll", () => {
35
+ const mouse = new CodeTuiMouse();
36
+ expect(mouse.read(sgr(66))?.rows).toBe(0);
37
+ expect(mouse.read(sgr(67))?.rows).toBe(0);
38
+ });
39
+
40
+ test("a keystroke is not the mouse", () => {
41
+ const mouse = new CodeTuiMouse();
42
+ for (const input of ["a", "", "[200~", "[A", "[<64;12;7", "/mcp"]) expect(mouse.read(input)).toBeUndefined();
43
+ });
44
+
45
+ /** A terminal that ignores `?1006h` keeps sending `ESC[M` plus three bytes, which Ink splits into two chunks. */
46
+ test("the legacy encoding is read across the two chunks it arrives in", () => {
47
+ const mouse = new CodeTuiMouse();
48
+ expect(mouse.read("[M")).toEqual({ rows: 0, rest: "" });
49
+ // Cb is the button biased by 32, so wheel-up is 96 and the two coordinates follow it.
50
+ expect(mouse.read(`${String.fromCharCode(96)}!"`)).toEqual({ rows: -CodeTuiMouse.step, rest: "" });
51
+ expect(mouse.read("[M")).toEqual({ rows: 0, rest: "" });
52
+ expect(mouse.read(`${String.fromCharCode(97)}!"`)).toEqual({ rows: CodeTuiMouse.step, rest: "" });
53
+ });
54
+
55
+ test("typing that lands in the same chunk as a legacy report is handed back, not eaten", () => {
56
+ const mouse = new CodeTuiMouse();
57
+ mouse.read("[M");
58
+ expect(mouse.read(`${String.fromCharCode(96)}!"hi`)).toEqual({ rows: -CodeTuiMouse.step, rest: "hi" });
59
+ expect(mouse.read("hi")).toBeUndefined();
60
+ });
61
+ });