-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.tests.ps1
More file actions
1043 lines (902 loc) · 38 KB
/
notes.tests.ps1
File metadata and controls
1043 lines (902 loc) · 38 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env pwsh
<#
Pester v5 tests for notes.ps1
Uses a temp directory for isolation; no editor popups.
#>
BeforeAll {
$script:ScriptPath = Join-Path $PSScriptRoot 'notes.ps1'
$script:OrigNotesDir = $env:NOTES_DIR
$script:OrigEditor = $env:EDITOR
$script:OrigVisual = $env:VISUAL
$script:OrigPager = $env:PAGER
# Isolated temp dir for every test run
$script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "notes-tests-$([guid]::NewGuid().ToString('N'))"
New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
$env:NOTES_DIR = $script:TempDir
# No-op editor — a tiny script that exits immediately (outside notes dir)
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
$script:NoopEditor = Join-Path ([System.IO.Path]::GetTempPath()) "notes-noop-editor-$([guid]::NewGuid().ToString('N')).cmd"
Set-Content -Path $script:NoopEditor -Value '@exit /b 0' -Encoding ascii
} else {
$script:NoopEditor = Join-Path ([System.IO.Path]::GetTempPath()) "notes-noop-editor-$([guid]::NewGuid().ToString('N')).sh"
Set-Content -Path $script:NoopEditor -Value '#!/bin/sh' -Encoding ascii -NoNewline
chmod +x $script:NoopEditor
}
$env:EDITOR = $script:NoopEditor
# ── helpers ──────────────────────────────────────────────────────
function script:Invoke-Notes {
param([string[]]$PassArgs)
& $script:ScriptPath @PassArgs
}
function script:Invoke-NotesProcess {
param(
[string[]]$PassArgs,
[hashtable]$Environment = @{}
)
$savedEnvironment = @{}
foreach ($entry in $Environment.GetEnumerator()) {
$savedEnvironment[$entry.Key] = [Environment]::GetEnvironmentVariable($entry.Key)
if ($null -eq $entry.Value) {
Remove-Item -Path "Env:$($entry.Key)" -ErrorAction SilentlyContinue
} else {
Set-Item -Path "Env:$($entry.Key)" -Value $entry.Value
}
}
try {
$output = & pwsh -NoProfile -File $script:ScriptPath @PassArgs 2>&1
[PSCustomObject]@{
Output = @($output | ForEach-Object { "$_" })
ExitCode = $LASTEXITCODE
}
} finally {
foreach ($entry in $savedEnvironment.GetEnumerator()) {
if ($null -eq $entry.Value) {
Remove-Item -Path "Env:$($entry.Key)" -ErrorAction SilentlyContinue
} else {
Set-Item -Path "Env:$($entry.Key)" -Value $entry.Value
}
}
}
}
function script:New-NoteFile {
param([string]$Slug, [string]$Content = "# test`nsome body text")
$path = Join-Path $env:NOTES_DIR "$Slug.md"
Set-Content -Path $path -Value $Content -Encoding utf8
}
}
AfterAll {
# Restore originals
$env:NOTES_DIR = $script:OrigNotesDir
$env:EDITOR = $script:OrigEditor
$env:VISUAL = $script:OrigVisual
$env:PAGER = $script:OrigPager
if ($script:TempDir -and (Test-Path $script:TempDir)) {
Remove-Item -Recurse -Force $script:TempDir
}
if ($script:NoopEditor -and (Test-Path $script:NoopEditor)) {
Remove-Item -Force $script:NoopEditor
}
}
# ── add ──────────────────────────────────────────────────────────────
Describe 'add' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'creates a new note file' {
Invoke-Notes 'add', 'Hello World'
Join-Path $env:NOTES_DIR 'hello-world.md' | Should -Exist
}
It 'writes a heading into the new note' {
Invoke-Notes 'add', 'Heading Test'
$content = Get-Content (Join-Path $env:NOTES_DIR 'heading-test.md') -Raw
$content | Should -Match '^# Heading Test'
}
It 'emits a success message' {
$out = Invoke-Notes 'add', 'Msg Test' 6>&1
"$out" | Should -BeLike "*Note 'Msg Test' created*"
}
It 'errors when title is missing' {
$out = Invoke-Notes 'add' 6>&1
"$out" | Should -BeLike '*Usage*'
}
It 'errors on duplicate add' {
Invoke-Notes 'add', 'Dup'
$out = Invoke-Notes 'add', 'Dup' 6>&1
"$out" | Should -BeLike '*already exists*'
}
It 'handles special characters in title' {
Invoke-Notes 'add', 'Café & Résumé!'
# slug strips non-alphanumeric chars
Join-Path $env:NOTES_DIR 'caf-rsum.md' | Should -Exist
}
It 'supports EDITOR commands with arguments' {
$capturePath = Join-Path $script:TempDir 'editor-args.txt'
$editorScript = Join-Path $script:TempDir 'editor helper.ps1'
@'
param([string]$NotePath)
Set-Content -Path $env:NOTES_EDITOR_CAPTURE -Value $NotePath -Encoding utf8
'@ | Set-Content -Path $editorScript -Encoding utf8
$env:EDITOR = "pwsh -NoProfile -File `"$editorScript`""
$env:NOTES_EDITOR_CAPTURE = $capturePath
try {
Invoke-Notes 'add', 'Arg Test'
} finally {
$env:EDITOR = $script:NoopEditor
Remove-Item Env:NOTES_EDITOR_CAPTURE -ErrorAction SilentlyContinue
}
$capturePath | Should -Exist
(Get-Content -Path $capturePath -Raw).Trim() | Should -Be (Join-Path $env:NOTES_DIR 'arg-test.md')
}
}
# ── list ─────────────────────────────────────────────────────────────
Describe 'list' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'reports when no notes exist' {
$out = Invoke-Notes 'list' 6>&1
"$out" | Should -BeLike '*No notes found*'
}
It 'lists note names sorted' {
New-NoteFile 'bravo'
New-NoteFile 'alpha'
New-NoteFile 'charlie'
$out = Invoke-Notes 'list'
$out | Should -HaveCount 3
$out[0] | Should -Be 'alpha.md'
$out[1] | Should -Be 'bravo.md'
$out[2] | Should -Be 'charlie.md'
}
}
# ── show ─────────────────────────────────────────────────────────────
Describe 'show' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'displays note content' {
New-NoteFile 'demo' "# Demo`nLine two"
$out = Invoke-Notes 'show', 'Demo'
"$out" | Should -Match 'Demo'
"$out" | Should -Match 'Line two'
}
It 'errors when title is missing' {
$out = Invoke-Notes 'show' 6>&1
"$out" | Should -BeLike '*Usage*'
}
It 'errors when note does not exist' {
$out = Invoke-Notes 'show', 'Ghost' 6>&1
"$out" | Should -BeLike '*not found*'
}
}
# ── edit ─────────────────────────────────────────────────────────────
Describe 'edit' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'opens without error for existing note' {
New-NoteFile 'edit-me'
{ Invoke-Notes 'edit', 'edit-me' } | Should -Not -Throw
}
It 'errors when title is missing' {
$out = Invoke-Notes 'edit' 6>&1
"$out" | Should -BeLike '*Usage*'
}
It 'errors when note does not exist' {
$out = Invoke-Notes 'edit', 'Nope' 6>&1
"$out" | Should -BeLike '*not found*'
}
}
# ── remove ───────────────────────────────────────────────────────────
Describe 'remove' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'removes a note with -Force' {
New-NoteFile 'bye'
Invoke-Notes 'remove', 'bye', '-Force'
Join-Path $env:NOTES_DIR 'bye.md' | Should -Not -Exist
}
It 'emits a removed message with -Force' {
New-NoteFile 'msgdel'
$out = Invoke-Notes 'remove', 'msgdel', '-Force' 6>&1
"$out" | Should -BeLike "*removed*"
}
It 'errors when title is missing' {
$out = Invoke-Notes 'remove' 6>&1
"$out" | Should -BeLike '*Usage*'
}
It 'errors when note does not exist' {
$out = Invoke-Notes 'remove', 'Missing', '-Force' 6>&1
"$out" | Should -BeLike '*not found*'
}
}
# ── search ───────────────────────────────────────────────────────────
Describe 'search' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'finds matching text in notes' {
New-NoteFile 'alpha' "# Alpha`nThe quick brown fox"
New-NoteFile 'beta' "# Beta`nSlow turtle"
$out = Invoke-Notes 'search', 'quick' 6>&1
"$out" | Should -Match 'alpha'
}
It 'is case-insensitive' {
New-NoteFile 'case' "# Case`nHeLLo WoRLd"
$out = Invoke-Notes 'search', 'hello' 6>&1
"$out" | Should -Match 'case'
}
It 'reports no matches when nothing matches' {
New-NoteFile 'nomatch' "# No match`nnothing here"
$out = Invoke-Notes 'search', 'zzzzxyz' 6>&1
"$out" | Should -BeLike '*No matches found*'
}
It 'reports no notes when dir is empty' {
$out = Invoke-Notes 'search', 'anything' 6>&1
"$out" | Should -BeLike '*No notes found*'
}
It 'errors when search text is missing' {
$out = Invoke-Notes 'search' 6>&1
"$out" | Should -BeLike '*Usage*'
}
It 'searches across multiple words' {
New-NoteFile 'multi' "# Multi`nfoo bar baz"
$out = Invoke-Notes 'search', 'foo', 'bar' 6>&1
"$out" | Should -Match 'multi'
}
}
# ── help / default ───────────────────────────────────────────────────
Describe 'help and default' {
It 'shows help with help command' {
$out = Invoke-Notes 'help' 6>&1
"$out" | Should -Match 'Usage'
}
It 'shows help with no command' {
$out = Invoke-Notes 6>&1
"$out" | Should -Match 'Usage'
}
}
Describe 'process exit codes' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
$env:EDITOR = $script:NoopEditor
}
It 'returns non-zero for a missing note' {
$result = Invoke-NotesProcess -PassArgs @('show', 'missing-note')
$result.ExitCode | Should -Not -Be 0
($result.Output -join "`n") | Should -BeLike '*not found*'
}
It 'returns non-zero for duplicate add' {
Invoke-Notes 'add', 'Dup'
$result = Invoke-NotesProcess -PassArgs @('add', 'Dup')
$result.ExitCode | Should -Not -Be 0
($result.Output -join "`n") | Should -BeLike '*already exists*'
}
It 'returns non-zero when the editor cannot be started' {
$result = Invoke-NotesProcess -PassArgs @('add', 'Bad Editor') -Environment @{ EDITOR = 'definitely-missing-editor' }
$result.ExitCode | Should -Not -Be 0
($result.Output -join "`n") | Should -BeLike '*Failed to start editor*'
}
It 'returns non-zero for unknown commands' {
$result = Invoke-NotesProcess -PassArgs @('bogus')
$result.ExitCode | Should -Not -Be 0
($result.Output -join "`n") | Should -BeLike '*Unknown command*'
}
}
# ── check ────────────────────────────────────────────────────────────
Describe 'check' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'reports directory path from NOTES_DIR and OK status with note count' {
New-NoteFile 'one'
New-NoteFile 'two'
$out = Invoke-Notes 'check' 6>&1
"$out" | Should -Match 'NOTES_DIR'
"$out" | Should -Match 'OK'
"$out" | Should -Match '2 note'
}
It 'reports OK with 0 notes when directory is empty' {
$out = Invoke-Notes 'check' 6>&1
"$out" | Should -Match 'OK'
"$out" | Should -Match '0 note'
}
It 'reports NOT FOUND when directory does not exist' {
$saved = $env:NOTES_DIR
$env:NOTES_DIR = Join-Path ([System.IO.Path]::GetTempPath()) "notes-nonexistent-$([guid]::NewGuid().ToString('N'))"
try {
$out = Invoke-Notes 'check' 6>&1
"$out" | Should -Match 'NOT FOUND'
} finally {
$env:NOTES_DIR = $saved
}
}
}
# ── list with pattern ────────────────────────────────────────────────
Describe 'list with pattern' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'filters notes by filename pattern' {
New-NoteFile 'alpha-one'
New-NoteFile 'alpha-two'
New-NoteFile 'beta-one'
$out = Invoke-Notes 'list', 'alpha'
$out | Should -HaveCount 2
$out[0] | Should -Be 'alpha-one.md'
$out[1] | Should -Be 'alpha-two.md'
}
It 'reports no matches for unmatched pattern' {
New-NoteFile 'alpha'
$out = Invoke-Notes 'list', 'zzz' 6>&1
"$out" | Should -BeLike "*No notes matching*"
}
It 'pattern filtering is case-insensitive' {
New-NoteFile 'alpha-note'
$out = @(Invoke-Notes 'list', 'ALPHA')
$out | Should -HaveCount 1
$out[0] | Should -Be 'alpha-note.md'
}
}
# ── tag operations ───────────────────────────────────────────────────
Describe 'tag operations' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
Context 'list with tag filter' {
It 'list +tag returns only tagged notes' {
New-NoteFile 'chess-opening' "#Chess`n# Chess Opening`nContent"
New-NoteFile 'recipe' "#Cooking`n# Recipe`nContent"
New-NoteFile 'plain' "# Plain`nNo tags"
$out = @(Invoke-Notes 'list', '+Chess')
$out | Should -HaveCount 1
$out[0] | Should -Be 'chess-opening.md'
}
It 'list tag:tag filters by tag' {
New-NoteFile 'chess-opening' "#Chess`n# Chess Opening`nContent"
New-NoteFile 'recipe' "#Cooking`n# Recipe`nContent"
$out = @(Invoke-Notes 'list', 'tag:Chess')
$out | Should -HaveCount 1
$out[0] | Should -Be 'chess-opening.md'
}
It 'list #tag filters by tag' {
New-NoteFile 'chess-opening' "#Chess`n# Chess Opening`nContent"
$out = Invoke-Notes 'list', '#Chess'
$out | Should -HaveCount 1
}
It 'reports no tagged notes when tag is not found' {
New-NoteFile 'alpha' "# Alpha`nContent"
$out = Invoke-Notes 'list', '+NoSuchTag' 6>&1
"$out" | Should -BeLike "*No notes tagged*"
}
It 'finds notes with multiple tags on first line' {
New-NoteFile 'multi' "#Chess #Opening`n# Multi-tagged`nContent"
$out1 = Invoke-Notes 'list', '+Chess'
$out1 | Should -HaveCount 1
$out2 = Invoke-Notes 'list', '+Opening'
$out2 | Should -HaveCount 1
}
It 'tag matching is case-insensitive' {
New-NoteFile 'chess' "#Chess`n# Chess`nContent"
$out = Invoke-Notes 'list', '+chess'
$out | Should -HaveCount 1
}
It 'does not match tag beyond the first line' {
New-NoteFile 'tricky' "# Title`n#Chess`nContent"
$out = Invoke-Notes 'list', '+Chess' 6>&1
"$out" | Should -BeLike "*No notes tagged*"
}
}
Context 'show with tag' {
It 'show +tag displays the tagged note' {
New-NoteFile 'chess-opening' "#Chess`n# Chess Opening`nSicilian defense"
$out = Invoke-Notes 'show', '+Chess'
"$out" | Should -Match 'Sicilian defense'
}
It 'show +tag errors when no notes have that tag' {
New-NoteFile 'alpha' "# Alpha`nContent"
$out = Invoke-Notes 'show', '+NoSuchTag' 6>&1
"$out" | Should -BeLike "*No notes tagged*"
}
}
Context 'search with tag' {
It 'search +tag text only searches tagged notes' {
New-NoteFile 'chess' "#Chess`n# Chess`nSicilian defense"
New-NoteFile 'recipe' "#Cooking`n# Recipe`nChicken curry"
$out = Invoke-Notes 'search', '+Chess', 'Sicilian' 6>&1
"$out" | Should -Match 'chess'
"$out" | Should -Not -Match 'recipe'
}
It 'search +tag reports no tagged notes when tag not found' {
New-NoteFile 'alpha' "# Alpha`nContent"
$out = Invoke-Notes 'search', '+NoSuchTag', 'anything' 6>&1
"$out" | Should -BeLike "*No notes tagged*"
}
It 'search +tag reports no matches when text not found in tagged notes' {
New-NoteFile 'chess' "#Chess`n# Chess`nSicilian defense"
$out = Invoke-Notes 'search', '+Chess', 'zzzzxyz' 6>&1
"$out" | Should -BeLike '*No matches found*'
}
}
}
# ── import ───────────────────────────────────────────────────────────
Describe 'import' {
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'errors when no backup dir is given' {
$out = Invoke-Notes 'import' 6>&1
"$out" | Should -BeLike '*Usage*'
}
It 'errors when backup dir does not exist' {
$out = Invoke-Notes 'import', 'C:\nonexistent-path-xyz' 6>&1
"$out" | Should -BeLike '*not found*'
}
It 'errors when Items\Note is missing' {
$backupDir = Join-Path ([System.IO.Path]::GetTempPath()) "notes-import-test-$([guid]::NewGuid().ToString('N'))"
New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
try {
$out = Invoke-Notes 'import', $backupDir 6>&1
"$out" | Should -BeLike '*No Items*Note directory*'
} finally {
Remove-Item $backupDir -Recurse -Force
}
}
It 'imports a plain text note' {
$backupDir = Join-Path ([System.IO.Path]::GetTempPath()) "notes-import-test-$([guid]::NewGuid().ToString('N'))"
$noteDir = Join-Path $backupDir 'Items\Note'
New-Item -ItemType Directory -Path $noteDir -Force | Out-Null
Set-Content -Path (Join-Path $noteDir 'My Note-abcd1234.txt') -Value 'Hello world' -Encoding utf8
try {
$out = Invoke-Notes 'import', $backupDir 6>&1
"$out" | Should -Match '1 imported'
Join-Path $env:NOTES_DIR 'my-note.md' | Should -Exist
$content = Get-Content (Join-Path $env:NOTES_DIR 'my-note.md') -Raw
$content | Should -Match '# My Note'
$content | Should -Match 'Hello world'
} finally {
Remove-Item $backupDir -Recurse -Force
}
}
It 'skips empty note files' {
$backupDir = Join-Path ([System.IO.Path]::GetTempPath()) "notes-import-test-$([guid]::NewGuid().ToString('N'))"
$noteDir = Join-Path $backupDir 'Items\Note'
New-Item -ItemType Directory -Path $noteDir -Force | Out-Null
# Create a truly empty (0-byte) file
New-Item -ItemType File -Path (Join-Path $noteDir 'Empty-abcd1234.txt') -Force | Out-Null
Set-Content -Path (Join-Path $noteDir 'Notempty-efgh5678.txt') -Value 'content' -Encoding utf8
try {
$out = Invoke-Notes 'import', $backupDir 6>&1
"$out" | Should -Match '1 imported'
"$out" | Should -Match '1 skipped'
} finally {
Remove-Item $backupDir -Recurse -Force
}
}
It 'handles duplicate slugs by appending a number' {
New-NoteFile 'my-note' "# Existing note"
$backupDir = Join-Path ([System.IO.Path]::GetTempPath()) "notes-import-test-$([guid]::NewGuid().ToString('N'))"
$noteDir = Join-Path $backupDir 'Items\Note'
New-Item -ItemType Directory -Path $noteDir -Force | Out-Null
Set-Content -Path (Join-Path $noteDir 'My Note-abcd1234.txt') -Value 'New content' -Encoding utf8
try {
$out = Invoke-Notes 'import', $backupDir 6>&1
"$out" | Should -Match '1 imported'
Join-Path $env:NOTES_DIR 'my-note-2.md' | Should -Exist
} finally {
Remove-Item $backupDir -Recurse -Force
}
}
It 'applies tags from tag files' {
$backupDir = Join-Path ([System.IO.Path]::GetTempPath()) "notes-import-test-$([guid]::NewGuid().ToString('N'))"
$noteDir = Join-Path $backupDir 'Items\Note'
$tagDir = Join-Path $backupDir 'Items\Tag'
New-Item -ItemType Directory -Path $noteDir -Force | Out-Null
New-Item -ItemType Directory -Path $tagDir -Force | Out-Null
Set-Content -Path (Join-Path $noteDir 'My Note-abcd1234.txt') -Value 'Some content' -Encoding utf8
$tagJson = @{
title = "Chess"
references = @(
@{ content_type = "Note"; uuid = "xxxx-xxxx-abcd1234" }
)
} | ConvertTo-Json -Depth 5
Set-Content -Path (Join-Path $tagDir 'Chess-tag12345.txt') -Value $tagJson -Encoding utf8
try {
$out = Invoke-Notes 'import', $backupDir 6>&1
"$out" | Should -Match '1 imported'
"$out" | Should -Match '1 with tags'
$content = Get-Content (Join-Path $env:NOTES_DIR 'my-note.md') -Raw
$content | Should -Match '#Chess'
} finally {
Remove-Item $backupDir -Recurse -Force
}
}
It 'converts Lexical JSON notes to markdown' {
$backupDir = Join-Path ([System.IO.Path]::GetTempPath()) "notes-import-test-$([guid]::NewGuid().ToString('N'))"
$noteDir = Join-Path $backupDir 'Items\Note'
New-Item -ItemType Directory -Path $noteDir -Force | Out-Null
$lexicalJson = @{
root = @{
type = "root"
children = @(
@{
type = "paragraph"
children = @(
@{ type = "text"; text = "Hello world"; format = 0 }
)
}
)
}
} | ConvertTo-Json -Depth 10
Set-Content -Path (Join-Path $noteDir 'Lexical Test-abcd1234.txt') -Value $lexicalJson -Encoding utf8
try {
$out = Invoke-Notes 'import', $backupDir 6>&1
"$out" | Should -Match '1 imported'
$content = Get-Content (Join-Path $env:NOTES_DIR 'lexical-test.md') -Raw
$content | Should -Match 'Hello world'
} finally {
Remove-Item $backupDir -Recurse -Force
}
}
It 'imports a note without hex suffix in filename' {
$backupDir = Join-Path ([System.IO.Path]::GetTempPath()) "notes-import-test-$([guid]::NewGuid().ToString('N'))"
$noteDir = Join-Path $backupDir 'Items\Note'
New-Item -ItemType Directory -Path $noteDir -Force | Out-Null
Set-Content -Path (Join-Path $noteDir 'SimpleNote.txt') -Value 'Plain body' -Encoding utf8
try {
$out = Invoke-Notes 'import', $backupDir 6>&1
"$out" | Should -Match '1 imported'
Join-Path $env:NOTES_DIR 'simplenote.md' | Should -Exist
$content = Get-Content (Join-Path $env:NOTES_DIR 'simplenote.md') -Raw
$content | Should -Match '# SimpleNote'
} finally {
Remove-Item $backupDir -Recurse -Force
}
}
}
# ── ConvertTo-Slug edge cases ────────────────────────────────────────
Describe 'ConvertTo-Slug edge cases' {
BeforeAll {
. $script:ScriptPath 'help' 6>&1 | Out-Null
}
It 'collapses multiple spaces into single hyphens' {
ConvertTo-Slug 'Hello World' | Should -Be 'hello-world'
}
It 'trims leading and trailing whitespace' {
ConvertTo-Slug ' Hello World ' | Should -Be 'hello-world'
}
It 'returns empty string for all special characters' {
ConvertTo-Slug '@#$%^&*()' | Should -Be ''
}
It 'returns empty string for empty input' {
ConvertTo-Slug '' | Should -Be ''
}
It 'strips non-ASCII unicode characters' {
ConvertTo-Slug 'Héllo Wörld' | Should -Be 'hllo-wrld'
}
It 'handles mixed case with numbers' {
ConvertTo-Slug 'Note 42 About Stuff' | Should -Be 'note-42-about-stuff'
}
It 'collapses multiple hyphens' {
ConvertTo-Slug 'Hello---World' | Should -Be 'hello-world'
}
It 'trims leading and trailing hyphens' {
ConvertTo-Slug '-hello-' | Should -Be 'hello'
}
}
# ── ConvertFrom-LexicalNode / ConvertFrom-LexicalJson ────────────────
Describe 'Lexical JSON conversion' {
BeforeAll {
. $script:ScriptPath 'help' 6>&1 | Out-Null
}
It 'converts plain text paragraph' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","text":"Hello world","format":0}]}]}}'
ConvertFrom-LexicalJson $json | Should -Be 'Hello world'
}
It 'converts bold text (format 1)' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","text":"bold","format":1}]}]}}'
ConvertFrom-LexicalJson $json | Should -Be '**bold**'
}
It 'converts italic text (format 2)' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","text":"italic","format":2}]}]}}'
ConvertFrom-LexicalJson $json | Should -Be '*italic*'
}
It 'converts bold+italic text (format 3)' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","text":"both","format":3}]}]}}'
ConvertFrom-LexicalJson $json | Should -Be '***both***'
}
It 'converts a link node' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"link","url":"https://example.com","children":[{"type":"text","text":"click","format":0}]}]}]}}'
ConvertFrom-LexicalJson $json | Should -Be '[click](https://example.com)'
}
It 'converts an autolink node' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"autolink","url":"https://example.com"}]}]}}'
ConvertFrom-LexicalJson $json | Should -Be 'https://example.com'
}
It 'joins multiple paragraphs with double newline' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","text":"Para 1","format":0}]},{"type":"paragraph","children":[{"type":"text","text":"Para 2","format":0}]}]}}'
$result = ConvertFrom-LexicalJson $json
$result | Should -Be "Para 1`n`nPara 2"
}
It 'converts unordered list' {
$json = @{
root = @{
type = "root"
children = @(
@{
type = "list"
listType = "bullet"
children = @(
@{ type = "listitem"; indent = 0; children = @(@{ type = "text"; text = "Item A"; format = 0 }) }
@{ type = "listitem"; indent = 0; children = @(@{ type = "text"; text = "Item B"; format = 0 }) }
)
}
)
}
} | ConvertTo-Json -Depth 10
$result = ConvertFrom-LexicalJson $json
$result | Should -Match '- Item A'
$result | Should -Match '- Item B'
}
It 'converts ordered list' {
$json = @{
root = @{
type = "root"
children = @(
@{
type = "list"
listType = "number"
children = @(
@{ type = "listitem"; indent = 0; children = @(@{ type = "text"; text = "First"; format = 0 }) }
@{ type = "listitem"; indent = 0; children = @(@{ type = "text"; text = "Second"; format = 0 }) }
)
}
)
}
} | ConvertTo-Json -Depth 10
$result = ConvertFrom-LexicalJson $json
$result | Should -Match '1\. First'
$result | Should -Match '2\. Second'
}
It 'converts a table with header separator' {
$json = @{
root = @{
type = "root"
children = @(
@{
type = "table"
children = @(
@{
type = "tablerow"
children = @(
@{ type = "tablecell"; children = @(@{ type = "paragraph"; children = @(@{ type = "text"; text = "H1"; format = 0 }) }) }
@{ type = "tablecell"; children = @(@{ type = "paragraph"; children = @(@{ type = "text"; text = "H2"; format = 0 }) }) }
)
}
@{
type = "tablerow"
children = @(
@{ type = "tablecell"; children = @(@{ type = "paragraph"; children = @(@{ type = "text"; text = "C1"; format = 0 }) }) }
@{ type = "tablecell"; children = @(@{ type = "paragraph"; children = @(@{ type = "text"; text = "C2"; format = 0 }) }) }
)
}
)
}
)
}
} | ConvertTo-Json -Depth 15
$result = ConvertFrom-LexicalJson $json
$result | Should -Match 'H1'
$result | Should -Match '---'
$result | Should -Match 'C1'
}
It 'returns empty string for null node' {
ConvertFrom-LexicalNode $null | Should -Be ''
}
It 'handles linebreak node' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","text":"before","format":0},{"type":"linebreak"},{"type":"text","text":"after","format":0}]}]}}'
$result = ConvertFrom-LexicalJson $json
$result | Should -Match 'before'
$result | Should -Match 'after'
}
It 'skips snfile nodes' {
$json = '{"root":{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","text":"visible","format":0},{"type":"snfile"}]}]}}'
$result = ConvertFrom-LexicalJson $json
$result | Should -Be 'visible'
}
}
# ── Get-TagFromArg / Test-IsTagArg ──────────────────────────────────
Describe 'tag argument helpers' {
BeforeAll {
. $script:ScriptPath 'help' 6>&1 | Out-Null
}
Context 'Get-TagFromArg' {
It 'parses #tag prefix' {
Get-TagFromArg '#Chess' | Should -Be 'Chess'
}
It 'parses +tag prefix' {
Get-TagFromArg '+Chess' | Should -Be 'Chess'
}
It 'parses tag: prefix' {
Get-TagFromArg 'tag:Chess' | Should -Be 'Chess'
}
It 'returns null for plain text' {
Get-TagFromArg 'Chess' | Should -BeNullOrEmpty
}
}
Context 'Test-IsTagArg' {
It 'returns true for #tag' {
Test-IsTagArg '#Chess' | Should -BeTrue
}
It 'returns true for +tag' {
Test-IsTagArg '+Chess' | Should -BeTrue
}
It 'returns true for tag:tag' {
Test-IsTagArg 'tag:Chess' | Should -BeTrue
}
It 'returns false for plain text' {
Test-IsTagArg 'Chess' | Should -BeFalse
}
}
Context 'Split-TagArgument' {
It 'splits a leading tag from the remaining arguments' {
$result = Split-TagArgument @('+Recipes', 'chicken', 'soup')
$result.Tag | Should -Be 'Recipes'
$result.Arguments | Should -Be @('chicken', 'soup')
}
It 'returns all arguments when there is no leading tag' {
$result = Split-TagArgument @('chicken', 'soup')
$result.Tag | Should -BeNullOrEmpty
$result.Arguments | Should -Be @('chicken', 'soup')
}
It 'returns empty arguments when no values are passed' {
$result = Split-TagArgument -Values @()
$result.Tag | Should -BeNullOrEmpty
$result.Arguments.Count | Should -Be 0
}
}
}
# ── Find-NotePath ────────────────────────────────────────────────────
Describe 'Find-NotePath' {
BeforeAll {
. $script:ScriptPath 'help' 6>&1 | Out-Null
}
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'exact filename match' {
New-NoteFile 'hello-world'
$result = Find-NotePath 'hello-world.md'
$result | Should -HaveCount 1
$result[0].Name | Should -Be 'hello-world.md'
}
It 'slug basename match' {
New-NoteFile 'hello-world'
$result = Find-NotePath 'Hello World'
$result | Should -HaveCount 1
$result[0].Name | Should -Be 'hello-world.md'
}
It 'partial substring match on filename' {
New-NoteFile 'hello-world'
$result = Find-NotePath 'hello'
$result | Should -HaveCount 1
$result[0].Name | Should -Be 'hello-world.md'
}
It 'partial slug match on basename' {
New-NoteFile 'hello-world-notes'
$result = Find-NotePath 'Hello World'
$result | Should -HaveCount 1
$result[0].Name | Should -Be 'hello-world-notes.md'
}
It 'returns empty array when no match' {
New-NoteFile 'hello-world'
$result = Find-NotePath 'zzz-no-match'
$result | Should -HaveCount 0
}
It 'returns empty array when directory is empty' {
$result = Find-NotePath 'anything'
$result | Should -HaveCount 0
}
}
Describe 'Test-NoteExists' {
BeforeAll {
. $script:ScriptPath 'help' 6>&1 | Out-Null
}
BeforeEach {
Get-ChildItem $env:NOTES_DIR -File | Remove-Item -Force
}
It 'returns true when a note can be resolved by title' {
New-NoteFile 'hello-world'
Test-NoteExists 'Hello World' | Should -BeTrue
}
It 'returns false when the note does not exist' {
Test-NoteExists 'Missing' | Should -BeFalse
}
}
# ── Get-Editor ───────────────────────────────────────────────────────
Describe 'Get-Editor' {
BeforeAll {
. $script:ScriptPath 'help' 6>&1 | Out-Null
$script:SavedEditor = $env:EDITOR
$script:SavedVisual = $env:VISUAL
}
AfterAll {
$env:EDITOR = $script:SavedEditor
$env:VISUAL = $script:SavedVisual
}
It 'returns EDITOR when set' {
$env:EDITOR = 'myeditor'
$env:VISUAL = 'myvisual'
Get-Editor | Should -Be 'myeditor'
}
It 'falls back to VISUAL when EDITOR not set' {
$env:EDITOR = ''
$env:VISUAL = 'myvisual'
Get-Editor | Should -Be 'myvisual'
}
It 'falls back to the platform default when neither set' {
$env:EDITOR = ''
$env:VISUAL = ''
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
Get-Editor | Should -Be 'notepad'
} else {
Get-Editor | Should -Be 'vi'
}
}
}
# ── Get-Pager ────────────────────────────────────────────────────────
Describe 'Get-Pager' {
BeforeAll {
. $script:ScriptPath 'help' 6>&1 | Out-Null
$script:SavedPager = $env:PAGER
}
AfterAll {
$env:PAGER = $script:SavedPager
}
It 'returns PAGER when set' {
$env:PAGER = 'mypager'
Get-Pager | Should -Be 'mypager'
}
It 'falls back to the platform default when not set' {
$env:PAGER = ''
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
Get-Pager | Should -Be 'more.com'
} else {
Get-Pager | Should -Be 'less'
}