-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_launch.sh
More file actions
executable file
·1979 lines (1716 loc) · 67.8 KB
/
Copy pathtest_launch.sh
File metadata and controls
executable file
·1979 lines (1716 loc) · 67.8 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
#!/bin/bash
# shellcheck disable=SC2034
set -euo pipefail
# shellcheck source=_test_env.sh
source "$(dirname "${BASH_SOURCE[0]}")/_test_env.sh"
# Unit tests for launch.sh parsing logic.
# No Docker or API key required.
PASS=0
FAIL=0
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
TESTS_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=../lib/project.sh
source "$TESTS_DIR/../lib/project.sh"
assert_eq() {
local label="$1" expected="$2" actual="$3"
if [ "$expected" = "$actual" ]; then
echo " PASS: ${label}"
PASS=$((PASS + 1))
else
echo " FAIL: ${label}"
echo " expected: ${expected}"
echo " actual: ${actual}"
FAIL=$((FAIL + 1))
fi
}
# --- Helpers: same logic used in launch.sh ---
shorten_model() {
local m="$1"
local short="${m/claude-/}"
short="${short//\//-}"
echo "$short"
}
parse_inject_git_rules() { jq -r 'if has("inject_git_rules") then .inject_git_rules else true end' "$1"; }
parse_pp_prompt() { jq -r '.post_process.prompt // empty' "$1"; }
parse_pp_model() { jq -r '.post_process.model // "claude-opus-4-6"' "$1"; }
parse_pp_base_url() { jq -r '.post_process.base_url // empty' "$1"; }
parse_pp_api_key() { jq -r '.post_process.api_key // empty' "$1"; }
parse_pp_effort() { jq -r '.post_process.effort // empty' "$1"; }
parse_pp_max_idle() { jq -r '.post_process.max_idle // .max_idle // 3' "$1"; }
state_model_summary_for_test() {
jq -r \
'(.prompt // "") as $dp |
($dp | split("/") | .[-1] // "" | rtrimstr(".md")) as $dp_stem |
[.agents[] | (.count // 0) as $count | select($count > 0) |
"\($count)x \(.model | split("/") | .[-1])" +
(if .context == "none" then " ctx:bare"
elif .context == "slim" then " ctx:slim"
else "" end) +
(if .prompt and .prompt != $dp then
":" + (.prompt | split("/") | .[-1] | rtrimstr(".md") |
if startswith($dp_stem + "-")
then .[$dp_stem | length + 1:]
else .
end)
else "" end)] | join(", ")' "$1"
}
parse_num_agents() {
jq '[.agents[]? | (.count // 0)] | add // 0' "$1"
}
parse_agents_cfg() {
jq -r '.tag as $dt | .driver as $dd |
.agents[] | range(.count // 0) as $i |
[.model, (.base_url // ""), (.api_key // ""), (.effort // ""), (.auth // ""), (.context // ""), (.prompt // ""), (.auth_token // ""), (.tag // $dt // ""), (.driver // $dd // "")] | join("|")' "$1"
}
# Mirrors the per-agent credential selection in launch.sh.
resolve_agent_creds() {
local agent_auth="$1" agent_api_key="$2" global_api_key="$3" global_oauth="$4"
local resolved_key="" oauth_env=""
case "${agent_auth}" in
oauth)
resolved_key=""
oauth_env="CLAUDE_CODE_OAUTH_TOKEN=${global_oauth}"
;;
apikey)
resolved_key="${agent_api_key:-${global_api_key}}"
oauth_env=""
;;
*)
resolved_key="${agent_api_key:-${global_api_key}}"
[ -n "$global_oauth" ] && oauth_env="CLAUDE_CODE_OAUTH_TOKEN=${global_oauth}"
;;
esac
echo "${resolved_key}|${oauth_env}"
}
# Mirrors the auth_label computation in launch.sh (after credential resolution).
# Args: agent_auth agent_api_key agent_auth_token resolved_api_key global_oauth
resolve_auth_label() {
local agent_auth="$1" agent_api_key="$2" agent_auth_token="$3"
local resolved_api_key="$4" global_oauth="$5"
local auth_label=""
if [ -n "$agent_auth_token" ]; then
auth_label="token"
elif [ "$agent_auth" = "oauth" ]; then
auth_label="oauth"
elif [ "$agent_auth" = "apikey" ]; then
auth_label="key"
elif [ -n "$agent_api_key" ]; then
auth_label="key"
elif [ -n "$resolved_api_key" ] && [ -n "$global_oauth" ]; then
auth_label="auto"
elif [ -n "$resolved_api_key" ]; then
auth_label="key"
elif [ -n "$global_oauth" ]; then
auth_label="oauth"
fi
echo "$auth_label"
}
# Mirrors the validation guard in cmd_start().
check_auth() {
local api_key="$1" oauth_token="$2" config_file="$3"
if [ -z "$api_key" ] && [ -z "$oauth_token" ] && [ -z "$config_file" ]; then
echo "fail"
else
echo "pass"
fi
}
# Mirrors the EXTRA_ENV construction for CLAUDE_CODE_OAUTH_TOKEN.
build_oauth_extra_env() {
local token="$1"
local -a EXTRA_ENV=()
[ -n "$token" ] \
&& EXTRA_ENV+=(-e "CLAUDE_CODE_OAUTH_TOKEN=${token}")
echo "${EXTRA_ENV[*]+"${EXTRA_ENV[*]}"}"
}
# ============================================================
echo "=== 0. Project ID sanitization ==="
assert_eq "lowercase project unchanged" \
"claude-swarm" "$(swarm_project_id "claude-swarm")"
assert_eq "uppercase project lowercased" \
"leanmultisig-swarm" "$(swarm_project_id "leanMultisig-swarm")"
assert_eq "spaces collapse to separator" \
"my-repo" "$(swarm_project_id "My Repo!")"
assert_eq "leading/trailing separators trimmed" \
"repo" "$(swarm_project_id "_Repo.")"
assert_eq "empty/all separators fallback" \
"swarm" "$(swarm_project_id "---")"
# ============================================================
echo "=== 1. Model name shortening ==="
assert_eq "opus" "opus-4-6" "$(shorten_model "claude-opus-4-6")"
assert_eq "sonnet" "sonnet-4-5" "$(shorten_model "claude-sonnet-4-5")"
assert_eq "haiku" "haiku-4-5" "$(shorten_model "claude-haiku-4-5")"
assert_eq "openrouter" "openrouter-custom" "$(shorten_model "openrouter/custom")"
assert_eq "no prefix" "MiniMax-M2.5" "$(shorten_model "MiniMax-M2.5")"
assert_eq "double slash" "a-b-c" "$(shorten_model "a/b/c")"
# ============================================================
echo ""
echo "=== 3. inject_git_rules config ==="
cat > "$TMPDIR/default.json" <<'EOF'
{ "prompt": "p.md", "agents": [{ "count": 1, "model": "m" }] }
EOF
cat > "$TMPDIR/inject_false.json" <<'EOF'
{ "prompt": "p.md", "inject_git_rules": false, "agents": [{ "count": 1, "model": "m" }] }
EOF
cat > "$TMPDIR/inject_true.json" <<'EOF'
{ "prompt": "p.md", "inject_git_rules": true, "agents": [{ "count": 1, "model": "m" }] }
EOF
assert_eq "default is true" "true" "$(parse_inject_git_rules "$TMPDIR/default.json")"
assert_eq "explicit false" "false" "$(parse_inject_git_rules "$TMPDIR/inject_false.json")"
assert_eq "explicit true" "true" "$(parse_inject_git_rules "$TMPDIR/inject_true.json")"
# ============================================================
echo ""
echo "=== 4. Post-process config parsing ==="
cat > "$TMPDIR/pp_full.json" <<'EOF'
{
"prompt": "p.md",
"agents": [{ "count": 1, "model": "m" }],
"post_process": {
"prompt": "review.md",
"model": "claude-sonnet-4-5",
"base_url": "https://example.com",
"api_key": "sk-pp-test"
}
}
EOF
assert_eq "pp prompt" "review.md" "$(parse_pp_prompt "$TMPDIR/pp_full.json")"
assert_eq "pp model" "claude-sonnet-4-5" "$(parse_pp_model "$TMPDIR/pp_full.json")"
assert_eq "pp base_url" "https://example.com" "$(parse_pp_base_url "$TMPDIR/pp_full.json")"
assert_eq "pp api_key" "sk-pp-test" "$(parse_pp_api_key "$TMPDIR/pp_full.json")"
assert_eq "pp max_idle default" "3" "$(parse_pp_max_idle "$TMPDIR/pp_full.json")"
cat > "$TMPDIR/pp_minimal.json" <<'EOF'
{
"prompt": "p.md",
"agents": [{ "count": 1, "model": "m" }],
"post_process": { "prompt": "review.md" }
}
EOF
assert_eq "pp model default" "claude-opus-4-6" "$(parse_pp_model "$TMPDIR/pp_minimal.json")"
assert_eq "pp base_url empty" "" "$(parse_pp_base_url "$TMPDIR/pp_minimal.json")"
assert_eq "pp api_key empty" "" "$(parse_pp_api_key "$TMPDIR/pp_minimal.json")"
assert_eq "pp max_idle minimal" "3" "$(parse_pp_max_idle "$TMPDIR/pp_minimal.json")"
cat > "$TMPDIR/pp_idle.json" <<'EOF'
{
"prompt": "p.md",
"max_idle": 5,
"agents": [{ "count": 1, "model": "m" }],
"post_process": { "prompt": "review.md", "max_idle": 3 }
}
EOF
assert_eq "pp max_idle explicit" "3" \
"$(parse_pp_max_idle "$TMPDIR/pp_idle.json")"
cat > "$TMPDIR/pp_idle_inherit.json" <<'EOF'
{
"prompt": "p.md",
"max_idle": 7,
"agents": [{ "count": 1, "model": "m" }],
"post_process": { "prompt": "review.md" }
}
EOF
assert_eq "pp max_idle inherits top-level" "7" \
"$(parse_pp_max_idle "$TMPDIR/pp_idle_inherit.json")"
cat > "$TMPDIR/no_pp.json" <<'EOF'
{ "prompt": "p.md", "agents": [{ "count": 1, "model": "m" }] }
EOF
assert_eq "no pp prompt" "" "$(parse_pp_prompt "$TMPDIR/no_pp.json")"
assert_eq "no pp max_idle" "3" "$(parse_pp_max_idle "$TMPDIR/no_pp.json")"
# ============================================================
echo ""
echo "=== 5. Git user name is clean (no model tag) ==="
GIT_USER_NAME="swarm-agent"
assert_eq "default name clean" "swarm-agent" "$GIT_USER_NAME"
GIT_USER_NAME="Nikos Baxevanis"
assert_eq "custom name clean" "Nikos Baxevanis" "$GIT_USER_NAME"
# ============================================================
echo ""
echo "=== 6. Effort in agent TSV (config path) ==="
cat > "$TMPDIR/effort.json" <<'EOF'
{
"prompt": "p.md",
"agents": [
{ "count": 1, "model": "claude-opus-4-6", "effort": "high" },
{ "count": 2, "model": "claude-sonnet-4-6", "effort": "medium" },
{ "count": 1, "model": "claude-haiku-4-5" }
]
}
EOF
CFG=$(parse_agents_cfg "$TMPDIR/effort.json")
LINE1=$(echo "$CFG" | sed -n '1p')
LINE2=$(echo "$CFG" | sed -n '2p')
LINE4=$(echo "$CFG" | sed -n '4p')
IFS='|' read -r m1 u1 k1 e1 a1 c1 p1 t1 g1 d1 <<< "$LINE1"
assert_eq "opus effort" "high" "$e1"
IFS='|' read -r m2 u2 k2 e2 a2 c2 p2 t2 g2 d2 <<< "$LINE2"
assert_eq "sonnet effort" "medium" "$e2"
IFS='|' read -r m4 u4 k4 e4 a4 c4 p4 t4 g4 d4 <<< "$LINE4"
assert_eq "haiku effort (empty)" "" "$e4"
cat > "$TMPDIR/omitted_count.json" <<'EOF'
{
"prompt": "p.md",
"agents": [
{ "name": "headless", "count": 1, "model": "claude-opus-4-6" },
{ "name": "manual", "model": "gpt-5.4", "driver": "codex-cli" }
]
}
EOF
assert_eq "omitted count defaults to zero" "1" \
"$(parse_num_agents "$TMPDIR/omitted_count.json")"
assert_eq "omitted count not expanded into agents cfg" "1" \
"$(parse_agents_cfg "$TMPDIR/omitted_count.json" | wc -l | tr -d ' ')"
cat > "$TMPDIR/state_summary_interactive.json" <<'EOF'
{
"prompt": "prompts/headless.md",
"agents": [
{
"name": "headless",
"count": 2,
"model": "claude-opus-4-6",
"context": "slim"
},
{
"name": "manual-omitted",
"model": "claude-opus-4-6",
"context": "slim",
"prompt": "prompts/headless-manual.md"
},
{
"name": "manual-zero",
"count": 0,
"model": "gpt-5.4",
"context": "slim",
"prompt": "prompts/headless-manual.md"
}
]
}
EOF
assert_eq "state summary skips interactive-only profiles" \
"2x claude-opus-4-6 ctx:slim" \
"$(state_model_summary_for_test "$TMPDIR/state_summary_interactive.json")"
# ============================================================
echo ""
echo "=== 7. Effort in post-process ==="
cat > "$TMPDIR/pp_effort.json" <<'EOF'
{
"prompt": "p.md",
"agents": [{ "count": 1, "model": "m" }],
"post_process": {
"prompt": "review.md",
"model": "claude-opus-4-6",
"effort": "low"
}
}
EOF
assert_eq "pp effort" "low" "$(parse_pp_effort "$TMPDIR/pp_effort.json")"
assert_eq "pp effort absent" "" "$(parse_pp_effort "$TMPDIR/no_pp.json")"
# ============================================================
echo ""
echo "=== 9. OAuth auth validation ==="
assert_eq "api_key only" "pass" "$(check_auth "sk-key" "" "")"
assert_eq "oauth only" "pass" "$(check_auth "" "sk-ant-oat01-tok" "")"
assert_eq "both set" "pass" "$(check_auth "sk-key" "sk-ant-oat01-tok" "")"
assert_eq "config only" "pass" "$(check_auth "" "" "swarm.json")"
assert_eq "nothing set" "fail" "$(check_auth "" "" "")"
assert_eq "oauth + config" "pass" "$(check_auth "" "sk-ant-oat01-tok" "swarm.json")"
# ============================================================
echo ""
echo "=== 10. OAuth EXTRA_ENV construction ==="
assert_eq "oauth env set" \
"-e CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-test" \
"$(build_oauth_extra_env "sk-ant-oat01-test")"
assert_eq "oauth env empty" "" "$(build_oauth_extra_env "")"
# ============================================================
echo ""
echo "=== 11. Per-agent auth field in TSV ==="
cat > "$TMPDIR/auth_mixed.json" <<'EOF'
{
"prompt": "p.md",
"agents": [
{ "count": 1, "model": "claude-opus-4-6", "auth": "apikey" },
{ "count": 1, "model": "claude-opus-4-6", "auth": "oauth" },
{ "count": 1, "model": "MiniMax-M2.5", "base_url": "https://api.minimax.io", "api_key": "sk-mm" }
]
}
EOF
CFG=$(parse_agents_cfg "$TMPDIR/auth_mixed.json")
LINE1=$(echo "$CFG" | sed -n '1p')
LINE2=$(echo "$CFG" | sed -n '2p')
LINE3=$(echo "$CFG" | sed -n '3p')
IFS='|' read -r m1 u1 k1 e1 a1 c1 p1 t1 g1 d1 <<< "$LINE1"
assert_eq "auth apikey" "apikey" "$a1"
assert_eq "auth apikey model" "claude-opus-4-6" "$m1"
IFS='|' read -r m2 u2 k2 e2 a2 c2 p2 t2 g2 d2 <<< "$LINE2"
assert_eq "auth oauth" "oauth" "$a2"
IFS='|' read -r m3 u3 k3 e3 a3 c3 p3 t3 g3 d3 <<< "$LINE3"
assert_eq "auth custom (empty)" "" "$a3"
assert_eq "auth custom key" "sk-mm" "$k3"
# ============================================================
echo ""
echo "=== 12. Per-agent credential resolution ==="
RESULT=$(resolve_agent_creds "oauth" "" "sk-global" "sk-oat-tok")
IFS='|' read -r rk re <<< "$RESULT"
assert_eq "oauth: api_key cleared" "" "$rk"
assert_eq "oauth: token passed" "CLAUDE_CODE_OAUTH_TOKEN=sk-oat-tok" "$re"
RESULT=$(resolve_agent_creds "apikey" "" "sk-global" "sk-oat-tok")
IFS='|' read -r rk re <<< "$RESULT"
assert_eq "apikey: api_key set" "sk-global" "$rk"
assert_eq "apikey: no token" "" "$re"
RESULT=$(resolve_agent_creds "" "" "sk-global" "sk-oat-tok")
IFS='|' read -r rk re <<< "$RESULT"
assert_eq "default: api_key set" "sk-global" "$rk"
assert_eq "default: token passed" "CLAUDE_CODE_OAUTH_TOKEN=sk-oat-tok" "$re"
RESULT=$(resolve_agent_creds "" "sk-agent" "sk-global" "sk-oat-tok")
IFS='|' read -r rk re <<< "$RESULT"
assert_eq "custom key overrides" "sk-agent" "$rk"
RESULT=$(resolve_agent_creds "" "" "sk-global" "")
IFS='|' read -r rk re <<< "$RESULT"
assert_eq "no oauth: api_key set" "sk-global" "$rk"
assert_eq "no oauth: no token" "" "$re"
# ============================================================
echo ""
echo "=== 13. Context field in agent TSV ==="
cat > "$TMPDIR/context.json" <<'EOF'
{
"prompt": "p.md",
"agents": [
{ "count": 1, "model": "claude-opus-4-6", "effort": "high" },
{ "count": 1, "model": "claude-opus-4-6", "context": "none" },
{ "count": 1, "model": "claude-sonnet-4-6", "context": "slim" }
]
}
EOF
CFG=$(parse_agents_cfg "$TMPDIR/context.json")
LINE1=$(echo "$CFG" | sed -n '1p')
LINE2=$(echo "$CFG" | sed -n '2p')
LINE3=$(echo "$CFG" | sed -n '3p')
IFS='|' read -r m1 u1 k1 e1 a1 c1 p1 t1 g1 d1 <<< "$LINE1"
assert_eq "context default (empty)" "" "$c1"
IFS='|' read -r m2 u2 k2 e2 a2 c2 p2 t2 g2 d2 <<< "$LINE2"
assert_eq "context none" "none" "$c2"
IFS='|' read -r m3 u3 k3 e3 a3 c3 p3 t3 g3 d3 <<< "$LINE3"
assert_eq "context slim" "slim" "$c3"
# ============================================================
echo ""
echo "=== 14. Prompt field in agent TSV ==="
cat > "$TMPDIR/per_prompt.json" <<'EOF'
{
"prompt": "tasks/default.md",
"agents": [
{ "count": 1, "model": "claude-opus-4-6" },
{ "count": 1, "model": "claude-opus-4-6", "prompt": "tasks/review.md" },
{ "count": 1, "model": "claude-sonnet-4-6", "prompt": "tasks/explore.md", "context": "none" }
]
}
EOF
CFG=$(parse_agents_cfg "$TMPDIR/per_prompt.json")
LINE1=$(echo "$CFG" | sed -n '1p')
LINE2=$(echo "$CFG" | sed -n '2p')
LINE3=$(echo "$CFG" | sed -n '3p')
IFS='|' read -r m1 u1 k1 e1 a1 c1 p1 t1 g1 d1 <<< "$LINE1"
assert_eq "prompt default (empty)" "" "$p1"
IFS='|' read -r m2 u2 k2 e2 a2 c2 p2 t2 g2 d2 <<< "$LINE2"
assert_eq "prompt override" "tasks/review.md" "$p2"
IFS='|' read -r m3 u3 k3 e3 a3 c3 p3 t3 g3 d3 <<< "$LINE3"
assert_eq "prompt + context" "tasks/explore.md" "$p3"
assert_eq "context preserved" "none" "$c3"
# ============================================================
echo ""
echo "=== 15. Tag field in agent TSV ==="
cat > "$TMPDIR/tagged.json" <<'EOF'
{
"prompt": "p.md",
"agents": [
{ "count": 2, "model": "claude-opus-4-6", "tag": "explore" },
{ "count": 1, "model": "claude-sonnet-4-6", "tag": "review" },
{ "count": 1, "model": "claude-haiku-4-5" }
]
}
EOF
CFG=$(parse_agents_cfg "$TMPDIR/tagged.json")
LINE1=$(echo "$CFG" | sed -n '1p')
LINE3=$(echo "$CFG" | sed -n '3p')
LINE4=$(echo "$CFG" | sed -n '4p')
IFS='|' read -r m1 u1 k1 e1 a1 c1 p1 t1 g1 d1 <<< "$LINE1"
assert_eq "tag explore" "explore" "$g1"
IFS='|' read -r m3 u3 k3 e3 a3 c3 p3 t3 g3 d3 <<< "$LINE3"
assert_eq "tag review" "review" "$g3"
IFS='|' read -r m4 u4 k4 e4 a4 c4 p4 t4 g4 d4 <<< "$LINE4"
assert_eq "tag empty" "" "$g4"
# ============================================================
echo ""
echo "=== 16. parse_start_args — dashboard flag ==="
_LAUNCH="$TESTS_DIR/../launch.sh"
eval "$(sed -n '/^parse_start_args()/,/^}/p' "$_LAUNCH")"
parse_start_args --dashboard
assert_eq "cli dashboard" "true" "$OPEN_DASHBOARD"
parse_start_args
assert_eq "default no dashboard" "false" "$OPEN_DASHBOARD"
# ============================================================
echo ""
echo "=== 17. parse_start_args — unknown flag errors ==="
if (parse_start_args --bogus 2>/dev/null); then
echo " FAIL: unknown flag should error"
FAIL=$((FAIL + 1))
else
echo " PASS: unknown flag rejected"
PASS=$((PASS + 1))
fi
if (parse_start_args --prompt foo.md 2>/dev/null); then
echo " FAIL: removed --prompt flag should error"
FAIL=$((FAIL + 1))
else
echo " PASS: --prompt rejected (config-only)"
PASS=$((PASS + 1))
fi
if (parse_start_args --model m 2>/dev/null); then
echo " FAIL: removed --model flag should error"
FAIL=$((FAIL + 1))
else
echo " PASS: --model rejected (config-only)"
PASS=$((PASS + 1))
fi
if (parse_start_args --agents 5 2>/dev/null); then
echo " FAIL: removed --agents flag should error"
FAIL=$((FAIL + 1))
else
echo " PASS: --agents rejected (config-only)"
PASS=$((PASS + 1))
fi
# ============================================================
echo ""
echo "=== 18. Interactive profile selection ==="
_INTERACTIVE_FUNCS="$TMPDIR/interactive_funcs.sh"
awk '
/^print_interactive_profiles\(\)[[:space:]]*\{/ { p = 1 }
/^cmd_interactive\(\)[[:space:]]*\{/ { exit }
p { print }
' "$_LAUNCH" > "$_INTERACTIVE_FUNCS"
# shellcheck source=/dev/null
source "$_INTERACTIVE_FUNCS"
cat > "$TMPDIR/interactive_named.json" <<'EOF'
{
"driver": "codex-cli",
"agents": [
{
"name": "hunter",
"count": 0,
"model": "gpt-5.4",
"effort": "xhigh",
"auth": "chatgpt"
},
{
"name": "triage",
"count": 1,
"model": "claude-opus-4-6",
"driver": "claude-code"
}
]
}
EOF
CONFIG_FILE="$TMPDIR/interactive_named.json"
select_interactive_profile hunter "" "$TMPDIR/profile.json"
assert_eq "select named profile" "gpt-5.4" \
"$(jq -r '.model' "$TMPDIR/profile.json")"
assert_eq "count zero profile selectable" "0" \
"$(jq -r '.count' "$TMPDIR/profile.json")"
select_interactive_profile "" "2" "$TMPDIR/profile.json"
assert_eq "select by agent index" "triage" \
"$(jq -r '.name' "$TMPDIR/profile.json")"
cat > "$TMPDIR/interactive_single.json" <<'EOF'
{
"agents": [
{ "count": 0, "model": "fake-model", "driver": "fake" }
]
}
EOF
CONFIG_FILE="$TMPDIR/interactive_single.json"
select_interactive_profile "" "" "$TMPDIR/profile.json"
assert_eq "single unnamed profile selected by default" "fake" \
"$(jq -r '.driver' "$TMPDIR/profile.json")"
CONFIG_FILE="$TMPDIR/interactive_named.json"
if (select_interactive_profile "" "" "$TMPDIR/profile.json" 2>/dev/null); then
echo " FAIL: ambiguous profile should error"
FAIL=$((FAIL + 1))
else
echo " PASS: ambiguous profile rejected"
PASS=$((PASS + 1))
fi
# ============================================================
echo ""
echo "=== 22. Auth label — credential source labels ==="
# auth_token set → "token"
assert_eq "auth_token → token" \
"token" \
"$(resolve_auth_label "" "" "sk-or-key" "" "")"
# auth_token set with auth field → still "token" (takes priority)
assert_eq "auth_token + auth:apikey → token" \
"token" \
"$(resolve_auth_label "apikey" "" "sk-or-key" "" "")"
# auth: "oauth" → "oauth"
assert_eq "auth:oauth → oauth" \
"oauth" \
"$(resolve_auth_label "oauth" "" "" "" "sk-oat-tok")"
# custom api_key (no auth field) → "key"
assert_eq "custom api_key → key" \
"key" \
"$(resolve_auth_label "" "sk-custom" "" "" "")"
# auth: "apikey" with resolved key → "key"
assert_eq "auth:apikey → key" \
"key" \
"$(resolve_auth_label "apikey" "" "" "sk-global" "")"
# auth: "apikey" with both host creds → still "key" (OAuth not forwarded)
assert_eq "auth:apikey + host oauth → key" \
"key" \
"$(resolve_auth_label "apikey" "" "" "sk-global" "sk-oat-tok")"
# default with both key + OAuth → "auto"
assert_eq "default key+oauth → auto" \
"auto" \
"$(resolve_auth_label "" "" "" "sk-global" "sk-oat-tok")"
# default with key only → "key"
assert_eq "default key only → key" \
"key" \
"$(resolve_auth_label "" "" "" "sk-global" "")"
# default with OAuth only → "oauth"
assert_eq "default oauth only → oauth" \
"oauth" \
"$(resolve_auth_label "" "" "" "" "sk-oat-tok")"
# nothing at all → empty
assert_eq "no creds → empty" \
"" \
"$(resolve_auth_label "" "" "" "" "")"
# ============================================================
# check_deps tests
# ============================================================
echo ""
echo "--- check_deps ---"
source "$TESTS_DIR/../lib/check-deps.sh"
# Present tools should pass silently (version warnings are
# expected on macOS where system bash is 3.2).
out=$(check_deps bash git 2>&1) || true
if [ "${BASH_VERSINFO[0]}" -ge 5 ]; then
assert_eq "present deps succeed" "" "$out"
else
echo " SKIP: system bash ${BASH_VERSION} triggers expected warning"
fi
# Missing tool should print error and list the tool name.
out=$(check_deps __no_such_tool__ 2>&1) || true
assert_eq "missing dep mentions tool" "true" \
"$([[ "$out" == *"__no_such_tool__"* ]] && echo true || echo false)"
assert_eq "missing dep says ERROR" "true" \
"$([[ "$out" == *"ERROR"* ]] && echo true || echo false)"
assert_eq "missing dep mentions README" "true" \
"$([[ "$out" == *"README"* ]] && echo true || echo false)"
# Mixed present and missing reports only the missing one.
out=$(check_deps bash __no_such_tool__ git 2>&1) || true
assert_eq "mixed deps lists missing" "true" \
"$([[ "$out" == *"__no_such_tool__"* ]] && echo true || echo false)"
assert_eq "mixed deps omits present" "false" \
"$([[ "$out" == *"bash"* ]] && echo true || echo false)"
# ============================================================
echo ""
echo "--- _ver_ge comparison ---"
assert_eq "ver_ge 1.6 >= 1.6" "0" "$(_ver_ge 1.6 1.6 && echo 0 || echo 1)"
assert_eq "ver_ge 1.8 >= 1.6" "0" "$(_ver_ge 1.8 1.6 && echo 0 || echo 1)"
assert_eq "ver_ge 2.0 >= 1.6" "0" "$(_ver_ge 2.0 1.6 && echo 0 || echo 1)"
assert_eq "ver_ge 1.5 < 1.6" "1" "$(_ver_ge 1.5 1.6 && echo 0 || echo 1)"
assert_eq "ver_ge 0.9 < 1.0" "1" "$(_ver_ge 0.9 1.0 && echo 0 || echo 1)"
assert_eq "ver_ge 24.0 >= 24.0" "0" "$(_ver_ge 24.0 24.0 && echo 0 || echo 1)"
assert_eq "ver_ge 29.3 >= 24.0" "0" "$(_ver_ge 29.3 24.0 && echo 0 || echo 1)"
assert_eq "ver_ge 23.9 < 24.0" "1" "$(_ver_ge 23.9 24.0 && echo 0 || echo 1)"
echo ""
echo "--- _dep_version extraction ---"
bash_ver=$(_dep_version bash)
assert_eq "bash ver non-empty" "true" \
"$([ -n "$bash_ver" ] && echo true || echo false)"
assert_eq "bash ver is dotted" "true" \
"$([[ "$bash_ver" == *.* ]] && echo true || echo false)"
git_ver=$(_dep_version git)
assert_eq "git ver non-empty" "true" \
"$([ -n "$git_ver" ] && echo true || echo false)"
jq_ver=$(_dep_version jq)
assert_eq "jq ver non-empty" "true" \
"$([ -n "$jq_ver" ] && echo true || echo false)"
assert_eq "unknown cmd returns error" "1" \
"$(_dep_version __no_such__ 2>/dev/null && echo 0 || echo 1)"
echo ""
echo "--- version warning output ---"
# Current system should produce no warnings (skip on macOS
# where system bash is 3.2, below tested minimum).
warn_out=$(check_deps bash git jq 2>&1) || true
if [ "${BASH_VERSINFO[0]}" -ge 5 ]; then
assert_eq "no warnings on current system" "" "$warn_out"
else
echo " SKIP: system bash ${BASH_VERSION} below minimum (expected)"
fi
# SWARM_SKIP_DEP_CHECK silences warnings.
warn_out=$(SWARM_SKIP_DEP_CHECK=1 check_deps bash git jq 2>&1) || true
assert_eq "skip dep check silences" "" "$warn_out"
# ============================================================
# Script-level dependency guard integration tests.
# Build a minimal PATH with only basic utilities so that
# jq, docker, bc, tput are genuinely absent.
# ============================================================
echo ""
echo "--- check_deps integration ---"
FAKE_BIN=$(mktemp -d)
trap 'rm -rf "$FAKE_BIN"' EXIT
for cmd in bash dirname basename cat date git; do
p=$(command -v "$cmd" 2>/dev/null) && ln -s "$p" "$FAKE_BIN/"
done
# launch.sh --help should exit 0 even without jq/docker.
out=$(PATH="$FAKE_BIN" bash "$TESTS_DIR/../launch.sh" --help 2>&1) \
&& rc=0 || rc=$?
assert_eq "launch --help exits 0 without jq" "0" "$rc"
assert_eq "launch help says wait does not start agents" "true" \
"$([[ "$out" == *"Does not"* && "$out" == *"start agents"* ]] \
&& echo true || echo false)"
assert_eq "launch help says post-process harvests" "true" \
"$([[ "$out" == *"Run only the post-processing agent"* \
&& "$out" == *"harvest"* ]] && echo true || echo false)"
assert_eq "launch help lists interactive command" "true" \
"$([[ "$out" == *"interactive PROFILE"* \
&& "$out" == *"agent profile"* ]] && echo true || echo false)"
# dashboard.sh --help should exit 0 even without jq/docker/tput/bc.
out=$(PATH="$FAKE_BIN" bash "$TESTS_DIR/../dashboard.sh" --help 2>&1) \
&& rc=0 || rc=$?
assert_eq "dashboard --help exits 0 without jq" "0" "$rc"
# launch.sh start should fail and mention missing tools.
out=$(PATH="$FAKE_BIN" bash "$TESTS_DIR/../launch.sh" start 2>&1) \
&& rc=0 || rc=$?
assert_eq "launch start exits nonzero without jq" "1" "$rc"
assert_eq "launch start error mentions jq" "true" \
"$([[ "$out" == *"jq"* ]] && echo true || echo false)"
# dashboard.sh (no args) should fail and mention missing tools.
out=$(PATH="$FAKE_BIN" bash "$TESTS_DIR/../dashboard.sh" 2>&1) \
&& rc=0 || rc=$?
assert_eq "dashboard exits nonzero without jq" "1" "$rc"
assert_eq "dashboard error mentions jq" "true" \
"$([[ "$out" == *"jq"* ]] && echo true || echo false)"
rm -rf "$FAKE_BIN"
trap 'rm -rf "$TMPDIR"' EXIT
# ============================================================
echo ""
echo "=== 18. Swarmfile is required ==="
# launch.sh start without a config should fail with a clear message.
# Must run from a git repo (launch.sh needs git rev-parse).
# Requires docker in PATH (check_deps runs before config check).
if command -v docker &>/dev/null; then
_no_cfg_dir=$(mktemp -d)
git -C "$_no_cfg_dir" init -q
out=$(cd "$_no_cfg_dir" && SWARM_CONFIG="" bash "$TESTS_DIR/../launch.sh" start 2>&1) \
&& rc=0 || rc=$?
rm -rf "$_no_cfg_dir"
assert_eq "no config exits nonzero" "1" "$rc"
assert_eq "no config says swarmfile" "true" \
"$([[ "$out" == *"swarmfile"* ]] && echo true || echo false)"
else
echo " SKIP: docker not available (macOS CI)"
fi
# ============================================================
echo ""
echo "=== SWARM_AGENTS derivation from config ==="
# Mirrors the logic in cmd_start() that reads AGENTS_CFG and builds
# a comma-separated list of unique drivers for the Docker build arg.
derive_swarm_agents() {
local cfg_file="$1" config_file="${2:-}" default_driver="claude-code"
local _swarm_agents="" _seen_agents=" "
while IFS='|' read -r _ _ _ _ _ _ _ _ _ _drv; do
_drv="${_drv:-${default_driver}}"
[[ "$_seen_agents" == *" $_drv "* ]] && continue
_seen_agents+="$_drv "
_swarm_agents="${_swarm_agents:+${_swarm_agents},}${_drv}"
done < "$cfg_file"
if [ -n "$config_file" ]; then
local _pp_drv
_pp_drv=$(jq -r '.post_process.driver // .driver // "claude-code"' "$config_file" 2>/dev/null || true)
if [[ "$_seen_agents" != *" $_pp_drv "* ]]; then
_swarm_agents="${_swarm_agents:+${_swarm_agents},}${_pp_drv}"
fi
fi
echo "$_swarm_agents"
}
# Single driver (no driver field in config).
# Format: model|base_url|api_key|effort|auth|context|prompt|auth_token|tag|driver (9 pipes)
: > "$TMPDIR/agents_single.cfg"
printf 'claude-opus-4-6|||||||||\n' >> "$TMPDIR/agents_single.cfg"
printf 'claude-sonnet-4-6|||||||||\n' >> "$TMPDIR/agents_single.cfg"
assert_eq "single driver default" "claude-code" \
"$(derive_swarm_agents "$TMPDIR/agents_single.cfg")"
# Mixed drivers.
: > "$TMPDIR/agents_mixed.cfg"
printf 'claude-opus-4-6|||||||||claude-code\n' >> "$TMPDIR/agents_mixed.cfg"
printf 'gemini-2.5-pro|||||||||gemini-cli\n' >> "$TMPDIR/agents_mixed.cfg"
assert_eq "mixed drivers" "claude-code,gemini-cli" \
"$(derive_swarm_agents "$TMPDIR/agents_mixed.cfg")"
# Deduplication — multiple agents with same driver.
: > "$TMPDIR/agents_dedup.cfg"
printf 'gemini-2.5-pro|||||||||gemini-cli\n' >> "$TMPDIR/agents_dedup.cfg"
printf 'gemini-3-flash|||||||||gemini-cli\n' >> "$TMPDIR/agents_dedup.cfg"
assert_eq "dedup same driver" "gemini-cli" \
"$(derive_swarm_agents "$TMPDIR/agents_dedup.cfg")"
# Post-process adds a new driver.
: > "$TMPDIR/agents_pp.cfg"
printf 'gemini-2.5-pro|||||||||gemini-cli\n' >> "$TMPDIR/agents_pp.cfg"
cat > "$TMPDIR/pp_driver.json" <<'EOF'
{
"prompt": "p.md",
"agents": [{ "count": 1, "model": "gemini-2.5-pro", "driver": "gemini-cli" }],
"post_process": { "prompt": "r.md", "driver": "claude-code" }
}
EOF
assert_eq "pp adds driver" "gemini-cli,claude-code" \
"$(derive_swarm_agents "$TMPDIR/agents_pp.cfg" "$TMPDIR/pp_driver.json")"
# Post-process driver already present — no duplicate.
cat > "$TMPDIR/pp_same.json" <<'EOF'
{
"prompt": "p.md",
"agents": [{ "count": 1, "model": "claude-opus-4-6" }],
"post_process": { "prompt": "r.md" }
}
EOF
: > "$TMPDIR/agents_pp_same.cfg"
printf 'claude-opus-4-6|||||||||\n' >> "$TMPDIR/agents_pp_same.cfg"
assert_eq "pp same driver no dup" "claude-code" \
"$(derive_swarm_agents "$TMPDIR/agents_pp_same.cfg" "$TMPDIR/pp_same.json")"
# Post-process inherits top-level driver.
cat > "$TMPDIR/pp_inherit.json" <<'EOF'
{
"prompt": "p.md",
"driver": "gemini-cli",
"agents": [{ "count": 1, "model": "gemini-2.5-pro" }],
"post_process": { "prompt": "r.md" }
}
EOF
: > "$TMPDIR/agents_pp_inh.cfg"
printf 'gemini-2.5-pro|||||||||gemini-cli\n' >> "$TMPDIR/agents_pp_inh.cfg"
assert_eq "pp inherits top driver" "gemini-cli" \
"$(derive_swarm_agents "$TMPDIR/agents_pp_inh.cfg" "$TMPDIR/pp_inherit.json")"
# ============================================================
echo ""
echo "=== 13. Pricing extraction from config ==="
# Mirrors the jq pricing lookup in launch.sh.
extract_pricing() {
local config="$1" model="$2"
jq -r --arg m "$model" \
'.pricing[$m] // empty | "\(.input + 0) \(.output + 0) \((.cached // 0) + 0)"' \
"$config" 2>/dev/null || true
}
assert_eq "gemini-2.5-pro pricing" "1.25 10 0.13" \
"$(extract_pricing "$TESTS_DIR/configs/heterogeneous-kitchen-sink.json" "gemini-2.5-pro")"
assert_eq "gemini-3.1 pricing" "2 12 0.2" \
"$(extract_pricing "$TESTS_DIR/configs/heterogeneous-kitchen-sink.json" "gemini-3.1-pro-preview")"
assert_eq "gemini-3.1 customtools pricing" "2 12 0.2" \
"$(extract_pricing "$TESTS_DIR/configs/heterogeneous-kitchen-sink.json" "gemini-3.1-pro-preview-customtools")"
assert_eq "flash pricing" "0.5 3 0" \
"$(extract_pricing "$TESTS_DIR/configs/heterogeneous-kitchen-sink.json" "gemini-3-flash-preview")"
# Model not in pricing map — returns empty.
assert_eq "unlisted model empty" "" \
"$(extract_pricing "$TESTS_DIR/configs/heterogeneous-kitchen-sink.json" "claude-opus-4-6")"
# MiniMax-M2.7 pricing in kitchen-sink.json.
assert_eq "minimax-m2.7 pricing" "0.3 1.2 0.06" \
"$(extract_pricing "$TESTS_DIR/configs/kitchen-sink.json" "MiniMax-M2.7")"
# Config without pricing section — returns empty.
assert_eq "no pricing section" "" \
"$(extract_pricing "$TESTS_DIR/configs/gemini-only.json" "gemini-2.5-pro")"
# ============================================================
echo ""
echo "=== 29. claude_code_version field ==="
cat > "$TMPDIR/cc_pinned.json" <<'JSON'
{
"prompt": "unused",
"claude_code_version": "1.0.30",
"agents": [{"count": 1, "model": "claude-opus-4-6"}]
}
JSON
assert_eq "cc version present" "1.0.30" \
"$(jq -r '.claude_code_version // empty' "$TMPDIR/cc_pinned.json")"
cat > "$TMPDIR/cc_no_version.json" <<'JSON'
{
"prompt": "unused",
"agents": [{"count": 1, "model": "claude-opus-4-6"}]
}
JSON
assert_eq "cc version absent" "" \
"$(jq -r '.claude_code_version // empty' "$TMPDIR/cc_no_version.json")"
# Mirror of the cc version test for the codex_cli_version field
# parsed by launch.sh's --build-arg plumbing -- pins the jq filter
# so a future typo in either path is caught immediately.
cat > "$TMPDIR/codex_pinned.json" <<'JSON'
{
"prompt": "unused",
"codex_cli_version": "0.125.0",
"agents": [{"count": 1, "driver": "codex-cli", "model": "gpt-5.4"}]
}
JSON
assert_eq "codex version present" "0.125.0" \