@aria-cli/tools 1.0.9 → 1.0.11

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 (241) hide show
  1. package/package.json +9 -5
  2. package/src/__tests__/web-fetch-download.test.ts +0 -433
  3. package/src/__tests__/web-tools.test.ts +0 -619
  4. package/src/ask-user-interaction.ts +0 -33
  5. package/src/cache/web-cache.ts +0 -110
  6. package/src/definitions/arion.ts +0 -118
  7. package/src/definitions/browser/browser.ts +0 -502
  8. package/src/definitions/browser/index.ts +0 -5
  9. package/src/definitions/browser/pw-downloads.ts +0 -142
  10. package/src/definitions/browser/pw-interactions.ts +0 -282
  11. package/src/definitions/browser/pw-responses.ts +0 -98
  12. package/src/definitions/browser/pw-session.ts +0 -405
  13. package/src/definitions/browser/pw-shared.ts +0 -85
  14. package/src/definitions/browser/pw-snapshot.ts +0 -383
  15. package/src/definitions/browser/pw-state.ts +0 -101
  16. package/src/definitions/browser/types.ts +0 -203
  17. package/src/definitions/code-intelligence.ts +0 -526
  18. package/src/definitions/core.ts +0 -118
  19. package/src/definitions/delegation.ts +0 -567
  20. package/src/definitions/deploy.ts +0 -73
  21. package/src/definitions/filesystem.ts +0 -217
  22. package/src/definitions/frg.ts +0 -67
  23. package/src/definitions/index.ts +0 -28
  24. package/src/definitions/memory.ts +0 -150
  25. package/src/definitions/messaging.ts +0 -734
  26. package/src/definitions/meta.ts +0 -392
  27. package/src/definitions/network.ts +0 -179
  28. package/src/definitions/outlook.ts +0 -318
  29. package/src/definitions/patch/apply-patch.ts +0 -235
  30. package/src/definitions/patch/fuzzy-match.ts +0 -217
  31. package/src/definitions/patch/index.ts +0 -1
  32. package/src/definitions/patch/patch-parser.ts +0 -297
  33. package/src/definitions/patch/sandbox-paths.ts +0 -129
  34. package/src/definitions/process/index.ts +0 -5
  35. package/src/definitions/process/process-registry.ts +0 -303
  36. package/src/definitions/process/process.ts +0 -456
  37. package/src/definitions/process/pty-keys.ts +0 -298
  38. package/src/definitions/process/session-slug.ts +0 -147
  39. package/src/definitions/quip.ts +0 -225
  40. package/src/definitions/search.ts +0 -67
  41. package/src/definitions/session-history.ts +0 -79
  42. package/src/definitions/shell.ts +0 -202
  43. package/src/definitions/slack.ts +0 -211
  44. package/src/definitions/web.ts +0 -119
  45. package/src/executors/apply-patch.ts +0 -1035
  46. package/src/executors/arion.ts +0 -199
  47. package/src/executors/code-intelligence.ts +0 -1179
  48. package/src/executors/deploy.ts +0 -1066
  49. package/src/executors/filesystem.ts +0 -1428
  50. package/src/executors/frg-freshness.ts +0 -743
  51. package/src/executors/frg.ts +0 -394
  52. package/src/executors/index.ts +0 -280
  53. package/src/executors/learning-meta.ts +0 -1367
  54. package/src/executors/lsp-client.ts +0 -355
  55. package/src/executors/memory.ts +0 -978
  56. package/src/executors/meta.ts +0 -293
  57. package/src/executors/process-registry.ts +0 -570
  58. package/src/executors/pty-session-store.ts +0 -43
  59. package/src/executors/pty.ts +0 -342
  60. package/src/executors/restart.ts +0 -133
  61. package/src/executors/search-freshness.ts +0 -249
  62. package/src/executors/search-types.ts +0 -98
  63. package/src/executors/search.ts +0 -89
  64. package/src/executors/self-diagnose.ts +0 -552
  65. package/src/executors/session-history.ts +0 -435
  66. package/src/executors/shell-safety.ts +0 -519
  67. package/src/executors/shell.ts +0 -1243
  68. package/src/executors/utils.ts +0 -40
  69. package/src/executors/web.ts +0 -786
  70. package/src/extraction/content-extraction.ts +0 -281
  71. package/src/extraction/index.ts +0 -5
  72. package/src/headless-control-contract.ts +0 -1149
  73. package/src/index.ts +0 -788
  74. package/src/local-control-http-auth.ts +0 -2
  75. package/src/mcp/client.ts +0 -218
  76. package/src/mcp/connection.ts +0 -568
  77. package/src/mcp/index.ts +0 -11
  78. package/src/mcp/jsonrpc.ts +0 -195
  79. package/src/mcp/types.ts +0 -199
  80. package/src/network-control-adapter.ts +0 -88
  81. package/src/network-runtime/address-types.ts +0 -218
  82. package/src/network-runtime/db-owner-fencing.ts +0 -91
  83. package/src/network-runtime/delivery-receipts.ts +0 -372
  84. package/src/network-runtime/direct-endpoint-authority.ts +0 -35
  85. package/src/network-runtime/index.ts +0 -316
  86. package/src/network-runtime/local-control-contract.ts +0 -784
  87. package/src/network-runtime/node-store-contract.ts +0 -46
  88. package/src/network-runtime/pair-route-contract.ts +0 -97
  89. package/src/network-runtime/peer-capabilities.ts +0 -48
  90. package/src/network-runtime/peer-principal-ref.ts +0 -20
  91. package/src/network-runtime/peer-state-machine.ts +0 -160
  92. package/src/network-runtime/protocol-schemas.ts +0 -265
  93. package/src/network-runtime/runtime-bootstrap-contract.ts +0 -83
  94. package/src/outlook/desktop-session.ts +0 -409
  95. package/src/policy.ts +0 -171
  96. package/src/providers/brave.ts +0 -80
  97. package/src/providers/duckduckgo.ts +0 -199
  98. package/src/providers/exa.ts +0 -85
  99. package/src/providers/firecrawl.ts +0 -77
  100. package/src/providers/index.ts +0 -8
  101. package/src/providers/jina.ts +0 -70
  102. package/src/providers/router.ts +0 -121
  103. package/src/providers/search-provider.ts +0 -74
  104. package/src/providers/tavily.ts +0 -74
  105. package/src/quip/desktop-session.ts +0 -435
  106. package/src/registry/index.ts +0 -1
  107. package/src/registry/registry.ts +0 -905
  108. package/src/runtime-socket-local-control-client.ts +0 -632
  109. package/src/security/dns-normalization.ts +0 -34
  110. package/src/security/dns-pinning.ts +0 -138
  111. package/src/security/external-content.ts +0 -129
  112. package/src/security/ssrf.ts +0 -207
  113. package/src/slack/desktop-session.ts +0 -493
  114. package/src/tool-factory.ts +0 -91
  115. package/src/types.ts +0 -1341
  116. package/src/utils/retry.ts +0 -163
  117. package/src/utils/safe-parse-json.ts +0 -176
  118. package/src/utils/url.ts +0 -20
  119. package/tests/benchmarks/registry.bench.ts +0 -57
  120. package/tests/cache/web-cache.test.ts +0 -147
  121. package/tests/critical-integration.test.ts +0 -1465
  122. package/tests/definitions/apply-patch.test.ts +0 -586
  123. package/tests/definitions/browser.test.ts +0 -495
  124. package/tests/definitions/delegation-pause-resume.test.ts +0 -758
  125. package/tests/definitions/execution.test.ts +0 -671
  126. package/tests/definitions/messaging-inbox-scope.test.ts +0 -229
  127. package/tests/definitions/messaging.test.ts +0 -1468
  128. package/tests/definitions/outlook.test.ts +0 -30
  129. package/tests/definitions/process.test.ts +0 -469
  130. package/tests/definitions/slack.test.ts +0 -28
  131. package/tests/definitions/tool-inventory.test.ts +0 -218
  132. package/tests/e2e/delegation-quest-orchestration.e2e.test.ts +0 -433
  133. package/tests/e2e/memory-tool-discovery-contract.e2e.test.ts +0 -81
  134. package/tests/executors/apply-patch.test.ts +0 -538
  135. package/tests/executors/arion.test.ts +0 -309
  136. package/tests/executors/conversation-primitives.test.ts +0 -250
  137. package/tests/executors/deploy.test.ts +0 -746
  138. package/tests/executors/filesystem-tools.test.ts +0 -357
  139. package/tests/executors/filesystem.test.ts +0 -959
  140. package/tests/executors/frg-freshness.test.ts +0 -136
  141. package/tests/executors/frg-merge.test.ts +0 -70
  142. package/tests/executors/frg-session-content.test.ts +0 -40
  143. package/tests/executors/frg.test.ts +0 -56
  144. package/tests/executors/memory-bugfixes.test.ts +0 -257
  145. package/tests/executors/memory-real-memoria.integration.test.ts +0 -316
  146. package/tests/executors/memory.test.ts +0 -853
  147. package/tests/executors/meta-tools.test.ts +0 -411
  148. package/tests/executors/meta.test.ts +0 -683
  149. package/tests/executors/path-containment.test.ts +0 -51
  150. package/tests/executors/process-registry.test.ts +0 -505
  151. package/tests/executors/pty.test.ts +0 -664
  152. package/tests/executors/quest-security.test.ts +0 -249
  153. package/tests/executors/read-file-media.test.ts +0 -230
  154. package/tests/executors/recall-knowledge-schema.test.ts +0 -209
  155. package/tests/executors/recall-tags.test.ts +0 -278
  156. package/tests/executors/remember-null-safety.contract.test.ts +0 -41
  157. package/tests/executors/restart.test.ts +0 -67
  158. package/tests/executors/search-unified.test.ts +0 -381
  159. package/tests/executors/session-history.test.ts +0 -340
  160. package/tests/executors/session-transcript.test.ts +0 -561
  161. package/tests/executors/shell-abort.test.ts +0 -416
  162. package/tests/executors/shell-env-blocklist.test.ts +0 -648
  163. package/tests/executors/shell-env-process.test.ts +0 -245
  164. package/tests/executors/shell-process-registry.test.ts +0 -334
  165. package/tests/executors/shell-tools.test.ts +0 -393
  166. package/tests/executors/shell.test.ts +0 -690
  167. package/tests/executors/web-abort-vs-timeout.test.ts +0 -213
  168. package/tests/executors/web-integration.test.ts +0 -633
  169. package/tests/executors/web-symlink.test.ts +0 -18
  170. package/tests/executors/web.test.ts +0 -1400
  171. package/tests/executors/write-stdin.test.ts +0 -145
  172. package/tests/extraction/content-extraction.test.ts +0 -153
  173. package/tests/guards/tools-default-test-lane.integration.test.ts +0 -21
  174. package/tests/guards/tools-package-test-commands.e2e.test.ts +0 -43
  175. package/tests/guards/tools-test-lane-manifest.contract.test.ts +0 -76
  176. package/tests/guards/tools-vitest-workspace-alias.contract.test.ts +0 -63
  177. package/tests/helpers/async-waits.ts +0 -53
  178. package/tests/integration/headless-control-contract.integration.test.ts +0 -153
  179. package/tests/integration/memory-tool-schema-parity.integration.test.ts +0 -67
  180. package/tests/integration/meta-tools-round-trip.integration.test.ts +0 -506
  181. package/tests/integration/quest-round-trip.test.ts +0 -303
  182. package/tests/integration/registry-executor-flow.test.ts +0 -85
  183. package/tests/integration.test.ts +0 -177
  184. package/tests/loading-tier.test.ts +0 -126
  185. package/tests/mcp/client-reconnect.test.ts +0 -267
  186. package/tests/mcp/connection.test.ts +0 -846
  187. package/tests/mcp/injectable-logger.test.ts +0 -83
  188. package/tests/mcp/jsonrpc.test.ts +0 -109
  189. package/tests/mcp/lifecycle.test.ts +0 -879
  190. package/tests/network-runtime/address-types.contract.test.ts +0 -143
  191. package/tests/network-runtime/continuity-bind-schema.contract.test.ts +0 -203
  192. package/tests/network-runtime/local-control-contract.test.ts +0 -869
  193. package/tests/network-runtime/local-control-invite-token.contract.test.ts +0 -146
  194. package/tests/network-runtime/node-store-contract.test.ts +0 -11
  195. package/tests/network-runtime/pair-protocol-nodeid.contract.test.ts +0 -15
  196. package/tests/network-runtime/peer-state-machine.contract.test.ts +0 -148
  197. package/tests/network-runtime/protocol-schemas.contract.test.ts +0 -512
  198. package/tests/network-runtime/relay-pending-nodeid.contract.test.ts +0 -62
  199. package/tests/network-runtime/runtime-bootstrap-contract.test.ts +0 -227
  200. package/tests/network-runtime/runtime-socket-local-control-client.test.ts +0 -621
  201. package/tests/network-runtime/wait-for-message-script.test.ts +0 -288
  202. package/tests/parallel.test.ts +0 -71
  203. package/tests/policy.test.ts +0 -184
  204. package/tests/print-default-test-lane.ts +0 -14
  205. package/tests/print-test-lane-manifest.ts +0 -22
  206. package/tests/providers/brave.test.ts +0 -159
  207. package/tests/providers/duckduckgo.test.ts +0 -207
  208. package/tests/providers/exa.test.ts +0 -175
  209. package/tests/providers/firecrawl.test.ts +0 -168
  210. package/tests/providers/jina.test.ts +0 -144
  211. package/tests/providers/router.test.ts +0 -328
  212. package/tests/providers/tavily.test.ts +0 -165
  213. package/tests/registry/discovery.test.ts +0 -154
  214. package/tests/registry/injectable-logger.test.ts +0 -230
  215. package/tests/registry/input-validation.test.ts +0 -361
  216. package/tests/registry/interface-completeness.test.ts +0 -85
  217. package/tests/registry/mcp-integration.test.ts +0 -103
  218. package/tests/registry/mcp-read-only-hint.test.ts +0 -60
  219. package/tests/registry/memoria-discovery.test.ts +0 -390
  220. package/tests/registry/nested-validation.test.ts +0 -283
  221. package/tests/registry/pseudo-tool-filtering.test.ts +0 -258
  222. package/tests/registry/registration-lifecycle.test.ts +0 -133
  223. package/tests/registry-validation.test.ts +0 -424
  224. package/tests/registry.test.ts +0 -460
  225. package/tests/security/dns-pinning.test.ts +0 -162
  226. package/tests/security/external-content.test.ts +0 -144
  227. package/tests/security/ssrf.test.ts +0 -118
  228. package/tests/shell-safety-integration.test.ts +0 -32
  229. package/tests/shell-safety.test.ts +0 -365
  230. package/tests/slack/desktop-session.test.ts +0 -50
  231. package/tests/test-lane-manifest.ts +0 -440
  232. package/tests/test-utils.ts +0 -27
  233. package/tests/tool-factory.test.ts +0 -188
  234. package/tests/utils/retry.test.ts +0 -231
  235. package/tests/utils/url.test.ts +0 -63
  236. package/tsconfig.cjs.json +0 -24
  237. package/tsconfig.json +0 -12
  238. package/vitest.config.ts +0 -55
  239. package/vitest.e2e.config.ts +0 -24
  240. package/vitest.integration.config.ts +0 -24
  241. package/vitest.native.config.ts +0 -24
@@ -1,561 +0,0 @@
1
- /**
2
- * @aria/tools - Session transcript tests
3
- *
4
- * Tests for the real getSessionTranscript implementation that reads
5
- * Claude Code session transcripts from ~/.claude/projects/ JSONL files.
6
- *
7
- * Uses temporary directories with mock JSONL files to test:
8
- * - Reading from JSONL files
9
- * - Handling missing/empty directories
10
- * - Handling malformed JSONL
11
- * - Symlink safety (skips them)
12
- * - Correct turn count
13
- * - Content truncation to 500 chars
14
- * - Most-recent-first sorting by mtime
15
- * - sessionId matching
16
- */
17
-
18
- import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
19
- import fs from "node:fs";
20
- import fsp from "node:fs/promises";
21
- import path from "node:path";
22
- import os from "node:os";
23
-
24
- // We need to test getSessionTranscript which reads from ~/.claude/projects/
25
- // To avoid touching real files, we mock the filesystem paths via os.homedir()
26
- import { getSessionTranscript } from "../../src/executors/memory.js";
27
-
28
- describe("getSessionTranscript", () => {
29
- let tmpDir: string;
30
- let originalHomedir: () => string;
31
- let projectsDir: string;
32
-
33
- beforeEach(() => {
34
- // Create a temp directory to act as the home directory
35
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "aria-transcript-test-"));
36
- projectsDir = path.join(tmpDir, ".claude", "projects");
37
- fs.mkdirSync(projectsDir, { recursive: true });
38
-
39
- // Mock os.homedir() to return our temp directory
40
- originalHomedir = os.homedir;
41
- vi.spyOn(os, "homedir").mockReturnValue(tmpDir);
42
- });
43
-
44
- afterEach(() => {
45
- // Restore os.homedir
46
- vi.restoreAllMocks();
47
- // Clean up temp directory
48
- fs.rmSync(tmpDir, { recursive: true, force: true });
49
- });
50
-
51
- // ── Helpers ──────────────────────────────────────────────────────────
52
-
53
- function createProjectDir(name: string): string {
54
- const dir = path.join(projectsDir, name);
55
- fs.mkdirSync(dir, { recursive: true });
56
- return dir;
57
- }
58
-
59
- function writeSessionFile(
60
- projectDir: string,
61
- filename: string,
62
- lines: string[],
63
- mtime?: Date,
64
- ): string {
65
- const filePath = path.join(projectDir, filename);
66
- fs.writeFileSync(filePath, lines.join("\n"), "utf-8");
67
- if (mtime) {
68
- fs.utimesSync(filePath, mtime, mtime);
69
- }
70
- return filePath;
71
- }
72
-
73
- function makeUserEntry(content: string): string {
74
- return JSON.stringify({
75
- type: "user",
76
- sessionId: "test-session",
77
- uuid: "uuid-1",
78
- timestamp: new Date().toISOString(),
79
- message: { role: "user", content },
80
- });
81
- }
82
-
83
- function makeAssistantEntry(content: string): string {
84
- return JSON.stringify({
85
- type: "assistant",
86
- sessionId: "test-session",
87
- uuid: "uuid-2",
88
- timestamp: new Date().toISOString(),
89
- message: { role: "assistant", content },
90
- });
91
- }
92
-
93
- function makeUserEntryWithBlocks(blocks: Array<{ type: string; text?: string }>): string {
94
- return JSON.stringify({
95
- type: "user",
96
- sessionId: "test-session",
97
- uuid: "uuid-1",
98
- timestamp: new Date().toISOString(),
99
- message: { role: "user", content: blocks },
100
- });
101
- }
102
-
103
- function makeAssistantEntryWithBlocks(blocks: Array<{ type: string; text?: string }>): string {
104
- return JSON.stringify({
105
- type: "assistant",
106
- sessionId: "test-session",
107
- uuid: "uuid-2",
108
- timestamp: new Date().toISOString(),
109
- message: { role: "assistant", content: blocks },
110
- });
111
- }
112
-
113
- // ── Tests ────────────────────────────────────────────────────────────
114
-
115
- describe("basic functionality", () => {
116
- it("reads user and assistant messages from a JSONL file", async () => {
117
- const dir = createProjectDir("my-project");
118
- writeSessionFile(dir, "session-abc.jsonl", [
119
- makeUserEntry("Hello"),
120
- makeAssistantEntry("Hi there!"),
121
- makeUserEntry("What is 2+2?"),
122
- makeAssistantEntry("4"),
123
- ]);
124
-
125
- const result = await getSessionTranscript({});
126
-
127
- expect(result).toHaveLength(4);
128
- expect(result[0]).toEqual({ role: "user", content: "Hello" });
129
- expect(result[1]).toEqual({ role: "assistant", content: "Hi there!" });
130
- expect(result[2]).toEqual({ role: "user", content: "What is 2+2?" });
131
- expect(result[3]).toEqual({ role: "assistant", content: "4" });
132
- });
133
-
134
- it("parses content block arrays (text blocks)", async () => {
135
- const dir = createProjectDir("my-project");
136
- writeSessionFile(dir, "session-blocks.jsonl", [
137
- makeUserEntryWithBlocks([
138
- { type: "text", text: "First part" },
139
- { type: "text", text: "Second part" },
140
- ]),
141
- makeAssistantEntryWithBlocks([
142
- { type: "text", text: "Response text" },
143
- { type: "tool_use" }, // Non-text blocks should be skipped
144
- ]),
145
- ]);
146
-
147
- const result = await getSessionTranscript({});
148
-
149
- expect(result).toHaveLength(2);
150
- expect(result[0]).toEqual({ role: "user", content: "First part\nSecond part" });
151
- expect(result[1]).toEqual({ role: "assistant", content: "Response text" });
152
- });
153
-
154
- it("skips non-user/assistant entry types", async () => {
155
- const dir = createProjectDir("my-project");
156
- writeSessionFile(dir, "session-mixed.jsonl", [
157
- makeUserEntry("Hello"),
158
- JSON.stringify({ type: "progress", message: { content: "thinking..." } }),
159
- JSON.stringify({ type: "system", message: { content: "init" } }),
160
- makeAssistantEntry("Hi!"),
161
- ]);
162
-
163
- const result = await getSessionTranscript({});
164
-
165
- expect(result).toHaveLength(2);
166
- expect(result[0].role).toBe("user");
167
- expect(result[1].role).toBe("assistant");
168
- });
169
- });
170
-
171
- describe("turn count", () => {
172
- it("returns last N turns (default 10 = 20 messages)", async () => {
173
- const dir = createProjectDir("my-project");
174
- const lines: string[] = [];
175
- for (let i = 0; i < 25; i++) {
176
- lines.push(makeUserEntry(`Question ${i}`));
177
- lines.push(makeAssistantEntry(`Answer ${i}`));
178
- }
179
- writeSessionFile(dir, "big-session.jsonl", lines);
180
-
181
- // Default is 10 turns = 20 messages
182
- const result = await getSessionTranscript({});
183
-
184
- expect(result).toHaveLength(20);
185
- // Should start from message index 30 (50 - 20)
186
- expect(result[0]).toEqual({ role: "user", content: "Question 15" });
187
- expect(result[19]).toEqual({ role: "assistant", content: "Answer 24" });
188
- });
189
-
190
- it("returns last 2 turns (4 messages) when turns=2", async () => {
191
- const dir = createProjectDir("my-project");
192
- writeSessionFile(dir, "session.jsonl", [
193
- makeUserEntry("Q1"),
194
- makeAssistantEntry("A1"),
195
- makeUserEntry("Q2"),
196
- makeAssistantEntry("A2"),
197
- makeUserEntry("Q3"),
198
- makeAssistantEntry("A3"),
199
- ]);
200
-
201
- const result = await getSessionTranscript({ turns: 2 });
202
-
203
- expect(result).toHaveLength(4);
204
- expect(result[0]).toEqual({ role: "user", content: "Q2" });
205
- expect(result[3]).toEqual({ role: "assistant", content: "A3" });
206
- });
207
-
208
- it("returns all messages when turns exceeds available", async () => {
209
- const dir = createProjectDir("my-project");
210
- writeSessionFile(dir, "session.jsonl", [makeUserEntry("Q1"), makeAssistantEntry("A1")]);
211
-
212
- const result = await getSessionTranscript({ turns: 100 });
213
-
214
- expect(result).toHaveLength(2);
215
- });
216
-
217
- it("returns empty array when turns=0", async () => {
218
- const dir = createProjectDir("my-project");
219
- writeSessionFile(dir, "session.jsonl", [makeUserEntry("Q1"), makeAssistantEntry("A1")]);
220
-
221
- const result = await getSessionTranscript({ turns: 0 });
222
-
223
- expect(result).toEqual([]);
224
- });
225
-
226
- it("returns empty array when turns is negative", async () => {
227
- const dir = createProjectDir("my-project");
228
- writeSessionFile(dir, "session.jsonl", [makeUserEntry("Q1"), makeAssistantEntry("A1")]);
229
-
230
- const result = await getSessionTranscript({ turns: -5 });
231
-
232
- expect(result).toEqual([]);
233
- });
234
-
235
- it("tails large session files without using a full read", async () => {
236
- const dir = createProjectDir("my-project");
237
- const fillerLines = Array.from({ length: 256 }, (_, index) =>
238
- index % 2 === 0
239
- ? makeUserEntry(`Filler user ${index}: ${"x".repeat(2048)}`)
240
- : makeAssistantEntry(`Filler assistant ${index}: ${"y".repeat(2048)}`),
241
- );
242
- const filePath = writeSessionFile(dir, "large-session.jsonl", [
243
- ...fillerLines,
244
- makeUserEntry("Tail Q1"),
245
- makeAssistantEntry("Tail A1"),
246
- makeUserEntry("Tail Q2"),
247
- makeAssistantEntry("Tail A2"),
248
- ]);
249
-
250
- const originalReadFile = fsp.readFile.bind(fsp) as typeof fsp.readFile;
251
- vi.spyOn(fsp, "readFile").mockImplementation(async (target, options) => {
252
- if (typeof target === "string" && target === filePath) {
253
- throw new Error("getSessionTranscript must not full-read transcript files");
254
- }
255
- return originalReadFile(target as never, options as never);
256
- });
257
-
258
- const result = await getSessionTranscript({ turns: 2 });
259
-
260
- expect(result).toEqual([
261
- { role: "user", content: "Tail Q1" },
262
- { role: "assistant", content: "Tail A1" },
263
- { role: "user", content: "Tail Q2" },
264
- { role: "assistant", content: "Tail A2" },
265
- ]);
266
- });
267
- });
268
-
269
- describe("content truncation", () => {
270
- it("truncates content to 500 characters", async () => {
271
- const longContent = "x".repeat(800);
272
- const dir = createProjectDir("my-project");
273
- writeSessionFile(dir, "session.jsonl", [
274
- makeUserEntry(longContent),
275
- makeAssistantEntry("short"),
276
- ]);
277
-
278
- const result = await getSessionTranscript({});
279
-
280
- expect(result).toHaveLength(2);
281
- expect(result[0].content).toHaveLength(500);
282
- expect(result[0].content).toBe("x".repeat(500));
283
- expect(result[1].content).toBe("short");
284
- });
285
-
286
- it("does not truncate content under 500 characters", async () => {
287
- const content = "x".repeat(499);
288
- const dir = createProjectDir("my-project");
289
- writeSessionFile(dir, "session.jsonl", [makeUserEntry(content)]);
290
-
291
- const result = await getSessionTranscript({});
292
-
293
- expect(result[0].content).toHaveLength(499);
294
- });
295
-
296
- it("exactly 500 characters is not truncated", async () => {
297
- const content = "x".repeat(500);
298
- const dir = createProjectDir("my-project");
299
- writeSessionFile(dir, "session.jsonl", [makeUserEntry(content)]);
300
-
301
- const result = await getSessionTranscript({});
302
-
303
- expect(result[0].content).toHaveLength(500);
304
- });
305
- });
306
-
307
- describe("mtime sorting (most recent first)", () => {
308
- it("selects the most recent session file by mtime", async () => {
309
- const dir = createProjectDir("my-project");
310
-
311
- // Old session (1 day ago)
312
- writeSessionFile(
313
- dir,
314
- "old-session.jsonl",
315
- [makeUserEntry("Old question"), makeAssistantEntry("Old answer")],
316
- new Date(Date.now() - 86400000),
317
- );
318
-
319
- // New session (now)
320
- writeSessionFile(
321
- dir,
322
- "new-session.jsonl",
323
- [makeUserEntry("New question"), makeAssistantEntry("New answer")],
324
- new Date(),
325
- );
326
-
327
- const result = await getSessionTranscript({});
328
-
329
- expect(result).toHaveLength(2);
330
- expect(result[0].content).toBe("New question");
331
- expect(result[1].content).toBe("New answer");
332
- });
333
-
334
- it("selects most recent across multiple project directories", async () => {
335
- const dirA = createProjectDir("project-a");
336
- const dirB = createProjectDir("project-b");
337
-
338
- // project-a has an older session
339
- writeSessionFile(
340
- dirA,
341
- "session-a.jsonl",
342
- [makeUserEntry("From project A"), makeAssistantEntry("A response")],
343
- new Date(Date.now() - 86400000),
344
- );
345
-
346
- // project-b has a newer session
347
- writeSessionFile(
348
- dirB,
349
- "session-b.jsonl",
350
- [makeUserEntry("From project B"), makeAssistantEntry("B response")],
351
- new Date(),
352
- );
353
-
354
- const result = await getSessionTranscript({});
355
-
356
- expect(result).toHaveLength(2);
357
- expect(result[0].content).toBe("From project B");
358
- });
359
- });
360
-
361
- describe("sessionId matching", () => {
362
- it("matches session by sessionId substring in path", async () => {
363
- const dir = createProjectDir("my-project");
364
-
365
- writeSessionFile(
366
- dir,
367
- "target-session-xyz.jsonl",
368
- [makeUserEntry("Target Q"), makeAssistantEntry("Target A")],
369
- new Date(Date.now() - 86400000), // Older
370
- );
371
-
372
- writeSessionFile(
373
- dir,
374
- "other-session.jsonl",
375
- [makeUserEntry("Other Q"), makeAssistantEntry("Other A")],
376
- new Date(), // Newer
377
- );
378
-
379
- // Should match the older one by sessionId, even though it's not the most recent
380
- const result = await getSessionTranscript({ sessionId: "target-session-xyz" });
381
-
382
- expect(result).toHaveLength(2);
383
- expect(result[0].content).toBe("Target Q");
384
- });
385
-
386
- it("returns empty array when sessionId matches nothing", async () => {
387
- const dir = createProjectDir("my-project");
388
- writeSessionFile(dir, "session-abc.jsonl", [makeUserEntry("Q"), makeAssistantEntry("A")]);
389
-
390
- const result = await getSessionTranscript({ sessionId: "nonexistent-session-id" });
391
-
392
- expect(result).toEqual([]);
393
- });
394
- });
395
-
396
- describe("symlink safety", () => {
397
- it("skips symlinked project directories", async () => {
398
- const realDir = createProjectDir("real-project");
399
- writeSessionFile(realDir, "session.jsonl", [
400
- makeUserEntry("Real Q"),
401
- makeAssistantEntry("Real A"),
402
- ]);
403
-
404
- // Create a symlink to the real directory
405
- const symlinkPath = path.join(projectsDir, "symlinked-project");
406
- fs.symlinkSync(realDir, symlinkPath);
407
-
408
- // Should still find the real project's session
409
- const result = await getSessionTranscript({});
410
-
411
- expect(result).toHaveLength(2);
412
- expect(result[0].content).toBe("Real Q");
413
- });
414
-
415
- it("skips symlinked JSONL files", async () => {
416
- const dir = createProjectDir("my-project");
417
- const realFile = writeSessionFile(
418
- dir,
419
- "real-session.jsonl",
420
- [makeUserEntry("Real Q"), makeAssistantEntry("Real A")],
421
- new Date(Date.now() - 86400000), // Older
422
- );
423
-
424
- // Create a symlinked session file that is "newer"
425
- const symlinkFile = path.join(dir, "symlinked-session.jsonl");
426
- fs.symlinkSync(realFile, symlinkFile);
427
- // Touch the symlink to be "newer" — but lstat won't follow symlinks
428
-
429
- const result = await getSessionTranscript({});
430
-
431
- // Should find the real session (not the symlink)
432
- expect(result).toHaveLength(2);
433
- expect(result[0].content).toBe("Real Q");
434
- });
435
- });
436
-
437
- describe("missing/empty directories", () => {
438
- it("returns empty array when ~/.claude/projects/ does not exist", async () => {
439
- // Remove the projects directory
440
- fs.rmSync(projectsDir, { recursive: true, force: true });
441
-
442
- const result = await getSessionTranscript({});
443
-
444
- expect(result).toEqual([]);
445
- });
446
-
447
- it("returns empty array when projects directory is empty", async () => {
448
- // projectsDir exists but has no subdirectories
449
- const result = await getSessionTranscript({});
450
-
451
- expect(result).toEqual([]);
452
- });
453
-
454
- it("returns empty array when project directory has no JSONL files", async () => {
455
- createProjectDir("empty-project");
456
-
457
- const result = await getSessionTranscript({});
458
-
459
- expect(result).toEqual([]);
460
- });
461
-
462
- it("returns empty array when JSONL file is empty", async () => {
463
- const dir = createProjectDir("my-project");
464
- writeSessionFile(dir, "empty-session.jsonl", []);
465
-
466
- const result = await getSessionTranscript({});
467
-
468
- expect(result).toEqual([]);
469
- });
470
- });
471
-
472
- describe("malformed JSONL handling", () => {
473
- it("skips malformed JSON lines and still parses valid ones", async () => {
474
- const dir = createProjectDir("my-project");
475
- writeSessionFile(dir, "session.jsonl", [
476
- makeUserEntry("Valid Q1"),
477
- "{ this is not valid JSON !!!",
478
- "definitely not JSON",
479
- makeAssistantEntry("Valid A1"),
480
- "",
481
- makeUserEntry("Valid Q2"),
482
- makeAssistantEntry("Valid A2"),
483
- ]);
484
-
485
- const result = await getSessionTranscript({});
486
-
487
- expect(result).toHaveLength(4);
488
- expect(result[0].content).toBe("Valid Q1");
489
- expect(result[1].content).toBe("Valid A1");
490
- expect(result[2].content).toBe("Valid Q2");
491
- expect(result[3].content).toBe("Valid A2");
492
- });
493
-
494
- it("handles entries without message field", async () => {
495
- const dir = createProjectDir("my-project");
496
- writeSessionFile(dir, "session.jsonl", [
497
- JSON.stringify({ type: "user" }), // No message field
498
- makeUserEntry("Valid Q"),
499
- makeAssistantEntry("Valid A"),
500
- ]);
501
-
502
- const result = await getSessionTranscript({});
503
-
504
- expect(result).toHaveLength(2);
505
- expect(result[0].content).toBe("Valid Q");
506
- });
507
-
508
- it("handles entries with empty content", async () => {
509
- const dir = createProjectDir("my-project");
510
- writeSessionFile(dir, "session.jsonl", [
511
- JSON.stringify({
512
- type: "user",
513
- sessionId: "s",
514
- uuid: "u",
515
- timestamp: new Date().toISOString(),
516
- message: { role: "user", content: "" },
517
- }),
518
- makeUserEntry("Real Q"),
519
- makeAssistantEntry("Real A"),
520
- ]);
521
-
522
- const result = await getSessionTranscript({});
523
-
524
- // Empty content entry should be skipped
525
- expect(result).toHaveLength(2);
526
- expect(result[0].content).toBe("Real Q");
527
- });
528
-
529
- it("handles content blocks with no text blocks", async () => {
530
- const dir = createProjectDir("my-project");
531
- writeSessionFile(dir, "session.jsonl", [
532
- makeUserEntryWithBlocks([{ type: "tool_use" }, { type: "tool_result" }]),
533
- makeUserEntry("Valid Q"),
534
- makeAssistantEntry("Valid A"),
535
- ]);
536
-
537
- const result = await getSessionTranscript({});
538
-
539
- // tool_use/tool_result only entry produces empty text, should be skipped
540
- expect(result).toHaveLength(2);
541
- });
542
- });
543
-
544
- describe("non-.jsonl files are ignored", () => {
545
- it("ignores non-JSONL files in project directories", async () => {
546
- const dir = createProjectDir("my-project");
547
- // Write a non-JSONL file
548
- fs.writeFileSync(path.join(dir, "notes.txt"), makeUserEntry("Should not read this"));
549
- // Write an actual JSONL file
550
- writeSessionFile(dir, "session.jsonl", [
551
- makeUserEntry("Real Q"),
552
- makeAssistantEntry("Real A"),
553
- ]);
554
-
555
- const result = await getSessionTranscript({});
556
-
557
- expect(result).toHaveLength(2);
558
- expect(result[0].content).toBe("Real Q");
559
- });
560
- });
561
- });