-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.ts
More file actions
307 lines (267 loc) · 9.37 KB
/
utils.test.ts
File metadata and controls
307 lines (267 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { describe, expect, test } from "bun:test";
import type { App, TFile } from "obsidian";
import {
getContent,
joinTokens,
splitIntoTokens,
truncateHeading,
truncateHeadOnly,
truncateHeadTail,
updateFrontMatter,
} from "./utils";
function makeApp(initial: Record<string, unknown> = {}): {
app: App;
fm: Record<string, unknown>;
} {
const fm = { ...initial };
const app = {
fileManager: {
processFrontMatter: async (
_file: unknown,
fn: (fm: Record<string, unknown>) => void,
) => {
fn(fm);
},
},
} as unknown as App;
return { app, fm };
}
describe("splitIntoTokens", () => {
test("splits English words", () => {
expect(splitIntoTokens("hello world")).toEqual(["hello", "world"]);
});
test("splits CJK characters individually", () => {
expect(splitIntoTokens("你好世界")).toEqual(["你", "好", "世", "界"]);
});
test("handles mixed English and CJK", () => {
expect(splitIntoTokens("hello你好world")).toEqual([
"hello",
"你",
"好",
"world",
]);
});
test("captures punctuation as separate tokens", () => {
expect(splitIntoTokens("hello, world!")).toEqual([
"hello",
",",
"world",
"!",
]);
});
test("captures newlines as tokens", () => {
expect(splitIntoTokens("hello\nworld")).toEqual(["hello", "\n", "world"]);
});
test("returns empty array for empty string", () => {
expect(splitIntoTokens("")).toEqual([]);
});
test("returns empty array for whitespace-only string", () => {
expect(splitIntoTokens(" ")).toEqual([]);
});
test("captures hash as punctuation token", () => {
expect(splitIntoTokens("# heading")).toEqual(["#", "heading"]);
});
test("handles CJK punctuation", () => {
expect(splitIntoTokens("你好,世界!")).toEqual([
"你",
"好",
",",
"世",
"界",
"!",
]);
});
});
describe("joinTokens", () => {
test("joins English words with spaces", () => {
expect(joinTokens(["hello", "world"])).toBe("hello world");
});
test("joins CJK characters without spaces", () => {
expect(joinTokens(["你", "好", "世", "界"])).toBe("你好世界");
});
test("attaches punctuation without leading space", () => {
expect(joinTokens(["hello", ",", "world"])).toBe("hello, world");
});
test("preserves newlines", () => {
expect(joinTokens(["hello", "\n", "world"])).toBe("hello\nworld");
});
test("returns empty string for empty array", () => {
expect(joinTokens([])).toBe("");
});
test("handles mixed content", () => {
expect(joinTokens(["hello", "你", "好", "world"])).toBe("hello你好 world");
});
test("handles single token", () => {
expect(joinTokens(["hello"])).toBe("hello");
});
});
describe("truncateHeadOnly", () => {
test("returns first N tokens with ellipsis", () => {
const tokens = ["one", "two", "three", "four", "five"];
expect(truncateHeadOnly(tokens, 3)).toBe("one two three...");
});
test("handles limit of 1", () => {
const tokens = ["one", "two", "three"];
expect(truncateHeadOnly(tokens, 1)).toBe("one...");
});
test("returns all tokens when limit equals length", () => {
const tokens = ["one", "two", "three"];
expect(truncateHeadOnly(tokens, 3)).toBe("one two three");
});
test("returns all tokens when limit exceeds length", () => {
const tokens = ["one", "two"];
expect(truncateHeadOnly(tokens, 10)).toBe("one two");
});
});
describe("truncateHeadTail", () => {
test("returns full content when limit covers all tokens", () => {
const tokens = Array.from({ length: 10 }, (_, i) => `t${i}`);
const result = truncateHeadTail(tokens, 10);
expect(result).toBe(joinTokens(tokens));
expect(result).not.toContain("...");
});
test("returns full content when limit exceeds token count", () => {
const tokens = ["a", "b", "c"];
const result = truncateHeadTail(tokens, 100);
expect(result).toBe(joinTokens(tokens));
expect(result).not.toContain("...");
});
test("handles small limit", () => {
const tokens = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
const result = truncateHeadTail(tokens, 5);
// 80% of 5 = 4, 20% of 5 = 1
expect(result).toContain("a");
expect(result).toContain("\n...\n");
});
test("handles limit of 1", () => {
const tokens = ["a", "b", "c", "d", "e"];
const result = truncateHeadTail(tokens, 1);
// All budget goes to head, tail is empty — no separator
expect(result).toBe("a");
expect(result).not.toContain("\n...\n");
});
});
describe("truncateHeading", () => {
test("extracts headings and first paragraph", () => {
const content =
"# Title\nSome paragraph text here for testing.\n\n## Section\nMore text in section.";
const tokens = splitIntoTokens(content);
const result = truncateHeading(content, tokens, 1000);
expect(result).toContain("# Title");
expect(result).toContain("## Section");
expect(result).toContain("Outline:");
expect(result).toContain("Body:");
});
test("truncates when outline exceeds limit", () => {
const content = "# H1\nParagraph one.\n# H2\nParagraph two.";
const tokens = splitIntoTokens(content);
// Very small limit to force truncation of the outline itself
const result = truncateHeading(content, tokens, 2);
const resultTokens = splitIntoTokens(result);
// Output should be truncated to at most the token limit
expect(resultTokens.length).toBeLessThanOrEqual(2);
// Should not contain the Outline:/Body: wrapper since outline exceeded limit
expect(result).not.toContain("Outline:");
expect(result).not.toContain("Body:");
});
test("filters empty lines", () => {
const content = "# Title\n\n\n\nParagraph after blanks.";
const tokens = splitIntoTokens(content);
const result = truncateHeading(content, tokens, 1000);
expect(result).toContain("# Title");
});
test("does not append ellipsis to short paragraphs", () => {
const content = "# Title\nShort paragraph.";
const tokens = splitIntoTokens(content);
const result = truncateHeading(content, tokens, 1000);
// "Short paragraph." is < 30 tokens, should not get "..."
expect(result).toContain("Short paragraph.");
expect(result).not.toMatch(/Short paragraph\.\.\.\./);
});
test("omits body when outline consumes entire budget", () => {
// Outline for "# A\nword" is ["# A", "word"] → "# A\nword"
// tokenized: ["#", "A", "\n", "word"] = 4 tokens
// limit=4 → remainingTokens=0 → no body section
const content = "# A\nword";
const tokens = splitIntoTokens(content);
const result = truncateHeading(content, tokens, 4);
expect(result).toContain("Outline:");
expect(result).not.toContain("Body:");
});
test("handles content with no headings", () => {
const content = "Just a plain paragraph with no headings at all.";
const tokens = splitIntoTokens(content);
const result = truncateHeading(content, tokens, 1000);
// No headings → empty outline, all budget goes to body
expect(result).toContain("Body:");
});
});
describe("updateFrontMatter", () => {
test("keep: preserves an existing value", async () => {
const { app, fm } = makeApp({ description: "existing" });
await updateFrontMatter(
{} as TFile,
app,
"description",
"new value",
"keep",
);
expect(fm.description).toBe("existing");
});
test("keep: sets the value when field is absent", async () => {
const { app, fm } = makeApp({});
await updateFrontMatter(
{} as TFile,
app,
"description",
"new value",
"keep",
);
expect(fm.description).toBe("new value");
});
test("update: overwrites an existing value", async () => {
const { app, fm } = makeApp({ description: "old" });
await updateFrontMatter(
{} as TFile,
app,
"description",
"new value",
"update",
);
expect(fm.description).toBe("new value");
});
test("append: merges and deduplicates array oldValue", async () => {
const { app, fm } = makeApp({ tags: ["a", "b"] });
await updateFrontMatter({} as TFile, app, "tags", ["b", "c"], "append");
expect(fm.tags).toEqual(["a", "b", "c"]);
});
test("append: normalises string oldValue to array before merge", async () => {
const { app, fm } = makeApp({ tags: "existing-tag" });
await updateFrontMatter({} as TFile, app, "tags", ["new-tag"], "append");
expect(fm.tags).toEqual(["existing-tag", "new-tag"]);
});
test("append: initialises correctly when field is absent", async () => {
const { app, fm } = makeApp({});
await updateFrontMatter({} as TFile, app, "tags", ["a", "b"], "append");
expect(fm.tags).toEqual(["a", "b"]);
});
});
describe("getContent", () => {
function makeVaultApp(content: string) {
return {
vault: { read: async () => content },
} as unknown as App;
}
test("returns full content when limit is 0", async () => {
const content = "Hello world, this is some content.";
const app = makeVaultApp(content);
const result = await getContent(app, {} as TFile, 0);
expect(result).toBe(content);
});
test("returns full content when limit is negative", async () => {
const content = "Hello world, this is some content.";
const app = makeVaultApp(content);
const result = await getContent(app, {} as TFile, -1);
expect(result).toBe(content);
});
});