-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-pack-loader.sh
More file actions
executable file
·197 lines (162 loc) · 4.77 KB
/
Copy pathtest-pack-loader.sh
File metadata and controls
executable file
·197 lines (162 loc) · 4.77 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
#!/usr/bin/env bash
# Standalone test for lib/packs.sh.
# Exercises the loader against temporary pack files to verify discovery,
# section extraction, env override, and fallback behavior.
#
# Run: bash tests/test-pack-loader.sh
# Exit: 0 on success, 1 on any failure.
set -uo pipefail
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
# shellcheck source=../lib/packs.sh
source "$ROOT_DIR/lib/packs.sh"
PASS=0
FAIL=0
FAILURES=()
assert_eq() {
local got="$1" want="$2" desc="$3"
if [ "$got" = "$want" ]; then
PASS=$((PASS + 1))
printf ' PASS %s\n' "$desc"
else
FAIL=$((FAIL + 1))
FAILURES+=("$desc")
printf ' FAIL %s\n' "$desc"
printf ' want: %q\n' "$want"
printf ' got: %q\n' "$got"
fi
}
assert_contains() {
local haystack="$1" needle="$2" desc="$3"
if [[ "$haystack" == *"$needle"* ]]; then
PASS=$((PASS + 1))
printf ' PASS %s\n' "$desc"
else
FAIL=$((FAIL + 1))
FAILURES+=("$desc")
printf ' FAIL %s\n' "$desc"
printf ' haystack: %q\n' "$haystack"
printf ' needle: %q\n' "$needle"
fi
}
echo ""
echo "=== load_pack_section ==="
TMP="$(mktemp -d -t pack-loader-test.XXXXXXXX)"
trap 'rm -rf "$TMP"' EXIT
cat > "$TMP/sample.txt" <<'EOF'
# This is a comment, ignored.
[positive_closeout]
done
implemented
ready
ready_with_padding
[negation]
not done
incomplete
[empty_section]
EOF
assert_eq \
"$(load_pack_section positive_closeout "$TMP/sample.txt")" \
"done|implemented|ready|ready_with_padding" \
"extract section, trim padding, ignore comments and blanks"
assert_eq \
"$(load_pack_section negation "$TMP/sample.txt")" \
"not done|incomplete" \
"extract a different section from the same file"
assert_eq \
"$(load_pack_section empty_section "$TMP/sample.txt")" \
"" \
"empty section returns empty string"
assert_eq \
"$(load_pack_section nonexistent_section "$TMP/sample.txt")" \
"" \
"missing section returns empty string"
assert_eq \
"$(load_pack_section positive_closeout "$TMP/does-not-exist.txt")" \
"" \
"missing file returns empty (no error)"
echo ""
echo "=== multi-file concatenation ==="
cat > "$TMP/extra.txt" <<'EOF'
[positive_closeout]
shipped
finished
EOF
assert_eq \
"$(load_pack_section positive_closeout "$TMP/sample.txt" "$TMP/extra.txt")" \
"done|implemented|ready|ready_with_padding|shipped|finished" \
"entries from multiple files concatenate in order"
echo ""
echo "=== active_locales ==="
# Explicit env wins over LANG
assert_eq \
"$(LLM_DARK_PATTERNS_LOCALE='pl,es' active_locales | tr '\n' ',')" \
"pl,es," \
"LLM_DARK_PATTERNS_LOCALE comma-separated, in order"
# LANG fallback when env unset: en + detected, in that order
assert_eq \
"$(unset LLM_DARK_PATTERNS_LOCALE; LANG=de_DE.UTF-8 active_locales | tr '\n' ',')" \
"en,de," \
"LANG fallback adds detected locale on top of base en"
# LANG=en* should not duplicate "en"
assert_eq \
"$(unset LLM_DARK_PATTERNS_LOCALE; LANG=en_US.UTF-8 active_locales | tr '\n' ',')" \
"en," \
"LANG=en* does not duplicate en"
# C/POSIX falls back to en only
assert_eq \
"$(unset LLM_DARK_PATTERNS_LOCALE; LANG=C active_locales)" \
"en" \
"C locale falls back to en only"
assert_eq \
"$(unset LLM_DARK_PATTERNS_LOCALE; LANG=POSIX active_locales)" \
"en" \
"POSIX locale falls back to en only"
assert_eq \
"$(unset LLM_DARK_PATTERNS_LOCALE LANG; active_locales)" \
"en" \
"no env at all falls back to en only"
# Explicit env wins entirely (no implicit en added)
assert_eq \
"$(LLM_DARK_PATTERNS_LOCALE='pl' active_locales | tr '\n' ',')" \
"pl," \
"explicit env override does not add implicit en"
echo ""
echo "=== resolve_pack_paths ==="
# With env override
output="$(LLM_DARK_PATTERNS_PACK_DIR=/tmp/custom resolve_pack_paths locale en)"
assert_contains \
"$output" \
"/tmp/custom/locale/en.txt" \
"explicit pack dir is in candidate list"
# XDG default
output="$(unset LLM_DARK_PATTERNS_PACK_DIR; resolve_pack_paths locale en)"
assert_contains \
"$output" \
"/llm-dark-patterns/packs/locale/en.txt" \
"XDG default path is in candidate list"
# Repo default
assert_contains \
"$output" \
"/packs/locale/en.txt" \
"repo-relative path is in candidate list"
echo ""
echo "=== load_locale_section integration ==="
# Build a temp pack + verify via env override
mkdir -p "$TMP/override/locale"
cat > "$TMP/override/locale/en.txt" <<'EOF'
[positive_closeout]
custom_done_verb
EOF
assert_contains \
"$(LLM_DARK_PATTERNS_PACK_DIR="$TMP/override" LLM_DARK_PATTERNS_LOCALE=en \
load_locale_section positive_closeout)" \
"custom_done_verb" \
"env-overridden pack is loaded by load_locale_section"
echo ""
echo "==============================="
echo "Total: $((PASS + FAIL)) Pass: $PASS Fail: $FAIL"
echo "==============================="
if [ "$FAIL" -gt 0 ]; then
exit 1
fi
exit 0