-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.ts
435 lines (373 loc) · 13.1 KB
/
main_test.ts
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import { describe, it } from "jsr:@std/testing/bdd";
import { expect } from "jsr:@std/expect";
import {
organizeFileWithSlashEncoding,
removeCollapsedBlocks,
replaceNumberedLists,
replacePageProperties,
replaceTagsForLinks,
replaceTODOs,
updateRelativePaths,
removeLogbooks,
} from "./main.ts";
describe("replaceTODOs", () => {
it("replaces a single TODO for standard markdown syntax", () => {
const result = replaceTODOs(`- TODO foo bar`);
expect(result).toBe(`- [ ] foo bar`);
});
it("replaces a single DOING for standard markdown syntax", () => {
const result = replaceTODOs(`- DOING foo bar`);
expect(result).toBe(`- [ ] foo bar`);
});
it("replaces a single DONE for standard markdown syntax", () => {
const result = replaceTODOs(`- DONE foo bar`);
expect(result).toBe(`- [x] foo bar`);
});
it("replaces multiple DONE for standard markdown syntax", () => {
const result = replaceTODOs(`
- DONE foo
- DONE bar
- DONE baz
`);
const want = `
- [x] foo
- [x] bar
- [x] baz
`;
expect(result).toBe(want);
});
it("replaces multiple mixed statuses for standard markdown syntax", () => {
const result = replaceTODOs(`
- DONE foo
- TODO bar
- DONE baz
- DOING rop
`);
const want = `
- [x] foo
- [ ] bar
- [x] baz
- [ ] rop
`;
expect(result).toBe(want);
});
it("replaces nested mixed statuses for standard markdown syntax", () => {
const result = replaceTODOs(`
- DONE foo
- TODO bar
- DONE baz
- DOING rop
`);
const want = `
- [x] foo
- [ ] bar
- [x] baz
- [ ] rop
`;
expect(result).toBe(want);
});
});
describe("replaceTagsForLinks", () => {
it("tags without a line ending break", () => {
const result = replaceTagsForLinks(`#foo`);
expect(result).toBe(`[[foo`);
});
it("replaces a logseq tag for an obsidian link", () => {
const result = replaceTagsForLinks(`#foo `);
expect(result).toBe(`[[foo]] `);
});
it("replaces a complex logseq tag for an obsidian link", () => {
const result = replaceTagsForLinks(`#[[foo]] `);
expect(result).toBe(`[[foo]] `);
});
it("leaves the hash untouched if it's led by non-white characters", () => {
const result = replaceTagsForLinks(`foobar#foo `);
expect(result).toBe(`foobar#foo `);
});
it("leaves the hash untouched if it's led by non-white characters", () => {
const result = replaceTagsForLinks(`foobar#foo `);
expect(result).toBe(`foobar#foo `);
});
it("leaves the hash untouched if the next charactar is a whitespace", () => {
const result = replaceTagsForLinks(`# foo `);
expect(result).toBe(`# foo `);
});
it("leaves the hash untouched if the next charactar is another hash", () => {
const result = replaceTagsForLinks(`## foo `);
expect(result).toBe(`## foo `);
const result2 = replaceTagsForLinks(`##foo `);
expect(result2).toBe(`##foo `);
});
it("understand nested tags", () => {
const result = replaceTagsForLinks(`#foo/bar `);
expect(result).toBe(`[[foo/bar]] `);
});
it("understand tags with dots", () => {
const result = replaceTagsForLinks(`#google.com `);
expect(result).toBe(`[[google.com]] `);
});
[",", ";", ":", "?", "!", "\\"].forEach((x) => {
it(`understand tags separated by ${x}`, () => {
const result = replaceTagsForLinks(`#foo${x} #bar `);
expect(result).toBe(`[[foo]]${x} [[bar]] `);
});
it(`doesn't understand tags separated by ${x} without the proper whitespace`, () => {
const result = replaceTagsForLinks(`#foo${x}#bar `);
expect(result).toBe(`[[foo]]${x}#bar `);
});
});
});
describe("replaceNumberedLists", () => {
it("replaces numbered lists", () => {
const input = `
- How can we help?
- They have three key issues:
- How they do [[things]]
logseq.order-list-type:: number
- They don't want to have to own their own [[thing]]
logseq.order-list-type:: number
- Granting users access is painful and buggy
logseq.order-list-type:: number
- Another thing
`;
const want = `
- How can we help?
- They have three key issues:
1. How they do [[things]]
2. They don't want to have to own their own [[thing]]
3. Granting users access is painful and buggy
- Another thing
`;
const result = replaceNumberedLists(input);
expect(result).toBe(want);
});
it("replaces multiple numbered lists in the same text", () => {
const input = `
- How can we help?
- They have three key issues:
- How they do [[things]]
logseq.order-list-type:: number
- They don't want to have to own their own [[thing]]
logseq.order-list-type:: number
- Granting users access is painful and buggy
logseq.order-list-type:: number
- Another thing
- How can we help 2?
- They have three key issues:
- How they do [[things]]
logseq.order-list-type:: number
- They don't want to have to own their own [[thing]]
logseq.order-list-type:: number
- Granting users access is painful and buggy
logseq.order-list-type:: number
- Another thing
`;
const want = `
- How can we help?
- They have three key issues:
1. How they do [[things]]
2. They don't want to have to own their own [[thing]]
3. Granting users access is painful and buggy
- Another thing
- How can we help 2?
- They have three key issues:
1. How they do [[things]]
2. They don't want to have to own their own [[thing]]
3. Granting users access is painful and buggy
- Another thing
`;
const result = replaceNumberedLists(input);
expect(result).toBe(want);
});
});
describe("organizeFileWithSlashEncoding", () => {
it("reorganizes a file path with %2F encoding", () => {
const result = organizeFileWithSlashEncoding("docker%2Fgitops.md");
expect(result).toEqual({
newPath: "docker/gitops.md",
targetDir: "docker",
shouldMove: true,
});
});
it("handles multiple %2F encodings", () => {
const result = organizeFileWithSlashEncoding(
"infrastructure%2Fkubernetes%2Fhelm.md",
);
expect(result).toEqual({
newPath: "infrastructure/kubernetes/helm.md",
targetDir: "infrastructure/kubernetes",
shouldMove: true,
});
});
it("leaves normal paths unchanged", () => {
const result = organizeFileWithSlashEncoding("normal-file.md");
expect(result).toEqual({
newPath: "normal-file.md",
shouldMove: false,
});
});
});
describe("replacePageProperties", () => {
it("replaces page properties for standard markdown frontmatter", () => {
const input = `habit-tracker:: true
wake-up:: 08:00h
morning-notes:: not hungry, fasted to keep alert.
breakfast:: had a late tortilla with coffee at 10.30h
lunch:: risotto with BBQ leftovers
dinner:: cabolo nero, tatoes and boiled eggs.
evening-notes:: left dinner slightly hungry, yet fine. Feeling quite tired at 21h.
bed-time:: 22.30h
reading:: 1h
physical-activity:: none, just sharpened an axe.
- {{embed ((64156f89-61ed-4d06-a326-f01ff0b93759))}}
- [[idea]] might be able to leverage [this repository](https://github.com/lxy1993/mac-network-switch/tree/master) to create a tool to switch from ethernet to wifi on the fly.
- I’ve been reading a bit of [[Critical Conversations]] and it explained how [[contrasting]] can be used to provide context and proportion when giving [[feedback]]. I think this might be a fantastic way to frame our yearly 360 reviews -> apart from potentially being clearer, it builds that safety.
`;
const want = `---
habit-tracker: true
wake-up: 08:00h
morning-notes: not hungry, fasted to keep alert.
breakfast: had a late tortilla with coffee at 10.30h
lunch: risotto with BBQ leftovers
dinner: cabolo nero, tatoes and boiled eggs.
evening-notes: left dinner slightly hungry, yet fine. Feeling quite tired at 21h.
bed-time: 22.30h
reading: 1h
physical-activity: none, just sharpened an axe.
---
- {{embed ((64156f89-61ed-4d06-a326-f01ff0b93759))}}
- [[idea]] might be able to leverage [this repository](https://github.com/lxy1993/mac-network-switch/tree/master) to create a tool to switch from ethernet to wifi on the fly.
- I’ve been reading a bit of [[Critical Conversations]] and it explained how [[contrasting]] can be used to provide context and proportion when giving [[feedback]]. I think this might be a fantastic way to frame our yearly 360 reviews -> apart from potentially being clearer, it builds that safety.
`;
const result = replacePageProperties(input);
expect(result).toBe(want);
});
it("trims empty leading lines", () => {
const input = `
habit-tracker:: true
wake-up:: 08:00h
- {{embed ((64156f89-61ed-4d06-a326-f01ff0b93759))}}
- [[idea]] might be able to leverage [this repository](https://github.com/lxy1993/mac-network-switch/tree/master) to create a tool to switch from ethernet to wifi on the fly.
- I’ve been reading a bit of [[Critical Conversations]] and it explained how [[contrasting]] can be used to provide context and proportion when giving [[feedback]]. I think this might be a fantastic way to frame our yearly 360 reviews -> apart from potentially being clearer, it builds that safety.
`;
const want = `---
habit-tracker: true
wake-up: 08:00h
---
- {{embed ((64156f89-61ed-4d06-a326-f01ff0b93759))}}
- [[idea]] might be able to leverage [this repository](https://github.com/lxy1993/mac-network-switch/tree/master) to create a tool to switch from ethernet to wifi on the fly.
- I’ve been reading a bit of [[Critical Conversations]] and it explained how [[contrasting]] can be used to provide context and proportion when giving [[feedback]]. I think this might be a fantastic way to frame our yearly 360 reviews -> apart from potentially being clearer, it builds that safety.
`;
const result = replacePageProperties(input);
expect(result).toBe(want);
});
it("transforms logseq links to plain text", () => {
const input = `
a-list:: [[something]], [[other]]
b-list:: [[just-one]]
c-list:: correct
- {{embed ((64156f89-61ed-4d06-a326-f01ff0b93759))}}
`;
const want = `---
a-list: something, other
b-list: just-one
c-list: correct
---
- {{embed ((64156f89-61ed-4d06-a326-f01ff0b93759))}}
`;
const result = replacePageProperties(input);
expect(result).toBe(want);
});
});
describe("removeCollapsedBlocks", () => {
it("does nothing if there are no collapsed blocks", () => {
const input = `- TODO foo bar`
expect(removeCollapsedBlocks(input)).toBe(input);
});
it("does nothing if there are no collapsed blocks", () => {
const input = `
- TODO foo bar
- another line
collapsed:: true
-last line`
const want = `
- TODO foo bar
- another line
-last line`
expect(removeCollapsedBlocks(input)).toBe(want);
});
});
describe("removeLogbooks", () => {
it("does nothing if there are no logbooks", () => {
const input = `- TODO foo bar`;
expect(removeLogbooks(input)).toBe(input);
});
it("removes a simple logbook", () => {
const input = `- Some content
:LOGBOOK:
* State "DONE" from "TODO" [2023-11-16 Thu 10:00]
:END:
- More content`;
const want = `- Some content
- More content`;
expect(removeLogbooks(input)).toBe(want);
});
it("removes multiple logbooks", () => {
const input = `- First section
:LOGBOOK:
* Log entry 1
:END:
- Middle section
:LOGBOOK:
* Log entry 2
:END:
- Last section`;
const want = `- First section
- Middle section
- Last section`;
expect(removeLogbooks(input)).toBe(want);
});
it("handles nested content correctly", () => {
const input = `- Top level
:LOGBOOK:
* Nested log
* Another log
:END:
- Still nested
- Back to top`;
const want = `- Top level
- Still nested
- Back to top`;
expect(removeLogbooks(input)).toBe(want);
});
});
describe("updateRelativePaths", () => {
it("adds extra ../ when file moves deeper", () => {
const originalPath = "/usr/foo/bar"
const newPath = "/usr/foo/bar/new"
const input = `Check this ![image](../assets/foo.png) and [link](../docs/bar.md)`;
const result = updateRelativePaths(input, originalPath, newPath);
expect(result).toBe(`Check this ![image](../../assets/foo.png) and [link](../../docs/bar.md)`);
});
it("leaves paths unchanged when depth is same", () => {
const originalPath = "/usr/foo/bar"
const newPath = "/usr/foo/bar"
const input = `Check this ![image](../assets/foo.png)`;
const result = updateRelativePaths(input, originalPath, newPath);
expect(result).toBe(input);
});
it("handles multiple references on same line", () => {
const originalPath = "/usr/foo/bar"
const newPath = "/usr/foo/bar/new"
const input = `![img1](../a.png) and ![img2](../b.png)`;
const result = updateRelativePaths(input, originalPath, newPath);
expect(result).toBe(`![img1](../../a.png) and ![img2](../../b.png)`);
});
it("only updates relative paths", () => {
const originalPath = "/usr/foo/bar"
const newPath = "/usr/foo/bar/new"
const input = `![abs](/assets/foo.png) and ![rel](../bar.png)`;
const result = updateRelativePaths(input, originalPath, newPath);
expect(result).toBe(`![abs](/assets/foo.png) and ![rel](../../bar.png)`);
});
});