aboutsummaryrefslogtreecommitdiff
path: root/scripts/tests/sync-check.bats
blob: df775b348af493a755afc3941941335a79bdfe47 (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
#!/usr/bin/env bats
#
# Tests for scripts/sync-check.sh
#
# Sandboxes a fake rulesets-shaped git repo under $BATS_TMPDIR, drops the
# real script in via PATH override, and exercises clean / drift / --fix
# behavior.

setup() {
    SANDBOX="$BATS_TEST_TMPDIR/repo"
    SCRIPT_SRC="$BATS_TEST_DIRNAME/../sync-check.sh"
    mkdir -p "$SANDBOX/scripts" "$SANDBOX/claude-templates/.ai/workflows" \
             "$SANDBOX/claude-templates/.ai/scripts" "$SANDBOX/.ai/workflows" \
             "$SANDBOX/.ai/scripts"
    cp "$SCRIPT_SRC" "$SANDBOX/scripts/sync-check.sh"
    chmod +x "$SANDBOX/scripts/sync-check.sh"
    cd "$SANDBOX"
    git init -q
    git config user.email test@example.com
    git config user.name test
    echo "protocols content" > claude-templates/.ai/protocols.org
    echo "protocols content" > .ai/protocols.org
    echo "workflow a" > claude-templates/.ai/workflows/a.org
    echo "workflow a" > .ai/workflows/a.org
    echo "script b" > claude-templates/.ai/scripts/b.sh
    echo "script b" > .ai/scripts/b.sh
}

@test "clean tree: exit 0" {
    run scripts/sync-check.sh
    [ "$status" -eq 0 ]
}

@test "drift in protocols.org: exit 1, names the file" {
    echo "drifted" > .ai/protocols.org
    run scripts/sync-check.sh
    [ "$status" -eq 1 ]
    [[ "$output" == *"protocols.org"* ]]
}

@test "drift in workflows/: exit 1, names workflows" {
    echo "workflow a modified" > .ai/workflows/a.org
    run scripts/sync-check.sh
    [ "$status" -eq 1 ]
    [[ "$output" == *"workflows"* ]]
}

@test "drift in scripts/: exit 1, names scripts" {
    echo "script b modified" > .ai/scripts/b.sh
    run scripts/sync-check.sh
    [ "$status" -eq 1 ]
    [[ "$output" == *"scripts"* ]]
}

@test "drift with --fix: syncs and exits 0" {
    echo "drifted" > .ai/protocols.org
    run scripts/sync-check.sh --fix
    [ "$status" -eq 0 ]
    run cat .ai/protocols.org
    [ "$output" = "protocols content" ]
}

@test "extra file in mirror: --delete removes it" {
    echo "stale" > .ai/workflows/stale.org
    run scripts/sync-check.sh --fix
    [ "$status" -eq 0 ]
    [ ! -f .ai/workflows/stale.org ]
}

@test "missing canonical: exit 2 with clear error" {
    rm -rf claude-templates
    run scripts/sync-check.sh
    [ "$status" -eq 2 ]
    [[ "$output" == *"not a rulesets-shaped repo"* ]]
}

@test "outside a git checkout: exit 2" {
    cd "$BATS_TEST_TMPDIR"
    mkdir not-a-repo
    cp "$SCRIPT_SRC" not-a-repo/sync-check.sh
    chmod +x not-a-repo/sync-check.sh
    cd not-a-repo
    run ./sync-check.sh
    [ "$status" -eq 2 ]
    [[ "$output" == *"not inside a git checkout"* ]]
}