aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_common.bats
blob: 6f9d1b14a45a8f609c96bd9a7e88804d84a9080e (plain)
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
#!/usr/bin/env bats
# Unit tests for installer/lib/common.sh

setup() {
    # shellcheck disable=SC1091
    source "${BATS_TEST_DIRNAME}/../../installer/lib/common.sh"
}

@test "command_exists returns 0 for an existing command" {
    run command_exists bash
    [ "$status" -eq 0 ]
}

@test "command_exists returns 1 for a missing command" {
    run command_exists this_does_not_exist_xyz_42
    [ "$status" -eq 1 ]
}

@test "require_command succeeds for an existing command" {
    run require_command bash
    [ "$status" -eq 0 ]
}

@test "require_command fails and reports missing command" {
    run require_command this_does_not_exist_xyz_42
    [ "$status" -eq 1 ]
    [[ "$output" == *"Required command not found"* ]]
    [[ "$output" == *"this_does_not_exist_xyz_42"* ]]
}

@test "enable_color populates color variables" {
    [ -z "$RED" ]
    [ -z "$GREEN" ]
    [ -z "$NC" ]
    enable_color
    [ -n "$RED" ]
    [ -n "$GREEN" ]
    [ -n "$YELLOW" ]
    [ -n "$BLUE" ]
    [ -n "$BOLD" ]
    [ -n "$NC" ]
}

@test "info prints [INFO] prefix and message" {
    run info "hello world"
    [ "$status" -eq 0 ]
    [[ "$output" == *"[INFO]"* ]]
    [[ "$output" == *"hello world"* ]]
}

@test "warn prints [WARN] prefix and message" {
    run warn "heads up"
    [ "$status" -eq 0 ]
    [[ "$output" == *"[WARN]"* ]]
    [[ "$output" == *"heads up"* ]]
}

@test "error prints [ERROR] and exits with status 1" {
    run error "something broke"
    [ "$status" -eq 1 ]
    [[ "$output" == *"[ERROR]"* ]]
    [[ "$output" == *"something broke"* ]]
}

@test "require_root fails for non-root user" {
    [ "$EUID" -ne 0 ] || skip "running as root"
    run require_root
    [ "$status" -eq 1 ]
    [[ "$output" == *"must be run as root"* ]]
}

@test "log writes timestamped line when LOG_FILE set" {
    local tmp
    tmp=$(mktemp)
    LOG_FILE="$tmp" log "test entry"
    run cat "$tmp"
    [ "$status" -eq 0 ]
    [[ "$output" == *"test entry"* ]]
    [[ "$output" =~ \[[0-9]{4}-[0-9]{2}-[0-9]{2} ]]
    rm -f "$tmp"
}

@test "log is a no-op when LOG_FILE unset" {
    unset LOG_FILE
    run log "should not crash"
    [ "$status" -eq 0 ]
    [ -z "$output" ]
}

#############################
# pacstrap_packages
#############################

@test "pacstrap_packages zfs includes zfs-dkms and zfs-utils" {
    run pacstrap_packages zfs
    [ "$status" -eq 0 ]
    [[ "$output" == *"zfs-dkms"* ]]
    [[ "$output" == *"zfs-utils"* ]]
}

@test "pacstrap_packages btrfs includes btrfs-progs, grub, grub-btrfs, snapper, snap-pac" {
    run pacstrap_packages btrfs
    [ "$status" -eq 0 ]
    [[ "$output" == *"btrfs-progs"* ]]
    [[ "$output" == *"grub"* ]]
    [[ "$output" == *"grub-btrfs"* ]]
    [[ "$output" == *"snapper"* ]]
    [[ "$output" == *"snap-pac"* ]]
}

@test "pacstrap_packages zfs excludes Btrfs-specific packages" {
    run pacstrap_packages zfs
    [ "$status" -eq 0 ]
    [[ "$output" != *"btrfs-progs"* ]]
    [[ "$output" != *"grub-btrfs"* ]]
    [[ "$output" != *"snapper"* ]]
}

@test "pacstrap_packages btrfs excludes ZFS-specific packages" {
    run pacstrap_packages btrfs
    [ "$status" -eq 0 ]
    [[ "$output" != *"zfs-dkms"* ]]
    [[ "$output" != *"zfs-utils"* ]]
}

@test "pacstrap_packages includes common packages for both filesystems" {
    for fs in zfs btrfs; do
        run pacstrap_packages "$fs"
        [ "$status" -eq 0 ]
        [[ "$output" == *"base"* ]]
        [[ "$output" == *"linux-lts"* ]]
        [[ "$output" == *"efibootmgr"* ]]
        [[ "$output" == *"networkmanager"* ]]
        [[ "$output" == *"openssh"* ]]
        [[ "$output" == *"inetutils"* ]]
    done
}

@test "pacstrap_packages unknown filesystem returns status 1" {
    run pacstrap_packages reiserfs
    [ "$status" -eq 1 ]
    [ -z "$output" ]
}

@test "pacstrap_packages emits one package per line" {
    run pacstrap_packages zfs
    [ "$status" -eq 0 ]
    local expected_lines
    expected_lines=$(echo "$output" | wc -l)
    [ "$expected_lines" -ge 20 ]
}

#############################
# prompt_password
#############################

@test "prompt_password accepts matching value meeting min length" {
    prompt_password PASS "label" 4 < <(printf 'hello\nhello\n') >/dev/null
    [ "$PASS" = "hello" ]
}

@test "prompt_password enforces min length by looping until met" {
    prompt_password PASS "label" 4 < <(printf 'ab\nab\nlongenough\nlongenough\n') >/dev/null
    [ "$PASS" = "longenough" ]
}

@test "prompt_password retries on mismatch" {
    prompt_password PASS "label" 4 < <(printf 'aaaa\nbbbb\nfinal1234\nfinal1234\n') >/dev/null
    [ "$PASS" = "final1234" ]
}

@test "prompt_password with min_len=0 skips length check" {
    prompt_password PASS "label" 0 < <(printf 'x\nx\n') >/dev/null
    [ "$PASS" = "x" ]
}

@test "prompt_password accepts empty passphrase when min_len=0" {
    prompt_password PASS "label" 0 < <(printf '\n\n') >/dev/null
    [ -z "$PASS" ]
}

#############################
# install_dropin
#############################

setup_dropin_tmp() {
    DROPIN_ROOT=$(mktemp -d)
}

teardown_dropin_tmp() {
    [ -n "${DROPIN_ROOT:-}" ] && rm -rf "$DROPIN_ROOT"
}

@test "install_dropin writes conf file at expected path" {
    setup_dropin_tmp
    install_dropin foo bar "$DROPIN_ROOT" <<< "[Service]"
    [ -f "$DROPIN_ROOT/etc/systemd/system/foo.service.d/bar.conf" ]
    teardown_dropin_tmp
}

@test "install_dropin writes stdin content verbatim" {
    setup_dropin_tmp
    install_dropin foo bar "$DROPIN_ROOT" <<< "[Service]
PrivateTmp=yes"
    run cat "$DROPIN_ROOT/etc/systemd/system/foo.service.d/bar.conf"
    [ "$status" -eq 0 ]
    [[ "$output" == *"[Service]"* ]]
    [[ "$output" == *"PrivateTmp=yes"* ]]
    teardown_dropin_tmp
}

@test "install_dropin creates dropin dir with 755 perms" {
    setup_dropin_tmp
    install_dropin foo bar "$DROPIN_ROOT" <<< "x"
    local perms
    perms=$(stat -c '%a' "$DROPIN_ROOT/etc/systemd/system/foo.service.d")
    [ "$perms" = "755" ]
    teardown_dropin_tmp
}

@test "install_dropin is idempotent — second call overwrites content" {
    setup_dropin_tmp
    install_dropin foo bar "$DROPIN_ROOT" <<< "first"
    install_dropin foo bar "$DROPIN_ROOT" <<< "second"
    run cat "$DROPIN_ROOT/etc/systemd/system/foo.service.d/bar.conf"
    [ "$output" = "second" ]
    teardown_dropin_tmp
}

@test "install_dropin accepts empty content" {
    setup_dropin_tmp
    install_dropin foo bar "$DROPIN_ROOT" < /dev/null
    [ -f "$DROPIN_ROOT/etc/systemd/system/foo.service.d/bar.conf" ]
    [ ! -s "$DROPIN_ROOT/etc/systemd/system/foo.service.d/bar.conf" ]
    teardown_dropin_tmp
}

@test "install_dropin preserves special characters in content" {
    setup_dropin_tmp
    install_dropin foo bar "$DROPIN_ROOT" <<< '# comment with $var and `backtick`
[Service]
Environment="FOO=bar baz"'
    run cat "$DROPIN_ROOT/etc/systemd/system/foo.service.d/bar.conf"
    [[ "$output" == *'$var'* ]]
    [[ "$output" == *'`backtick`'* ]]
    [[ "$output" == *'"FOO=bar baz"'* ]]
    teardown_dropin_tmp
}

#############################
# parse_efibootmgr_entry
#############################

@test "parse_efibootmgr_entry returns boot number for matching label" {
    local sample="BootCurrent: 0001
Boot0000* Windows Boot Manager
Boot0001* ZFSBootMenu
Boot0002* PXE Boot"
    run parse_efibootmgr_entry "ZFSBootMenu" <<< "$sample"
    [ "$status" -eq 0 ]
    [ "$output" = "0001" ]
}

@test "parse_efibootmgr_entry returns first match when multiple labels match" {
    local sample="Boot0001* ZFSBootMenu
Boot0002* ZFSBootMenu-disk2"
    run parse_efibootmgr_entry "ZFSBootMenu" <<< "$sample"
    [ "$status" -eq 0 ]
    [ "$output" = "0001" ]
}

@test "parse_efibootmgr_entry handles hex characters in boot number" {
    local sample="Boot00FE* ZFSBootMenu"
    run parse_efibootmgr_entry "ZFSBootMenu" <<< "$sample"
    [ "$status" -eq 0 ]
    [ "$output" = "00FE" ]
}

@test "parse_efibootmgr_entry returns 1 with empty output when label absent" {
    local sample="Boot0001* Windows Boot Manager"
    run parse_efibootmgr_entry "ZFSBootMenu" <<< "$sample"
    [ "$status" -eq 1 ]
    [ -z "$output" ]
}

@test "parse_efibootmgr_entry returns 1 with empty output for empty input" {
    run parse_efibootmgr_entry "ZFSBootMenu" < /dev/null
    [ "$status" -eq 1 ]
    [ -z "$output" ]
}

@test "parse_efibootmgr_entry returns 1 for empty label without false-matching BootCurrent" {
    local sample="BootCurrent: 0001
Boot0001* ZFSBootMenu"
    run parse_efibootmgr_entry "" <<< "$sample"
    [ "$status" -eq 1 ]
    [ -z "$output" ]
}

#############################
# parse_efibootmgr_bootorder
#############################

@test "parse_efibootmgr_bootorder extracts comma-separated boot numbers" {
    local sample="BootCurrent: 0001
BootOrder: 0001,0002,0003
Boot0001* ZFSBootMenu"
    run parse_efibootmgr_bootorder <<< "$sample"
    [ "$status" -eq 0 ]
    [ "$output" = "0001,0002,0003" ]
}

@test "parse_efibootmgr_bootorder strips whitespace from boot order" {
    local sample="BootOrder: 0001, 0002 , 0003"
    run parse_efibootmgr_bootorder <<< "$sample"
    [ "$status" -eq 0 ]
    [ "$output" = "0001,0002,0003" ]
}

@test "parse_efibootmgr_bootorder returns 1 when BootOrder line absent" {
    local sample="BootCurrent: 0001
Boot0001* ZFSBootMenu"
    run parse_efibootmgr_bootorder <<< "$sample"
    [ "$status" -eq 1 ]
    [ -z "$output" ]
}

#############################
# Path constants
#############################

@test "EFI_DIR is defined and equals /mnt/efi" {
    [ "$EFI_DIR" = "/mnt/efi" ]
}

@test "MNTPOINT is defined and equals /mnt" {
    [ "$MNTPOINT" = "/mnt" ]
}

#############################
# enable_sshd_root_login
#############################
# enable_sshd_root_login takes an sshd_config path and ensures the
# file ends up with `PermitRootLogin yes`. It must error loudly if
# neither the commented (#PermitRootLogin) nor uncommented
# (PermitRootLogin) form is present, since silently appending would
# mask a corrupted starting file.

@test "enable_sshd_root_login uncomments stock Arch sshd_config line" {
    local f
    f=$(mktemp)
    printf '%s\n' '#PermitRootLogin prohibit-password' > "$f"

    enable_sshd_root_login "$f"

    grep -q '^PermitRootLogin yes$' "$f"
    rm -f "$f"
}

@test "enable_sshd_root_login flips PermitRootLogin no to yes" {
    local f
    f=$(mktemp)
    printf '%s\n' 'PermitRootLogin no' > "$f"

    enable_sshd_root_login "$f"

    grep -q '^PermitRootLogin yes$' "$f"
    ! grep -q '^PermitRootLogin no$' "$f"
    rm -f "$f"
}

@test "enable_sshd_root_login is idempotent on PermitRootLogin yes" {
    local f
    f=$(mktemp)
    printf '%s\n' 'PermitRootLogin yes' > "$f"

    enable_sshd_root_login "$f"

    [ "$(grep -c '^PermitRootLogin yes$' "$f")" -eq 1 ]
    rm -f "$f"
}

@test "enable_sshd_root_login replaces all matching lines (mixed commented + uncommented)" {
    local f
    f=$(mktemp)
    printf '%s\n' \
        '#PermitRootLogin prohibit-password' \
        'PermitRootLogin no' \
        'OtherOption value' \
        '#PermitRootLogin without-password' > "$f"

    enable_sshd_root_login "$f"

    [ "$(grep -c '^PermitRootLogin yes$' "$f")" -eq 3 ]
    ! grep -q '^PermitRootLogin no$' "$f"
    grep -q '^OtherOption value$' "$f"
    rm -f "$f"
}

@test "enable_sshd_root_login errors when no PermitRootLogin line is present" {
    local f
    f=$(mktemp)
    printf '%s\n' 'OnlyOtherOptions yes' > "$f"

    error() { echo "ERROR: $*" >&2; return 1; }
    run enable_sshd_root_login "$f"
    [ "$status" -ne 0 ]
    [[ "$output" == *"PermitRootLogin"* ]]
    ! grep -q 'PermitRootLogin' "$f"
    rm -f "$f"
}

#############################
# prepend_grub_cmdline_linux
#############################
# prepend_grub_cmdline_linux prepends a string to GRUB_CMDLINE_LINUX
# in /etc/default/grub. Errors loudly if the line isn't present, since
# silently doing nothing would leave the kernel without the parameter
# (e.g. cryptdevice= for LUKS — a missing prefix means the system
# can't unlock the root partition at boot).

@test "prepend_grub_cmdline_linux prepends to an empty GRUB_CMDLINE_LINUX" {
    local f
    f=$(mktemp)
    printf '%s\n' 'GRUB_CMDLINE_LINUX=""' > "$f"

    prepend_grub_cmdline_linux "cryptdevice=UUID=abc123:root " "$f"

    grep -qF 'GRUB_CMDLINE_LINUX="cryptdevice=UUID=abc123:root "' "$f"
    rm -f "$f"
}

@test "prepend_grub_cmdline_linux preserves text already inside GRUB_CMDLINE_LINUX" {
    local f
    f=$(mktemp)
    printf '%s\n' 'GRUB_CMDLINE_LINUX="quiet splash"' > "$f"

    prepend_grub_cmdline_linux "cryptdevice=UUID=abc:root " "$f"

    grep -qF 'GRUB_CMDLINE_LINUX="cryptdevice=UUID=abc:root quiet splash"' "$f"
    rm -f "$f"
}

@test "prepend_grub_cmdline_linux preserves other lines in /etc/default/grub" {
    local f
    f=$(mktemp)
    printf '%s\n' \
        'GRUB_DEFAULT=0' \
        'GRUB_TIMEOUT=5' \
        'GRUB_CMDLINE_LINUX=""' \
        'GRUB_DISABLE_RECOVERY=true' > "$f"

    prepend_grub_cmdline_linux "cryptdevice=UUID=abc:root " "$f"

    grep -qF 'GRUB_DEFAULT=0' "$f"
    grep -qF 'GRUB_TIMEOUT=5' "$f"
    grep -qF 'GRUB_CMDLINE_LINUX="cryptdevice=UUID=abc:root "' "$f"
    grep -qF 'GRUB_DISABLE_RECOVERY=true' "$f"
    rm -f "$f"
}

@test "prepend_grub_cmdline_linux errors when GRUB_CMDLINE_LINUX line is absent" {
    local f
    f=$(mktemp)
    printf '%s\n' \
        'GRUB_DEFAULT=0' \
        'GRUB_TIMEOUT=5' > "$f"

    error() { echo "ERROR: $*" >&2; return 1; }
    run prepend_grub_cmdline_linux "cryptdevice=UUID=abc:root " "$f"
    [ "$status" -ne 0 ]
    [[ "$output" == *"GRUB_CMDLINE_LINUX"* ]]
    ! grep -q 'cryptdevice' "$f"
    rm -f "$f"
}

#############################
# ensure_initramfs_files
#############################
# ensure_initramfs_files sets mkinitcpio.conf's FILES= line to the
# given value, replacing an existing line or appending one if absent.
# Self-healing rather than error-on-miss: FILES= is optional, so a
# missing line means "no extra files," not a broken config.

@test "ensure_initramfs_files replaces an empty FILES= line" {
    local f
    f=$(mktemp)
    printf '%s\n' 'FILES=()' > "$f"

    ensure_initramfs_files "/etc/luks.key" "$f"

    grep -qF 'FILES=(/etc/luks.key)' "$f"
    rm -f "$f"
}

@test "ensure_initramfs_files replaces a FILES= line that lists a different value" {
    local f
    f=$(mktemp)
    printf '%s\n' 'FILES=(/etc/old-key)' > "$f"

    ensure_initramfs_files "/etc/luks.key" "$f"

    grep -qF 'FILES=(/etc/luks.key)' "$f"
    ! grep -qF '/etc/old-key' "$f"
    rm -f "$f"
}

@test "ensure_initramfs_files appends FILES= when the line is absent" {
    local f
    f=$(mktemp)
    printf '%s\n' \
        'MODULES=()' \
        'BINARIES=()' \
        'HOOKS=(base udev)' > "$f"

    ensure_initramfs_files "/etc/luks.key" "$f"

    grep -qF 'FILES=(/etc/luks.key)' "$f"
    grep -qF 'MODULES=()' "$f"
    grep -qF 'HOOKS=(base udev)' "$f"
    rm -f "$f"
}

#############################
# required_commands
#############################

@test "required_commands zfs includes zpool and zfs" {
    run required_commands zfs
    [ "$status" -eq 0 ]
    [[ "$output" == *"zpool"* ]]
    [[ "$output" == *"zfs"* ]]
}

@test "required_commands btrfs includes mkfs.btrfs and grub-install" {
    run required_commands btrfs
    [ "$status" -eq 0 ]
    [[ "$output" == *"mkfs.btrfs"* ]]
    [[ "$output" == *"grub-install"* ]]
}

@test "required_commands zfs excludes Btrfs-specific commands" {
    run required_commands zfs
    [ "$status" -eq 0 ]
    [[ "$output" != *"mkfs.btrfs"* ]]
    [[ "$output" != *"grub-install"* ]]
}

@test "required_commands btrfs excludes the zpool command" {
    run required_commands btrfs
    [ "$status" -eq 0 ]
    [[ "$output" != *"zpool"* ]]
}

@test "required_commands includes partitioning + pacstrap commands for both filesystems" {
    for fs in zfs btrfs; do
        run required_commands "$fs"
        [ "$status" -eq 0 ]
        [[ "$output" == *"sgdisk"* ]]
        [[ "$output" == *"wipefs"* ]]
        [[ "$output" == *"partprobe"* ]]
        [[ "$output" == *"mkfs.fat"* ]]
        [[ "$output" == *"pacstrap"* ]]
    done
}

@test "required_commands unknown filesystem returns 1" {
    run required_commands ext4
    [ "$status" -eq 1 ]
}

#############################
# append_aur_repo
#############################
# Appends an [aur] stanza to a pacman.conf for the baked local repo, the
# same shape as the [archzfs] handling. Idempotent: a second call is a
# no-op so re-running the installer doesn't stack duplicate stanzas.

@test "append_aur_repo adds the stanza with the given Server" {
    local f
    f=$(mktemp)
    printf '%s\n' '[options]' '[core]' > "$f"
    append_aur_repo "$f" "file:///usr/share/aur-packages"
    grep -q '^\[aur\]$' "$f"
    grep -q '^SigLevel = Optional TrustAll$' "$f"
    grep -q '^Server = file:///usr/share/aur-packages$' "$f"
    rm -f "$f"
}

@test "append_aur_repo preserves existing repos" {
    local f
    f=$(mktemp)
    printf '%s\n' '[core]' 'Include = /etc/pacman.d/mirrorlist' > "$f"
    append_aur_repo "$f" "file:///usr/share/aur-packages"
    grep -q '^\[core\]$' "$f"
    grep -q '^Include = /etc/pacman.d/mirrorlist$' "$f"
    rm -f "$f"
}

@test "append_aur_repo is idempotent — no duplicate [aur] on a second call" {
    local f
    f=$(mktemp)
    printf '%s\n' '[core]' > "$f"
    append_aur_repo "$f" "file:///usr/share/aur-packages"
    append_aur_repo "$f" "file:///usr/share/aur-packages"
    [ "$(grep -c '^\[aur\]$' "$f")" -eq 1 ]
    rm -f "$f"
}

#############################
# strip_repo_stanza
#############################
# Removes a named repo stanza (header + its config lines up to the next
# section) so the installed target's pacman.conf never references the baked
# [aur] repo path, which won't exist on the target.

@test "strip_repo_stanza removes the [aur] stanza and its config lines" {
    local f
    f=$(mktemp)
    printf '%s\n' \
        '[core]' 'Include = /etc/pacman.d/mirrorlist' \
        '' '[aur]' 'SigLevel = Optional TrustAll' \
        'Server = file:///usr/share/aur-packages' \
        '' '[extra]' 'Include = /etc/pacman.d/mirrorlist' > "$f"
    strip_repo_stanza aur "$f"
    ! grep -q '^\[aur\]$' "$f"
    ! grep -q 'aur-packages' "$f"
    rm -f "$f"
}

@test "strip_repo_stanza preserves sections before and after [aur]" {
    local f
    f=$(mktemp)
    printf '%s\n' \
        '[core]' 'Include = /etc/pacman.d/mirrorlist' \
        '[aur]' 'Server = file:///usr/share/aur-packages' \
        '[extra]' 'Include = /etc/pacman.d/mirrorlist' > "$f"
    strip_repo_stanza aur "$f"
    grep -q '^\[core\]$' "$f"
    grep -q '^\[extra\]$' "$f"
    rm -f "$f"
}

@test "strip_repo_stanza handles a stanza at end of file" {
    local f
    f=$(mktemp)
    printf '%s\n' \
        '[core]' 'Include = /etc/pacman.d/mirrorlist' \
        '[aur]' 'SigLevel = Optional TrustAll' \
        'Server = file:///usr/share/aur-packages' > "$f"
    strip_repo_stanza aur "$f"
    grep -q '^\[core\]$' "$f"
    ! grep -q '^\[aur\]$' "$f"
    ! grep -q 'aur-packages' "$f"
    rm -f "$f"
}

@test "strip_repo_stanza is a no-op when the stanza is absent" {
    local f before after
    f=$(mktemp)
    printf '%s\n' '[core]' '[extra]' > "$f"
    before=$(cat "$f")
    strip_repo_stanza aur "$f"
    after=$(cat "$f")
    [ "$before" = "$after" ]
    rm -f "$f"
}

#############################
# aur_repo_available
#############################

@test "aur_repo_available is true when aur.db is present" {
    local d
    d=$(mktemp -d)
    touch "$d/aur.db"
    run aur_repo_available "$d"
    [ "$status" -eq 0 ]
    rm -rf "$d"
}

@test "aur_repo_available is true when only the aur.db.tar.gz is present" {
    local d
    d=$(mktemp -d)
    touch "$d/aur.db.tar.gz"
    run aur_repo_available "$d"
    [ "$status" -eq 0 ]
    rm -rf "$d"
}

@test "aur_repo_available is false when the repo db is missing" {
    local d
    d=$(mktemp -d)
    run aur_repo_available "$d"
    [ "$status" -ne 0 ]
    rm -rf "$d"
}

#############################
# aur_manifest_names
#############################

@test "aur_manifest_names prints package names, skipping the header" {
    local f
    f=$(mktemp)
    printf 'name\tpkgbase\tfilename\n' > "$f"
    printf 'yay\tyay\tyay-12.0-1-x86_64.pkg.tar.zst\n' >> "$f"
    printf 'zrepl\tzrepl\tzrepl-0.6-1-x86_64.pkg.tar.zst\n' >> "$f"
    run aur_manifest_names "$f"
    [ "$status" -eq 0 ]
    [ "$(echo "$output" | wc -l)" -eq 2 ]
    [[ "$output" == *"yay"* ]]
    [[ "$output" == *"zrepl"* ]]
    [[ "$output" != *"name"* ]]
    rm -f "$f"
}

@test "aur_manifest_names emits nothing for a missing manifest" {
    run aur_manifest_names /nonexistent/manifest.tsv
    [ "$status" -eq 0 ]
    [ -z "$output" ]
}