ceulen 0.13.0 → 0.14.0
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.
- package/CHANGELOG.md +42 -0
- package/extensions/modules/a2a/lib/config.ts +15 -1
- package/extensions/modules/a2a/test/config.test.ts +27 -0
- package/extensions/modules/fff/index.ts +19 -0
- package/extensions/modules/fff/test/index.test.ts +31 -4
- package/extensions/modules/gh/configPanel.ts +9 -6
- package/extensions/modules/gh/test/gh.test.ts +61 -0
- package/extensions/modules/jfind/index.ts +10 -1
- package/extensions/modules/jfind/lib/cascade.ts +15 -1
- package/extensions/modules/jfind/lib/lexical.ts +18 -6
- package/extensions/modules/jfind/lib/tree.ts +34 -4
- package/extensions/modules/jfind/test/cascade.test.ts +56 -1
- package/extensions/modules/repair/configPanel.ts +11 -8
- package/extensions/modules/repair/index.ts +58 -25
- package/extensions/modules/repair/lib/auto-bg.ts +45 -2
- package/extensions/modules/repair/lib/auto-generated.ts +14 -4
- package/extensions/modules/repair/lib/read-archive.ts +14 -0
- package/extensions/modules/repair/lib/read-sqlite.ts +10 -4
- package/extensions/modules/repair/lib/settings.ts +19 -8
- package/extensions/modules/repair/test/auto-bg.test.ts +66 -0
- package/extensions/modules/repair/test/hardening.test.ts +214 -0
- package/extensions/modules/repair/test/settings.test.ts +52 -13
- package/extensions/modules/shells/index.ts +31 -14
- package/extensions/modules/shells/lib/sessions.ts +76 -8
- package/extensions/modules/shells/test/output.test.ts +130 -0
- package/extensions/modules/shells/test/sessions.test.ts +127 -5
- package/extensions/modules/web/lib/pdf.ts +4 -2
- package/extensions/modules/web/test/pdf.test.ts +84 -0
- package/package.json +1 -1
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Unit tests for the tool's output rendering + store lifecycle wiring
|
|
2
|
+
// (index.ts): per-stream `since:"last"` cursors and the session_start clear.
|
|
3
|
+
// index.ts is loaded through a stub ExtensionAPI; spawn is not touched.
|
|
4
|
+
|
|
5
|
+
import assert from "node:assert/strict";
|
|
6
|
+
import { describe, it } from "node:test";
|
|
7
|
+
import { RingBuffer, SessionStore, type ShellSession } from "../lib/sessions.js";
|
|
8
|
+
import mod, { getStoreForTests } from "../index.js";
|
|
9
|
+
|
|
10
|
+
/** Drive the module against a stub pi and return the registered tool + handlers. */
|
|
11
|
+
function load(): { tool: any; handlers: Record<string, Function>; store: SessionStore } {
|
|
12
|
+
const handlers: Record<string, Function> = {};
|
|
13
|
+
let tool: any;
|
|
14
|
+
const fakePi: any = {
|
|
15
|
+
on: (ev: string, fn: Function) => { handlers[ev] = fn; },
|
|
16
|
+
registerTool: (t: any) => { tool = t; },
|
|
17
|
+
};
|
|
18
|
+
mod(fakePi);
|
|
19
|
+
// Reach the store for cursor/clear assertions (module closes over it).
|
|
20
|
+
return { tool, handlers, store: getStoreForTests()! };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function session(over: Partial<ShellSession>): ShellSession {
|
|
24
|
+
return {
|
|
25
|
+
id: "s1",
|
|
26
|
+
pid: 1,
|
|
27
|
+
child: {} as never,
|
|
28
|
+
cwd: "/",
|
|
29
|
+
command: "true",
|
|
30
|
+
startedAt: Date.now(),
|
|
31
|
+
stdout: new RingBuffer(),
|
|
32
|
+
stderr: new RingBuffer(),
|
|
33
|
+
lastStdoutOffset: 0,
|
|
34
|
+
lastStderrOffset: 0,
|
|
35
|
+
...over,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe("output cursors (since:\"last\")", () => {
|
|
40
|
+
it("advances per-stream, independently", async () => {
|
|
41
|
+
const { tool, store } = load();
|
|
42
|
+
const s = session({ id: "s1" });
|
|
43
|
+
s.stdout.push("out-1\n");
|
|
44
|
+
s.stderr.push("err-1\n");
|
|
45
|
+
store.add(s);
|
|
46
|
+
|
|
47
|
+
const read = async () => {
|
|
48
|
+
const r = await tool.execute("t", { action: "output", id: "s1", since: "last" }, undefined as never, undefined as never, { cwd: "/" } as never);
|
|
49
|
+
return (r.content[0] as { text: string }).text;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
let text = await read();
|
|
53
|
+
assert.match(text, /out-1/);
|
|
54
|
+
assert.match(text, /err-1/);
|
|
55
|
+
assert.equal(s.lastStdoutOffset, 1);
|
|
56
|
+
assert.equal(s.lastStderrOffset, 1);
|
|
57
|
+
|
|
58
|
+
// Only stderr advances now: stdout cursor must stay put.
|
|
59
|
+
s.stderr.push("err-2\n");
|
|
60
|
+
text = await read();
|
|
61
|
+
assert.doesNotMatch(text, /out-1/);
|
|
62
|
+
assert.match(text, /err-2/);
|
|
63
|
+
assert.equal(s.lastStdoutOffset, 1, "stdout cursor unchanged when only stderr grew");
|
|
64
|
+
assert.equal(s.lastStderrOffset, 2);
|
|
65
|
+
|
|
66
|
+
// Only stdout grows past stderr — stderr must not repeat (the old
|
|
67
|
+
// single-cursor bug showed stderr again whenever stdout was longer).
|
|
68
|
+
s.stdout.push("out-2\n");
|
|
69
|
+
text = await read();
|
|
70
|
+
assert.match(text, /out-2/);
|
|
71
|
+
assert.doesNotMatch(text, /err-1/);
|
|
72
|
+
assert.doesNotMatch(text, /err-2/);
|
|
73
|
+
assert.equal(s.lastStdoutOffset, 2);
|
|
74
|
+
assert.equal(s.lastStderrOffset, 2);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("since:\"start\" never moves the cursors", async () => {
|
|
78
|
+
const { tool, store } = load();
|
|
79
|
+
const s = session({ id: "s1" });
|
|
80
|
+
s.stdout.push("a\nb\n");
|
|
81
|
+
store.add(s);
|
|
82
|
+
await tool.execute("t", { action: "output", id: "s1" }, undefined as never, undefined as never, { cwd: "/" } as never);
|
|
83
|
+
assert.equal(s.lastStdoutOffset, 0);
|
|
84
|
+
assert.equal(s.lastStderrOffset, 0);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("re-clamps a cursor left above a ring-cap-shrunk array and recovers", async () => {
|
|
88
|
+
const { tool, store } = load();
|
|
89
|
+
const s = session({ id: "s1" });
|
|
90
|
+
store.add(s);
|
|
91
|
+
const read = async () => {
|
|
92
|
+
const r = await tool.execute("t", { action: "output", id: "s1", since: "last" }, undefined as never, undefined as never, { cwd: "/" } as never);
|
|
93
|
+
return (r.content[0] as { text: string }).text;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// Fill the ring PAST its cap (1KB lines, two big chunks): enforceCap()
|
|
97
|
+
// rebuilds the array to head + marker + tail (~260 lines). The drain
|
|
98
|
+
// poll reports the truncation and records that line count as the cursor.
|
|
99
|
+
s.stdout.push("HEAD-LINE\n");
|
|
100
|
+
const kbLine = `${"A".repeat(999)}\n`.repeat(420); // ~420KB per chunk
|
|
101
|
+
s.stdout.push(kbLine);
|
|
102
|
+
s.stdout.push(kbLine);
|
|
103
|
+
const text1 = await read();
|
|
104
|
+
assert.match(text1, /truncated/);
|
|
105
|
+
assert.equal(s.lastStdoutOffset, s.stdout.lifetimeLines(), "drain records the lifetime cursor");
|
|
106
|
+
|
|
107
|
+
// Two huge lines re-cap and SHRINK the window far below the recorded
|
|
108
|
+
// index-space position. A window-index cursor (the old design) strands
|
|
109
|
+
// above the shrunk array forever — the lifetime cursor cannot.
|
|
110
|
+
s.stdout.push(`${"x".repeat(99_999)}\n${"y".repeat(99_999)}\n`);
|
|
111
|
+
await read();
|
|
112
|
+
assert.ok(s.lastStdoutOffset > s.stdout.lines().length, "cursor is lifetime space, above the shrunk window (previously the blackout)");
|
|
113
|
+
assert.equal(s.lastStdoutOffset, s.stdout.lifetimeLines(), "cursor stays synced to lifetime lines after the shrink");
|
|
114
|
+
|
|
115
|
+
// Recovery: new pushes must surface again, not "no new output yet".
|
|
116
|
+
for (let i = 0; i < 12; i++) s.stdout.push(`${"NEW-".padEnd(4999, "N")}\n`.repeat(60));
|
|
117
|
+
const text2 = await read();
|
|
118
|
+
assert.match(text2, /NEW-/);
|
|
119
|
+
assert.doesNotMatch(text2, /no new output yet/);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("session_start wiring", () => {
|
|
124
|
+
it("clears the store (no-op bug regression)", async () => {
|
|
125
|
+
const { handlers, store } = load();
|
|
126
|
+
store.add(session({ id: "s1" }));
|
|
127
|
+
(handlers.session_start as (e: unknown, ctx: unknown) => void)({}, {});
|
|
128
|
+
assert.equal(store.list().length, 0);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -25,7 +25,9 @@ import {
|
|
|
25
25
|
function fakeChild(): { child: any; writes: string[]; killCalls: string[]; emitExit: (code: number | null, signal?: NodeJS.Signals | null) => void } {
|
|
26
26
|
const child = new EventEmitter() as any;
|
|
27
27
|
child.pid = 42_424;
|
|
28
|
-
|
|
28
|
+
// stdin must be an EventEmitter: createSession registers an 'error'
|
|
29
|
+
// listener on it (EPIPE containment).
|
|
30
|
+
child.stdin = Object.assign(new EventEmitter(), { write: (d: string) => child.__writes.push(d) });
|
|
29
31
|
child.stdout = new EventEmitter();
|
|
30
32
|
child.stderr = new EventEmitter();
|
|
31
33
|
child.kill = (sig: NodeJS.Signals = "SIGTERM") => {
|
|
@@ -82,6 +84,15 @@ describe("RingBuffer", () => {
|
|
|
82
84
|
it("truncationMarker formats", () => {
|
|
83
85
|
assert.equal(truncationMarker(5), "[… 5 bytes truncated …]");
|
|
84
86
|
});
|
|
87
|
+
|
|
88
|
+
it("keeps the unterminated partial line after the cap trips", () => {
|
|
89
|
+
const rb = new RingBuffer();
|
|
90
|
+
rb.push("HEAD-LINE\n");
|
|
91
|
+
rb.push("x".repeat(TOTAL_BYTES) + "\n"); // cap trips
|
|
92
|
+
rb.push("spinner…"); // no trailing newline, arrives after capping
|
|
93
|
+
assert.equal(rb.isTruncated(), true);
|
|
94
|
+
assert.match(rb.lines().at(-1)!, /^spinner…/);
|
|
95
|
+
});
|
|
85
96
|
});
|
|
86
97
|
|
|
87
98
|
// ── ids + resolution ────────────────────────────────────────────────────────
|
|
@@ -106,7 +117,7 @@ describe("ids and resolveSession", () => {
|
|
|
106
117
|
startedAt: Date.now(),
|
|
107
118
|
stdout: new RingBuffer(),
|
|
108
119
|
stderr: new RingBuffer(),
|
|
109
|
-
|
|
120
|
+
lastStdoutOffset: 0, lastStderrOffset: 0,
|
|
110
121
|
...over,
|
|
111
122
|
};
|
|
112
123
|
}
|
|
@@ -128,6 +139,24 @@ describe("ids and resolveSession", () => {
|
|
|
128
139
|
assert.throws(() => resolveSession(store, "dev"), /ambiguous.*dev/s);
|
|
129
140
|
assert.throws(() => resolveSession(store, "nope"), /no session 'nope'/);
|
|
130
141
|
});
|
|
142
|
+
|
|
143
|
+
it("prefers a live match over an exited one with the same name", () => {
|
|
144
|
+
const store = new SessionStore();
|
|
145
|
+
const dead = session({ id: "s1", name: "dev", exitedAt: Date.now(), exitCode: 0 });
|
|
146
|
+
const live = session({ id: "s2", name: "dev" });
|
|
147
|
+
store.add(dead);
|
|
148
|
+
store.add(live);
|
|
149
|
+
assert.equal(resolveSession(store, "dev"), live);
|
|
150
|
+
// Ambiguity is judged among LIVE sessions only.
|
|
151
|
+
const live2 = session({ id: "s3", name: "dev" });
|
|
152
|
+
store.add(live2);
|
|
153
|
+
assert.throws(() => resolveSession(store, "dev"), /ambiguous \(2 sessions\)/);
|
|
154
|
+
// All matches exited → fall back to the FULL list (original semantics:
|
|
155
|
+
// ambiguity is decided over the fallback set).
|
|
156
|
+
live.exitedAt = Date.now();
|
|
157
|
+
live2.exitedAt = Date.now();
|
|
158
|
+
assert.throws(() => resolveSession(store, "dev"), /ambiguous \(3 sessions\)/);
|
|
159
|
+
});
|
|
131
160
|
});
|
|
132
161
|
|
|
133
162
|
// ── SessionStore ────────────────────────────────────────────────────────────
|
|
@@ -143,7 +172,7 @@ describe("SessionStore", () => {
|
|
|
143
172
|
startedAt: Date.now(),
|
|
144
173
|
stdout: new RingBuffer(),
|
|
145
174
|
stderr: new RingBuffer(),
|
|
146
|
-
|
|
175
|
+
lastStdoutOffset: 0, lastStderrOffset: 0,
|
|
147
176
|
...over,
|
|
148
177
|
};
|
|
149
178
|
}
|
|
@@ -168,18 +197,26 @@ describe("SessionStore", () => {
|
|
|
168
197
|
assert.equal(store.list()[0]!.id, "d2"); // oldest two gone
|
|
169
198
|
});
|
|
170
199
|
|
|
200
|
+
it("clear drops every session", () => {
|
|
201
|
+
const store = new SessionStore();
|
|
202
|
+
store.add(live({ id: "s1" }));
|
|
203
|
+
store.add(live({ id: "s2" }));
|
|
204
|
+
store.clear();
|
|
205
|
+
assert.equal(store.list().length, 0);
|
|
206
|
+
});
|
|
207
|
+
|
|
171
208
|
it("killAll skips exited sessions and escalates for live ones", { timeout: 10_000 }, async () => {
|
|
172
209
|
const store = new SessionStore();
|
|
173
210
|
const fc = fakeChild();
|
|
174
211
|
const s = store.list().length; // 0
|
|
175
212
|
store.add({
|
|
176
213
|
id: "s1", pid: fc.child.pid, child: fc.child, cwd: "/", command: "x",
|
|
177
|
-
startedAt: Date.now(), stdout: new RingBuffer(), stderr: new RingBuffer(),
|
|
214
|
+
startedAt: Date.now(), stdout: new RingBuffer(), stderr: new RingBuffer(), lastStdoutOffset: 0, lastStderrOffset: 0,
|
|
178
215
|
});
|
|
179
216
|
store.add({
|
|
180
217
|
id: "s2", pid: 1, child: fakeChild().child, cwd: "/", command: "x",
|
|
181
218
|
startedAt: Date.now(), exitedAt: Date.now(), exitCode: 0,
|
|
182
|
-
stdout: new RingBuffer(), stderr: new RingBuffer(),
|
|
219
|
+
stdout: new RingBuffer(), stderr: new RingBuffer(), lastStdoutOffset: 0, lastStderrOffset: 0,
|
|
183
220
|
});
|
|
184
221
|
await store.killAll("SIGTERM");
|
|
185
222
|
// Group signal fails (bogus pid) → falls back to child.kill; escalation
|
|
@@ -218,6 +255,78 @@ describe("createSession", () => {
|
|
|
218
255
|
assert.equal(store.list().length, 0);
|
|
219
256
|
});
|
|
220
257
|
|
|
258
|
+
it("marks an async spawn failure exited and never signals pi's own group", async () => {
|
|
259
|
+
const store = new SessionStore();
|
|
260
|
+
resetIdCounter(0);
|
|
261
|
+
// Async failure: child emits 'error' with pid undefined (bad cwd etc.).
|
|
262
|
+
const fc = fakeChild();
|
|
263
|
+
fc.child.pid = undefined;
|
|
264
|
+
const s = createSession(store, () => {
|
|
265
|
+
queueMicrotask(() => fc.child.emit("error", new Error("ENOENT: spawn bad-cwd")));
|
|
266
|
+
return fc.child as never;
|
|
267
|
+
}, { command: "x", cwd: "/nonexistent-cwd" });
|
|
268
|
+
assert.equal(s.pid, 0);
|
|
269
|
+
await new Promise<void>((r) => setImmediate(r));
|
|
270
|
+
assert.ok(s.exitedAt !== undefined, "spawn failure marks the session exited");
|
|
271
|
+
assert.equal(s.exitCode, null);
|
|
272
|
+
|
|
273
|
+
// kill on the zombie resolves early — nothing to signal, and never
|
|
274
|
+
// process.kill(-0) (== kill(0) == SIGTERM for every process in pi's
|
|
275
|
+
// own group).
|
|
276
|
+
const origKill = process.kill;
|
|
277
|
+
const groupCalls: [number | undefined, NodeJS.Signals][] = [];
|
|
278
|
+
(process as any).kill = (pid: number | undefined, sig?: NodeJS.Signals) => {
|
|
279
|
+
if (typeof pid === "number" && pid <= 0) groupCalls.push([pid, sig as NodeJS.Signals]);
|
|
280
|
+
return true;
|
|
281
|
+
};
|
|
282
|
+
try {
|
|
283
|
+
const r = await killSession(s, "SIGKILL", 50);
|
|
284
|
+
assert.deepEqual(r, { code: null, signal: null }); // early exit, no signal
|
|
285
|
+
assert.deepEqual(groupCalls, [], "no negative/zero-pid group signal may be issued");
|
|
286
|
+
} finally {
|
|
287
|
+
(process as any).kill = origKill;
|
|
288
|
+
}
|
|
289
|
+
assert.deepEqual(fc.killCalls, [] as string[], "exited session is not signalled");
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("kill on a live pid-0 session falls back to child.kill, not the host group", async () => {
|
|
293
|
+
const store = new SessionStore();
|
|
294
|
+
// Race window: session registered, spawn 'error' not yet fired → still
|
|
295
|
+
// live with pid 0. killSession must reach for child.kill only.
|
|
296
|
+
const fc = fakeChild();
|
|
297
|
+
fc.child.pid = undefined;
|
|
298
|
+
const s = createSession(store, () => fc.child as never, { command: "x", cwd: "/" });
|
|
299
|
+
assert.equal(s.pid, 0);
|
|
300
|
+
const origKill = process.kill;
|
|
301
|
+
const groupCalls: [number | undefined, NodeJS.Signals][] = [];
|
|
302
|
+
(process as any).kill = (pid: number | undefined, sig?: NodeJS.Signals) => {
|
|
303
|
+
if (typeof pid === "number" && pid <= 0) groupCalls.push([pid, sig as NodeJS.Signals]);
|
|
304
|
+
return true;
|
|
305
|
+
};
|
|
306
|
+
try {
|
|
307
|
+
const r = await killSession(s, "SIGTERM", 50);
|
|
308
|
+
assert.equal(r.signal, "SIGKILL"); // escalated: fake never exits
|
|
309
|
+
assert.deepEqual(groupCalls, [], "no negative/zero-pid group signal may be issued");
|
|
310
|
+
} finally {
|
|
311
|
+
(process as any).kill = origKill;
|
|
312
|
+
}
|
|
313
|
+
assert.ok(fc.killCalls.includes("SIGTERM"));
|
|
314
|
+
assert.ok(fc.killCalls.includes("SIGKILL"), "falls back to child.kill");
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("frees the name after an async spawn failure", async () => {
|
|
318
|
+
const store = new SessionStore();
|
|
319
|
+
const fc = fakeChild();
|
|
320
|
+
fc.child.pid = undefined;
|
|
321
|
+
createSession(store, () => {
|
|
322
|
+
queueMicrotask(() => fc.child.emit("error", new Error("ENOENT")));
|
|
323
|
+
return fc.child as never;
|
|
324
|
+
}, { command: "x", cwd: "/", name: "dev" });
|
|
325
|
+
await new Promise<void>((r) => setImmediate(r));
|
|
326
|
+
const again = createSession(store, noopSpawn, { command: "y", cwd: "/", name: "dev" });
|
|
327
|
+
assert.ok(again.id.length > 0);
|
|
328
|
+
});
|
|
329
|
+
|
|
221
330
|
it("killSession escalates to SIGKILL after the wait", async () => {
|
|
222
331
|
const store = new SessionStore();
|
|
223
332
|
const fc = fakeChild(); // never emits exit on its own
|
|
@@ -237,6 +346,19 @@ describe("createSession", () => {
|
|
|
237
346
|
const r = await killSession(s);
|
|
238
347
|
assert.equal(r.code, 3);
|
|
239
348
|
});
|
|
349
|
+
|
|
350
|
+
it("contains a stdin EPIPE that races child exit (no uncaught error)", () => {
|
|
351
|
+
const store = new SessionStore();
|
|
352
|
+
const fc = fakeChild();
|
|
353
|
+
const s = createSession(store, () => fc.child as never, { command: "sh -c 'exit 0'", cwd: "/" });
|
|
354
|
+
fc.emitExit(0);
|
|
355
|
+
// A write that raced the exit already EPIPEd: the 'error' event lands on
|
|
356
|
+
// stdin with no writer left. On a real child that would be an
|
|
357
|
+
// uncaughtException killing pi; on an EventEmitter, emitting 'error'
|
|
358
|
+
// with no listener THROWS — so doesNotThrow is the no-crash assertion.
|
|
359
|
+
assert.doesNotThrow(() => fc.child.stdin.emit("error", new Error("EPIPE")));
|
|
360
|
+
assert.deepEqual(s.stderr.lines(), ["[shells] stdin write failed (process exited)"]);
|
|
361
|
+
});
|
|
240
362
|
});
|
|
241
363
|
|
|
242
364
|
// ── REAL spawn integration (posix) ──────────────────────────────────────────
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import { readFileSync } from "node:fs";
|
|
11
11
|
import path from "node:path";
|
|
12
|
-
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
13
13
|
|
|
14
14
|
// ── Shapes ──────────────────────────────────────────────────────────────────
|
|
15
15
|
|
|
@@ -61,7 +61,9 @@ export async function loadPdfEngine(): Promise<PdfEngine> {
|
|
|
61
61
|
const file = path.join(path.dirname(fileURLToPath(import.meta.url)), "../vendor/pdfjs/pdf.min.mjs");
|
|
62
62
|
// Lazily dynamic-import the vendored module by FILE URL so the bundler /
|
|
63
63
|
// typechecker never sees a static dependency.
|
|
64
|
-
|
|
64
|
+
// pathToFileURL (not string concat): spaces/`#` in the install path must
|
|
65
|
+
// be %-encoded or the lazy pdf.js import resolves a broken URL.
|
|
66
|
+
const mod = (await import(pathToFileURL(file).href)) as PdfEngine & Record<string, unknown>;
|
|
65
67
|
cachedEngine = mod;
|
|
66
68
|
return mod;
|
|
67
69
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// read_pdf unit tests — C12 regression: the lazy pdf.js engine import must go
|
|
2
|
+
// through pathToFileURL (an unencoded `file://` string breaks on spaces in
|
|
3
|
+
// the install path). Verified by loading the vendored engine from a vendor
|
|
4
|
+
// directory whose name carries a space — no valid PDF is needed: the import
|
|
5
|
+
// resolving at all is the regression, so getDocument is never asserted.
|
|
6
|
+
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { cpSync, mkdtempSync, rmSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { after, before, describe, it } from "node:test";
|
|
12
|
+
import { itemsToLines, parsePagesParam, resetPdfEngineCache, formatPdfText } from "../lib/pdf.js";
|
|
13
|
+
import type { PdfEngine } from "../lib/pdf.js";
|
|
14
|
+
|
|
15
|
+
const dir = mkdtempSync(path.join(tmpdir(), "ceulen-web-pdf-"));
|
|
16
|
+
after(() => rmSync(dir, { recursive: true, force: true }));
|
|
17
|
+
|
|
18
|
+
// The vendored pdfjs (source of truth for the copy).
|
|
19
|
+
const vendorPdfjs = path.join(
|
|
20
|
+
path.dirname(new URL(import.meta.url).pathname.replace(/^\/([^/]:)/, "$1")),
|
|
21
|
+
"../vendor/pdfjs",
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
describe("pdf engine load (C12)", () => {
|
|
25
|
+
// A spaced vendor dir: `file://` + raw path would split at the space and
|
|
26
|
+
// fail the dynamic import; pathToFileURL percent-encodes it.
|
|
27
|
+
const spacedVendor = path.join(dir, "vendor space", "vendor", "pdfjs");
|
|
28
|
+
|
|
29
|
+
before(() => {
|
|
30
|
+
cpSync(vendorPdfjs, spacedVendor, { recursive: true });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("loadPdfEngine resolves from an install path containing a space", async () => {
|
|
34
|
+
// Point the module at the spaced copy: loadPdfEngine derives the vendor
|
|
35
|
+
// path from import.meta.url, so the test re-imports the lib FROM the
|
|
36
|
+
// spaced directory (mirroring the module layout: lib/ beside vendor/).
|
|
37
|
+
const libCopy = path.join(dir, "vendor space", "lib");
|
|
38
|
+
cpSync(path.join(vendorPdfjs, "..", "..", "lib", "pdf.ts"), path.join(libCopy, "pdf.ts"));
|
|
39
|
+
const mod = await import(`file://${encodeURI(libCopy)}/pdf.ts`).catch(() => null);
|
|
40
|
+
assert.ok(mod, "spaced-path lib import resolves");
|
|
41
|
+
const engine: PdfEngine = await mod!.loadPdfEngine();
|
|
42
|
+
assert.ok(engine, "engine resolved through the encoded file URL");
|
|
43
|
+
assert.equal(typeof engine.getDocument, "function");
|
|
44
|
+
// getDocument is deliberately NOT called here — a real PDF is out of
|
|
45
|
+
// scope for this regression; the import resolving IS the fix proof.
|
|
46
|
+
resetPdfEngineCache();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("pdf formatting helpers", () => {
|
|
51
|
+
it("itemsToLines groups by baseline y, sorts by x, joins runs", () => {
|
|
52
|
+
const lines = itemsToLines([
|
|
53
|
+
{ str: "world", transform: [1, 0, 0, 1, 120, 100] },
|
|
54
|
+
{ str: "hello", transform: [1, 0, 0, 1, 10, 100] },
|
|
55
|
+
{ str: "next", transform: [1, 0, 0, 1, 10, 90] },
|
|
56
|
+
{ str: "", transform: [1, 0, 0, 1, 0, 80] },
|
|
57
|
+
]);
|
|
58
|
+
assert.deepEqual(lines, ["hello world", "next"]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("parsePagesParam: default first 5, ranges, cap 10, out-of-range dropped", () => {
|
|
62
|
+
assert.deepEqual(parsePagesParam(undefined, 12), [1, 2, 3, 4, 5]);
|
|
63
|
+
assert.deepEqual(parsePagesParam("3", 12), [3]);
|
|
64
|
+
assert.deepEqual(parsePagesParam("2,4,6-8", 12), [2, 4, 6, 7, 8]);
|
|
65
|
+
assert.deepEqual(parsePagesParam("1-3", 2), [1, 2], "clamped to the document");
|
|
66
|
+
assert.throws(() => parsePagesParam("abc", 5), /invalid pages spec/);
|
|
67
|
+
assert.throws(() => parsePagesParam("40-50", 5), /no valid pages/);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("formatPdfText notes empty pages", () => {
|
|
71
|
+
const out = formatPdfText({
|
|
72
|
+
path: "x.pdf",
|
|
73
|
+
totalPages: 2,
|
|
74
|
+
pages: [
|
|
75
|
+
{ page: 1, lines: ["text"], chars: 4 },
|
|
76
|
+
{ page: 2, lines: [], chars: 0 },
|
|
77
|
+
],
|
|
78
|
+
emptyPages: [2],
|
|
79
|
+
});
|
|
80
|
+
assert.match(out, /2 page\(s\), showing 2/);
|
|
81
|
+
assert.match(out, /no extractable text/);
|
|
82
|
+
assert.match(out, /\[empty pages: 2\]/);
|
|
83
|
+
});
|
|
84
|
+
});
|
package/package.json
CHANGED