Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFSP from "node:fs/promises";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import { expect, it } from "vite-plus/test";
6import { extractDocumentText } from "./documentTextWorker.ts";
7β
8const cellHeading = "## Cell 1 (code)\n";
9const outputHeading = "\nOutput:\n";
10β
11it.each([
12 {
13 name: "omits the output heading when only half an emoji fits after it",
14 source: "x".repeat(1_000_000 - cellHeading.length - outputHeading.length - 1),
15 outputs: ["π"],
16 remainder: "\n\n[Extracted text truncated at document extraction limits.]",
17 },
18 {
19 name: "keeps reading after a high surrogate in an output below the limit",
20 source: "run()",
21 outputs: ["\uD83D", "\uDE00"],
22 remainder: "\nOutput:\n\uD83D\n\uDE00\n\n## Cell 2 (markdown)\nLater cell",
23 },
24 {
25 name: "counts the stream's own trailing newline before the next cell",
26 source: "run()",
27 outputs: ["Finished\n"],
28 remainder: "\nOutput:\nFinished\n\n## Cell 2 (markdown)\nLater cell",
29 },
30 {
31 name: "preserves saved blank lines that exceed the gap before the next cell",
32 source: "run()",
33 outputs: ["Finished\n\n\n"],
34 remainder: "\nOutput:\nFinished\n\n\n## Cell 2 (markdown)\nLater cell",
35 },
36 {
37 name: "keeps all saved trailing blank lines in the final stream",
38 source: "run()",
39 outputs: ["Finished\n\n\n"],
40 lastCell: true,
41 remainder: "\nOutput:\nFinished\n\n\n",
42 },
43])("$name", async ({ source, outputs, remainder, lastCell }) => {
44 const folder = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-notebook-budget-"));
45 try {
46 const path = NodePath.join(folder, "output.ipynb");
47 await NodeFSP.writeFile(
48 path,
49 JSON.stringify({
50 cells: [
51 {
52 cell_type: "code",
53 source,
54 outputs: outputs.map((text, index) => ({
55 output_type: "stream",
56 name: index === 0 ? "stdout" : "stderr",
57 text,
58 })),
59 },
60 ...(lastCell ? [] : [{ cell_type: "markdown", source: "Later cell" }]),
61 ],
62 }),
63 );
64 const extracted = await extractDocumentText(path, ".ipynb");
65 expect(extracted).toBe(cellHeading + source + remainder);
66 } finally {
67 await NodeFSP.rm(folder, { recursive: true, force: true });
68 }
69});