autumn-cli 0.7.0

CLI tool for the Autumn web framework
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
//! Production deployment scaffolding for `autumn release init`.
//!
//! Emits a curated set of files (Dockerfile, .dockerignore, config example,
//! and optional target-specific scaffolds) at the project root.

use std::fs;
use std::path::{Path, PathBuf};

mod templates {
    pub const DOCKERFILE: &str = include_str!("templates/release/Dockerfile.tmpl");
    pub const DOCKERIGNORE: &str = include_str!("templates/release/.dockerignore.tmpl");
    pub const PRODUCTION_CONFIG: &str =
        include_str!("templates/release/autumn.production.toml.example.tmpl");
    pub const FLY_TOML: &str = include_str!("templates/release/fly.toml.tmpl");
    pub const DOCKER_COMPOSE: &str = include_str!("templates/release/docker-compose.yml.tmpl");
    pub const AZURE_MAIN_TF: &str = include_str!("templates/release/main.tf.tmpl");
    pub const AZURE_VARIABLES_TF: &str = include_str!("templates/release/variables.tf.tmpl");
    pub const AZURE_OUTPUTS_TF: &str = include_str!("templates/release/outputs.tf.tmpl");
    pub const AZURE_TFVARS_EXAMPLE: &str =
        include_str!("templates/release/terraform.tfvars.example.tmpl");
    pub const AZURE_DEPLOY_WORKFLOW: &str = include_str!("templates/release/azure-deploy.yml.tmpl");

    pub const AWS_APP_RUNNER_MAIN_TF: &str =
        include_str!("templates/release/aws-app-runner-main.tf.tmpl");
    pub const AWS_APP_RUNNER_VARIABLES_TF: &str =
        include_str!("templates/release/aws-app-runner-variables.tf.tmpl");
    pub const AWS_APP_RUNNER_OUTPUTS_TF: &str =
        include_str!("templates/release/aws-app-runner-outputs.tf.tmpl");
    pub const AWS_APP_RUNNER_TFVARS_EXAMPLE: &str =
        include_str!("templates/release/aws-app-runner-terraform.tfvars.example.tmpl");

    pub const AWS_ECS_MAIN_TF: &str = include_str!("templates/release/aws-ecs-main.tf.tmpl");
    pub const AWS_ECS_VARIABLES_TF: &str =
        include_str!("templates/release/aws-ecs-variables.tf.tmpl");
    pub const AWS_ECS_OUTPUTS_TF: &str = include_str!("templates/release/aws-ecs-outputs.tf.tmpl");
    pub const AWS_ECS_TFVARS_EXAMPLE: &str =
        include_str!("templates/release/aws-ecs-terraform.tfvars.example.tmpl");
    pub const AWS_DEPLOY_WORKFLOW: &str = include_str!("templates/release/aws-deploy.yml.tmpl");

    pub const GCP_CLOUD_RUN_MAIN_TF: &str =
        include_str!("templates/release/gcp-cloud-run-main.tf.tmpl");
    pub const GCP_CLOUD_RUN_VARIABLES_TF: &str =
        include_str!("templates/release/gcp-cloud-run-variables.tf.tmpl");
    pub const GCP_CLOUD_RUN_OUTPUTS_TF: &str =
        include_str!("templates/release/gcp-cloud-run-outputs.tf.tmpl");
    pub const GCP_CLOUD_RUN_TFVARS_EXAMPLE: &str =
        include_str!("templates/release/gcp-cloud-run-terraform.tfvars.example.tmpl");
    pub const GCP_DEPLOY_WORKFLOW: &str = include_str!("templates/release/gcp-deploy.yml.tmpl");
}

#[derive(Debug, thiserror::Error)]
pub enum ReleaseError {
    #[error("'{0}' already exists — run with --force to overwrite")]
    FileExists(String),

    #[error("failed to read Cargo.toml: {0}")]
    CargoToml(String),

    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Target {
    Default,
    Fly,
    DockerCompose,
    AzureContainerApps,
    AwsAppRunner,
    AwsEcs,
    GcpCloudRun,
}

impl std::str::FromStr for Target {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "fly" => Ok(Self::Fly),
            "docker-compose" => Ok(Self::DockerCompose),
            "azure-container-apps" => Ok(Self::AzureContainerApps),
            "aws-app-runner" => Ok(Self::AwsAppRunner),
            "aws-ecs" => Ok(Self::AwsEcs),
            "gcp-cloud-run" => Ok(Self::GcpCloudRun),
            other => Err(format!(
                "unknown target '{other}'; expected 'fly', 'docker-compose', \
                 'azure-container-apps', 'aws-app-runner', 'aws-ecs', or 'gcp-cloud-run'"
            )),
        }
    }
}

/// Whether `target` scaffolds a Terraform-based deployment (as opposed to
/// `fly`/`docker-compose`'s plain config files). Terraform targets share two
/// behaviors: a merged `.gitignore` (state files hold every secret in
/// plaintext) and, when their workflow lands under a nested Cargo workspace
/// member, the same GitHub Actions discoverability warning.
const fn is_terraform_target(target: Target) -> bool {
    matches!(
        target,
        Target::AzureContainerApps | Target::AwsAppRunner | Target::AwsEcs | Target::GcpCloudRun
    )
}

#[derive(Clone, Copy)]
pub enum ReleaseAction {
    Init {
        force: bool,
        target: Target,
        /// Scaffold a separate `worker` service (docker-compose target) that runs
        /// the app's worker role alongside a web-only `app` service.
        split_workers: bool,
    },
}

pub fn run(action: ReleaseAction) {
    eprintln!("🍂 autumn release\n");

    let cwd = std::env::current_dir().unwrap_or_else(|e| {
        eprintln!("Error: cannot determine current directory: {e}");
        std::process::exit(1);
    });

    match action {
        ReleaseAction::Init {
            force,
            target,
            split_workers,
        } => {
            let project_name = read_project_name(&cwd).unwrap_or_else(|e| {
                eprintln!("Error: {e}");
                std::process::exit(1);
            });

            match init(&cwd, &project_name, force, target, split_workers) {
                Ok(files) => {
                    for f in &files {
                        println!("  Created {f}");
                    }

                    let workflow_file = planned_files(target)
                        .into_iter()
                        .map(|(name, _)| name)
                        .find(|name| name.starts_with(".github/workflows/"));
                    if let Some(workflow_file) = workflow_file
                        && let Some(warning) =
                            nested_workflow_relocation_warning(&cwd, workflow_file)
                    {
                        eprintln!("\n{warning}");
                    }

                    // Smoke gate: verify the generated production config does
                    // not contain a committed signing secret literal.
                    let config_path = cwd.join("autumn.production.toml.example");
                    if let Ok(content) = std::fs::read_to_string(&config_path)
                        && let Err(e) = check_production_config_signing_secret(&content)
                    {
                        eprintln!("Warning: smoke gate failed for generated config: {e}");
                    }

                    println!();
                    println!("Next steps:");
                    println!(
                        "  1. Generate and set your signing secret (REQUIRED before production boot):"
                    );
                    println!(
                        "       export AUTUMN_SECURITY__SIGNING_SECRET=\"$(openssl rand -hex 32)\""
                    );
                    println!("     Smoke-gate check — the app must refuse to start without it:");
                    println!("       AUTUMN_ENV=prod docker run --rm \\");
                    println!("         -e AUTUMN_DATABASE__PRIMARY_URL=... \\");
                    println!("         {project_name} 2>&1 | grep -i 'signing secret'");
                    println!("     And must start with it:");
                    println!("       AUTUMN_ENV=prod docker run --rm \\");
                    println!("         -e AUTUMN_DATABASE__PRIMARY_URL=... \\");
                    println!(
                        "         -e AUTUMN_SECURITY__SIGNING_SECRET=\"$AUTUMN_SECURITY__SIGNING_SECRET\" \\"
                    );
                    println!("         {project_name}");
                    println!();
                    println!("  2. Build, migrate the primary once, then run web replicas:");
                    println!("       docker build -t {project_name} .");
                    println!("       AUTUMN_DATABASE__PRIMARY_URL=... autumn migrate");
                    println!(
                        "       docker run --rm -p 3000:3000 -e AUTUMN_DATABASE__PRIMARY_URL=... \\"
                    );
                    println!(
                        "         -e AUTUMN_SECURITY__SIGNING_SECRET=\"$AUTUMN_SECURITY__SIGNING_SECRET\" \\"
                    );
                    println!("         {project_name}");
                    println!();
                    println!(
                        "  See docs/guide/deployment.md and docs/guide/signing-secrets.md for the full walkthrough."
                    );
                }
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            }
        }
    }
}

/// Validate a generated production config file for signing-secret compliance.
///
/// Returns `Ok(())` when the config file correctly documents the signing
/// secret via an environment variable reference (not a committed literal value).
/// Returns `Err` with a human-readable explanation when the file contains a
/// committed secret literal.
///
/// Used by the release checklist smoke gate to verify that generated
/// deployment files obey the "never commit secrets" rule.
///
/// # Errors
///
/// Returns a string error message when a raw signing secret literal is found
/// in a non-comment line of `content`.
pub fn check_production_config_signing_secret(content: &str) -> Result<(), String> {
    for (line_num, line) in content.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            continue;
        }
        // A non-comment line containing a `secret =` assignment with a non-empty
        // RHS is a committed secret literal — a critical misconfiguration.
        if let Some(rest) = trimmed.strip_prefix("secret") {
            let rest = rest.trim_start();
            if let Some(rest) = rest.strip_prefix('=') {
                let value = rest.trim().trim_matches('"').trim_matches('\'');
                if !value.is_empty() && value != "[]" {
                    return Err(format!(
                        "line {}: production config contains a committed signing secret literal \
                         at `secret = ...`; use AUTUMN_SECURITY__SIGNING_SECRET env var instead",
                        line_num + 1,
                    ));
                }
            }
        }
    }
    Ok(())
}

pub fn read_project_name(dir: &Path) -> Result<String, ReleaseError> {
    let path = dir.join("Cargo.toml");
    let content = fs::read_to_string(&path)
        .map_err(|e| ReleaseError::CargoToml(format!("{}: {e}", path.display())))?;

    // Check for workspace root before parsing; workspace-only Cargo.toml files
    // may not parse cleanly as a member manifest.
    if content.contains("[workspace]") && !content.contains("[package]") {
        return Err(ReleaseError::CargoToml(
            "found [workspace] but no [package] — run this command from a member crate directory, not the workspace root".into(),
        ));
    }

    let parsed: toml::Value = toml::from_str(&content)
        .map_err(|e| ReleaseError::CargoToml(format!("parse error: {e}")))?;
    parsed
        .get("package")
        .and_then(|p| p.get("name"))
        .and_then(|n| n.as_str())
        .map(str::to_owned)
        .ok_or_else(|| ReleaseError::CargoToml("missing [package] name".into()))
}

/// Locate the nearest ancestor of `dir` (inclusive) containing a `.git`
/// entry. A worktree or submodule uses a `.git` FILE rather than a
/// directory, so this checks existence generally rather than requiring a
/// directory. Returns `None` if no ancestor up to the filesystem root has
/// one (`dir` isn't inside a git repository at all).
fn find_git_root(dir: &Path) -> Option<PathBuf> {
    let mut current = dir;
    loop {
        if current.join(".git").exists() {
            return Some(current.to_path_buf());
        }
        current = current.parent()?;
    }
}

/// For targets that scaffold a nested workflow file (currently
/// `.github/workflows/azure-deploy.yml` and `.github/workflows/aws-deploy.yml`),
/// `workflow_rel_path` is written under `dir` — but GitHub Actions only
/// discovers workflow files under the git repository ROOT's
/// `.github/workflows/`, never an arbitrary subdirectory's (see
/// <https://docs.github.com/en/actions/concepts/workflows-and-actions/workflows>).
/// `autumn release init` explicitly supports running from a Cargo workspace
/// member directory (`read_project_name` rejects only the workspace root
/// itself), so a scaffold run from `examples/blog/` writes a workflow that
/// would silently never fire. Returns an actionable warning to print in
/// that case; `None` when `dir` IS the git root (the common, correct case)
/// or isn't inside a git repository at all (nothing more specific to say).
fn nested_workflow_relocation_warning(dir: &Path, workflow_rel_path: &str) -> Option<String> {
    let git_root = find_git_root(dir)?;
    if git_root == dir {
        return None;
    }
    let rel = dir.strip_prefix(&git_root).ok()?;
    // The suggested `working-directory:` always runs inside the generated
    // workflow's own YAML on a Linux CI runner (every template here targets
    // `ubuntu-latest`), regardless of which OS `autumn release init` itself
    // ran on — so this must always render with forward slashes, even when
    // `rel.display()` would use `\` on a Windows host.
    let rel_forward_slash = rel
        .components()
        .map(|c| c.as_os_str().to_string_lossy().into_owned())
        .collect::<Vec<_>>()
        .join("/");
    Some(format!(
        "Warning: this project lives inside a Git repository whose root is\n\
         {}, but `{workflow_rel_path}` was written under\n\
         {} — GitHub Actions only discovers workflow files under the\n\
         repository ROOT's `.github/workflows/`, so this workflow will never\n\
         run as-is. Move it to {}/{workflow_rel_path} and add\n\
         the following so its `docker build` step still finds this crate's\n\
         Dockerfile:\n\
         \n\
         defaults:\n  run:\n    working-directory: {rel_forward_slash}\n",
        git_root.display(),
        dir.display(),
        git_root.display(),
    ))
}

/// Emit release scaffolding files into `dir` for the given `project_name`.
///
/// Returns the list of file names written. Returns [`ReleaseError::FileExists`]
/// if any output file already exists and `force` is `false`.
pub fn init(
    dir: &Path,
    project_name: &str,
    force: bool,
    target: Target,
    split_workers: bool,
) -> Result<Vec<String>, ReleaseError> {
    let files = planned_files(target);

    if !force {
        for (name, _) in &files {
            if dir.join(name).exists() {
                return Err(ReleaseError::FileExists(name.to_string()));
            }
        }
    }

    // Validate/merge .gitignore BEFORE writing any scaffold file below, not
    // after: this can fail (an existing .gitignore that isn't valid UTF-8,
    // say), and failing after the other files are already on disk would
    // leave a partial, complete-looking scaffold that then blocks a retry
    // without --force on the very files this call just created.
    if is_terraform_target(target) {
        ensure_terraform_gitignore_entries(dir)?;
    }

    // Embed assets into the binary only when the project opts in via the
    // `embed-assets` feature (as `autumn new` generates). Pre-existing apps
    // without that feature get the disk-based build (`cargo build --release`
    // plus `COPY static`), so their Docker builds keep working.
    let embed = project_has_embed_assets(dir);

    let mut created = Vec::new();
    for (name, template) in files {
        let path = dir.join(name);
        // Most planned files sit at the project root, but some targets (e.g.
        // azure-container-apps' `.github/workflows/...`) nest under a
        // subdirectory that a fresh project won't have yet.
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(&path, render(template, project_name, embed, split_workers))?;
        created.push(name.to_string());
    }

    Ok(created)
}

/// Terraform state (`*.tfstate*`) holds every secret value in plaintext —
/// `sensitive = true` on a variable only redacts CLI plan/apply output, never
/// the state file — and a real `terraform.tfvars` holds the operator's own
/// secret values. None of that may ever land in version control. Shared by
/// every Terraform-based target (`azure-container-apps`, `aws-app-runner`,
/// `aws-ecs`) — the comment names all three so it stays legible however the
/// project got its `.gitignore` merged.
const TERRAFORM_GITIGNORE_ENTRIES: &[&str] = &[
    "# Terraform (autumn release init --target azure-container-apps / aws-app-runner / aws-ecs / gcp-cloud-run)",
    ".terraform/",
    "*.tfstate",
    "*.tfstate.*",
    "terraform.tfvars",
];

/// Whether `pattern` is still in effect by the end of `existing`, applying
/// git's own "last matching rule wins" semantics. Deliberately conservative
/// rather than a full gitignore glob engine (`*`, `**`, `?`, character
/// classes, directory-anchoring rules): once `pattern` has appeared, ANY
/// later negation line — not just an exact `!pattern` match — is treated
/// as potentially un-ignoring it again, since a broader wildcard like
/// `!*.tfvars` or a blanket `!*` also defeats it and being unable to prove
/// a negation doesn't apply must never be mistaken for proof that it
/// doesn't. The cost of this conservatism is, at worst, an unrelated
/// negation elsewhere in the file causing a harmless re-append (a
/// duplicate line) on the next run — never a false "already protected".
fn gitignore_pattern_still_effective(existing: &str, pattern: &str) -> bool {
    let mut effective = false;
    for line in existing.lines() {
        let line = line.trim();
        if line == pattern {
            effective = true;
        } else if effective && line.starts_with('!') {
            effective = false;
        }
    }
    effective
}

/// Ensure `dir/.gitignore` excludes Terraform state and the operator's real
/// `terraform.tfvars`, merging into an existing file (creating one if
/// missing) without touching unrelated lines. Idempotent: a re-run (e.g.
/// under `--force`) never duplicates entries whose protection still holds —
/// but does re-append one whose earlier occurrence has since been negated,
/// since re-asserting it after the negation is the only way to make it the
/// final (and therefore effective) matching rule again.
fn ensure_terraform_gitignore_entries(dir: &Path) -> std::io::Result<()> {
    let path = dir.join(".gitignore");
    // Only a missing file is safe to default to empty. Any other read
    // failure (invalid UTF-8, permission denied, ...) must propagate —
    // silently treating it as "no file" would make the fs::write below
    // replace the operator's existing (unreadable-by-us, but still real)
    // content with just the Terraform entries.
    let existing = match fs::read_to_string(&path) {
        Ok(contents) => contents,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
        Err(e) => return Err(e),
    };
    let missing: Vec<&str> = TERRAFORM_GITIGNORE_ENTRIES
        .iter()
        .copied()
        .filter(|line| {
            if line.starts_with('#') {
                !existing.lines().any(|l| l.trim() == *line)
            } else {
                !gitignore_pattern_still_effective(&existing, line)
            }
        })
        .collect();
    if missing.is_empty() {
        return Ok(());
    }

    let mut updated = existing;
    if !updated.is_empty() {
        if !updated.ends_with('\n') {
            updated.push('\n');
        }
        updated.push('\n');
    }
    for line in missing {
        updated.push_str(line);
        updated.push('\n');
    }
    fs::write(path, updated)
}

// ── helpers ───────────────────────────────────────────────────────────────────

/// Whether the project at `dir` defines an `embed-assets` Cargo feature, i.e.
/// it is wired for single-binary embedded builds (`autumn build --embed`).
///
/// Parses the `[features]` table rather than substring-matching the file, so a
/// comment or unrelated text mentioning "embed-assets" doesn't cause
/// `autumn build --embed` (which would then fail for a project that lacks the
/// feature) to be baked into the generated Dockerfile.
fn project_has_embed_assets(dir: &Path) -> bool {
    let Ok(contents) = fs::read_to_string(dir.join("Cargo.toml")) else {
        return false;
    };
    let Ok(parsed) = toml::from_str::<toml::Table>(&contents) else {
        return false;
    };
    parsed
        .get("features")
        .and_then(toml::Value::as_table)
        .is_some_and(|features| features.contains_key("embed-assets"))
}

/// The extra `worker:` service block spliced into the docker-compose output when
/// `--split-workers` is set. Runs the SAME image (`build: .`) and default
/// entrypoint as the `app` service, but with `AUTUMN_ROLE=worker` so it runs the
/// job workers + scheduler only. Shares the app's database URL, signing secret,
/// and one-shot migration gate. Both tiers use the durable `postgres` jobs
/// backend so the queue is shared across the separate web and worker processes
/// (the in-process `local` backend can't span processes).
const WORKER_SERVICE_BLOCK: &str = "\n  worker:\n    build: .\n    environment:\n      \
AUTUMN_PROFILE: prod\n      AUTUMN_ROLE: worker\n      AUTUMN_JOBS__BACKEND: postgres\n      \
AUTUMN_DATABASE__PRIMARY_URL: postgres://autumn:autumn@db:5432/{{project_name}}_prod\n      \
AUTUMN_SECURITY__SIGNING_SECRET: \"${AUTUMN_SECURITY__SIGNING_SECRET:?set it first}\"\n    \
depends_on:\n      db:\n        condition: service_healthy\n      migrate:\n        \
condition: service_completed_successfully\n    restart: unless-stopped\n";

/// The web-tier role env spliced into the `app` service when `--split-workers`
/// is set: pin the app to the HTTP-only `web` role and the shared `postgres`
/// jobs backend so enqueues land in the queue the worker process drains.
const WEB_ROLE_ENV: &str = "\n      AUTUMN_ROLE: web\n      AUTUMN_JOBS__BACKEND: postgres";

fn render(template: &str, project_name: &str, embed: bool, split_workers: bool) -> String {
    let (build_step, static_copy) = if embed {
        (
            "# Single-binary build: fingerprint static assets, then compile with the\n\
             # embed-assets feature so the binary serves static/ (incl. the fingerprint\n\
             # manifest) and i18n locales from itself — no sidecar directories. The app\n\
             # opts in via `.embedded_static()` / `.embedded_locales()` (see src/main.rs).\n\
             RUN autumn build --embed",
            // Assets/locales are embedded; only migrations/ is staged below.
            "# Assets and locales are embedded in the binary (`autumn build --embed`);\n\
             # only migrations/ is staged, for the one-shot `autumn migrate` job.\n",
        )
    } else {
        (
            "RUN cargo build --release",
            "COPY --chown=autumn:autumn --from=builder /app/static /app/static\n",
        )
    };
    // Split-topology placeholders exist only in the docker-compose template, so
    // these replacements are no-ops for the other files. Substitute them before
    // `{{project_name}}` so the worker block's own `{{project_name}}` tokens are
    // resolved by the shared replacement below.
    let (worker_service, web_role_env) = if split_workers {
        (WORKER_SERVICE_BLOCK, WEB_ROLE_ENV)
    } else {
        ("", "")
    };
    template
        .replace("{{worker_service}}", worker_service)
        .replace("{{app_role_env}}", web_role_env)
        .replace("{{project_name}}", project_name)
        .replace(
            "{{rust_version}}",
            option_env!("CARGO_PKG_RUST_VERSION").unwrap_or("1.88.0"),
        )
        .replace("{{autumn_cli_version}}", env!("CARGO_PKG_VERSION"))
        .replace("{{diesel_cli_version}}", "2.3.8")
        .replace("{{build_step}}", build_step)
        .replace("{{static_copy}}", static_copy)
}

fn planned_files(target: Target) -> Vec<(&'static str, &'static str)> {
    let mut files: Vec<(&'static str, &'static str)> = vec![
        ("Dockerfile", templates::DOCKERFILE),
        (".dockerignore", templates::DOCKERIGNORE),
        (
            "autumn.production.toml.example",
            templates::PRODUCTION_CONFIG,
        ),
    ];
    match target {
        Target::Fly => files.push(("fly.toml", templates::FLY_TOML)),
        Target::DockerCompose => files.push(("docker-compose.yml", templates::DOCKER_COMPOSE)),
        Target::AzureContainerApps => {
            files.push(("main.tf", templates::AZURE_MAIN_TF));
            files.push(("variables.tf", templates::AZURE_VARIABLES_TF));
            files.push(("outputs.tf", templates::AZURE_OUTPUTS_TF));
            files.push(("terraform.tfvars.example", templates::AZURE_TFVARS_EXAMPLE));
            files.push((
                ".github/workflows/azure-deploy.yml",
                templates::AZURE_DEPLOY_WORKFLOW,
            ));
        }
        Target::AwsAppRunner => {
            files.push(("main.tf", templates::AWS_APP_RUNNER_MAIN_TF));
            files.push(("variables.tf", templates::AWS_APP_RUNNER_VARIABLES_TF));
            files.push(("outputs.tf", templates::AWS_APP_RUNNER_OUTPUTS_TF));
            files.push((
                "terraform.tfvars.example",
                templates::AWS_APP_RUNNER_TFVARS_EXAMPLE,
            ));
        }
        Target::AwsEcs => {
            files.push(("main.tf", templates::AWS_ECS_MAIN_TF));
            files.push(("variables.tf", templates::AWS_ECS_VARIABLES_TF));
            files.push(("outputs.tf", templates::AWS_ECS_OUTPUTS_TF));
            files.push((
                "terraform.tfvars.example",
                templates::AWS_ECS_TFVARS_EXAMPLE,
            ));
            files.push((
                ".github/workflows/aws-deploy.yml",
                templates::AWS_DEPLOY_WORKFLOW,
            ));
        }
        Target::GcpCloudRun => {
            files.push(("main.tf", templates::GCP_CLOUD_RUN_MAIN_TF));
            files.push(("variables.tf", templates::GCP_CLOUD_RUN_VARIABLES_TF));
            files.push(("outputs.tf", templates::GCP_CLOUD_RUN_OUTPUTS_TF));
            files.push((
                "terraform.tfvars.example",
                templates::GCP_CLOUD_RUN_TFVARS_EXAMPLE,
            ));
            files.push((
                ".github/workflows/gcp-deploy.yml",
                templates::GCP_DEPLOY_WORKFLOW,
            ));
        }
        Target::Default => {}
    }
    files
}

// ── tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn make_project(tmp: &TempDir, name: &str) -> std::path::PathBuf {
        let dir = tmp.path().to_path_buf();
        fs::write(
            dir.join("Cargo.toml"),
            format!("[package]\nname = \"{name}\"\nversion = \"0.1.0\"\n"),
        )
        .unwrap();
        dir
    }

    // ── default target ────────────────────────────────────────────────────────

    #[test]
    fn init_creates_dockerfile() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        assert!(dir.join("Dockerfile").is_file(), "Dockerfile not created");
    }

    #[test]
    fn init_creates_dockerignore() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        assert!(
            dir.join(".dockerignore").is_file(),
            ".dockerignore not created"
        );
    }

    #[test]
    fn init_creates_production_config_example() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        assert!(
            dir.join("autumn.production.toml.example").is_file(),
            "autumn.production.toml.example not created"
        );
    }

    #[test]
    fn init_returns_list_of_created_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        let files = init(&dir, "my-app", false, Target::Default, false).unwrap();
        assert!(files.contains(&"Dockerfile".to_string()));
        assert!(files.contains(&".dockerignore".to_string()));
        assert!(files.contains(&"autumn.production.toml.example".to_string()));
    }

    // ── Dockerfile content ────────────────────────────────────────────────────

    #[test]
    fn dockerfile_has_cargo_chef_stages() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("cargo-chef"),
            "Dockerfile must use cargo-chef for dependency caching"
        );
        assert!(
            content.contains("cargo chef prepare"),
            "Dockerfile must run 'cargo chef prepare'"
        );
        assert!(
            content.contains("cargo chef cook"),
            "Dockerfile must run 'cargo chef cook'"
        );
    }

    #[test]
    fn dockerfile_uses_declared_msrv_for_rust_build_image() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        let msrv = option_env!("CARGO_PKG_RUST_VERSION").unwrap_or("1.88.0");

        assert!(
            content.contains(&format!("FROM rust:{msrv}-bookworm AS chef")),
            "Dockerfile build stage must use the declared MSRV {msrv}: {content}"
        );
        assert!(
            !content.contains("rust-1.86") && !content.contains("rust:1.86"),
            "Dockerfile must not pin an older Rust image than the declared MSRV: {content}"
        );
    }

    #[test]
    fn dockerfile_passes_through_git_provenance_build_args() {
        // Issue #1676: the build context excludes `/.git`, so the release
        // Dockerfile must surface the `AUTUMN_BUILD_*` provenance as build-arg
        // ENV before the build step. Otherwise the generated build.rs finds no
        // git repo and the container reports `git.*` as `null` on
        // `/actuator/info`, defeating deploy/rollback verification.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();

        for arg in [
            "AUTUMN_BUILD_GIT_SHA",
            "AUTUMN_BUILD_GIT_SHA_SHORT",
            "AUTUMN_BUILD_GIT_BRANCH",
            "AUTUMN_BUILD_GIT_DIRTY",
            "AUTUMN_BUILD_TIMESTAMP",
        ] {
            assert!(
                content.contains(&format!("ARG {arg}")),
                "Dockerfile must declare `ARG {arg}` for git provenance passthrough: {content}"
            );
            assert!(
                content.contains(&format!("{arg}=${{{arg}}}")),
                "Dockerfile must surface `{arg}` as ENV so build.rs bakes it: {content}"
            );
        }

        // The provenance ENV must precede the build step so the compile sees it.
        let env_pos = content.find("AUTUMN_BUILD_GIT_SHA=${AUTUMN_BUILD_GIT_SHA}");
        let build_pos = content.find("{{build_step}}").or_else(|| {
            // `{{build_step}}` is already substituted; both variants run cargo.
            content
                .find("cargo build --release")
                .or_else(|| content.find("autumn build --embed"))
        });
        assert!(
            matches!((env_pos, build_pos), (Some(e), Some(b)) if e < b),
            "provenance ENV must appear before the build step: {content}"
        );
    }

    #[test]
    fn dockerfile_has_three_stages() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        let from_count = content.lines().filter(|l| l.starts_with("FROM ")).count();
        assert!(
            from_count >= 3,
            "Dockerfile must have at least 3 FROM stages (chef, planner, builder, runtime); got {from_count}"
        );
    }

    #[test]
    fn dockerfile_copies_production_config_as_runtime_autumn_toml() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("autumn.production.toml.example"),
            "Dockerfile must COPY autumn.production.toml.example into the runtime image so \
             the container binds to 0.0.0.0 (not the dev 127.0.0.1) without manual edits"
        );
    }

    #[test]
    fn dockerfile_runtime_uses_debian_slim() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("debian:bookworm-slim"),
            "runtime stage must use debian:bookworm-slim"
        );
    }

    #[test]
    fn dockerfile_runtime_installs_libpq_and_tini() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("libpq"),
            "runtime must install libpq for Diesel"
        );
        assert!(
            content.contains("tini"),
            "runtime must install tini as init process"
        );
        assert!(
            content.contains("ca-certificates"),
            "runtime must install ca-certificates"
        );
    }

    #[test]
    fn dockerfile_uses_disk_build_without_embed_feature() {
        // A project that doesn't define the `embed-assets` feature (e.g. one
        // scaffolded before embedding existed) must get the disk-based build so
        // its Docker build keeps working.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("RUN cargo build --release"),
            "non-embed project must use the disk-based build: {content}"
        );
        assert!(
            !content.contains("autumn build --embed"),
            "non-embed project must not require the embed-assets feature"
        );
        assert!(
            content.contains("/app/static"),
            "non-embed build must COPY the static/ sidecar into the runtime image"
        );
    }

    #[test]
    fn dockerfile_embeds_when_project_has_embed_feature() {
        // A project that opts into the `embed-assets` feature gets a
        // single-binary build with no static/ sidecar.
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path().to_path_buf();
        fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"my-app\"\nversion = \"0.1.0\"\n\n\
             [features]\nembed-assets = [\"autumn-web/embed-assets\"]\n",
        )
        .unwrap();
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("autumn build --embed"),
            "embed-feature project must build the embedded single binary: {content}"
        );
        assert!(
            !content.contains("/app/static"),
            "embedded build must not COPY a static/ sidecar into the runtime image"
        );
    }

    #[test]
    fn dockerfile_copies_migrations() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("migrations"),
            "Dockerfile must COPY migrations into runtime image"
        );
    }

    #[test]
    fn dockerfile_defers_migrations_to_one_shot_primary_job() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            !content.contains("auto_migrate_in_production = true"),
            "Dockerfile must not enable startup migrations for every web replica"
        );
        assert!(
            content.contains("autumn migrate"),
            "Dockerfile must document the explicit primary-role migration job"
        );
    }

    #[test]
    fn dockerfile_installs_autumn_cli_for_migration_jobs() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("cargo install")
                && content.contains("autumn-cli")
                && content.contains("/usr/local/bin/autumn"),
            "Dockerfile must include the autumn CLI used by one-shot migration jobs"
        );
    }

    #[test]
    fn dockerfile_installs_diesel_cli_for_migration_jobs() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("cargo install")
                && content.contains("diesel_cli")
                && content.contains("libpq-dev")
                && content.contains("--features postgres")
                && content.contains("/usr/local/bin/diesel"),
            "Dockerfile must include the diesel CLI used by autumn migrate"
        );
    }

    #[test]
    fn dockerfile_has_healthcheck() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("HEALTHCHECK"),
            "Dockerfile must have a HEALTHCHECK directive"
        );
        assert!(
            content.contains("/health"),
            "HEALTHCHECK must probe the /health actuator endpoint"
        );
    }

    #[test]
    fn dockerfile_exposes_port_3000() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("EXPOSE 3000"),
            "Dockerfile must EXPOSE 3000"
        );
    }

    #[test]
    fn dockerfile_substitutes_project_name() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-blog");
        init(&dir, "my-blog", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert!(
            content.contains("my-blog"),
            "Dockerfile must contain the substituted project name"
        );
        assert!(
            !content.contains("{{project_name}}"),
            "Dockerfile must not contain unsubstituted {{{{project_name}}}}"
        );
        assert!(
            !content.contains("{{"),
            "Dockerfile must not contain any unsubstituted template placeholders"
        );
    }

    // ── .dockerignore content ─────────────────────────────────────────────────

    #[test]
    fn dockerignore_excludes_target() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        assert!(
            content.contains("target"),
            ".dockerignore must exclude target/"
        );
    }

    #[test]
    fn dockerignore_excludes_git() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        assert!(content.contains(".git"), ".dockerignore must exclude .git");
    }

    #[test]
    fn dockerignore_excludes_node_modules() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        assert!(
            content.contains("node_modules"),
            ".dockerignore must exclude node_modules"
        );
    }

    #[test]
    fn dockerignore_excludes_dist() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        assert!(content.contains("dist"), ".dockerignore must exclude dist/");
    }

    #[test]
    fn dockerignore_excludes_master_key() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        assert!(
            content.contains("/config/master.key") || content.contains("config/master.key"),
            ".dockerignore must exclude config/master.key"
        );
    }

    #[test]
    fn dockerignore_excludes_terraform_state() {
        // The azure-container-apps target scaffolds main.tf/terraform.tfvars
        // directly alongside the Dockerfile in the same directory. Docker's
        // build context is whatever the positional path argument points at
        // (`docker build .`), so running that from this directory AFTER
        // `terraform apply` would otherwise upload the plaintext
        // terraform.tfstate — every secret value, `sensitive` flag or not —
        // into the builder/build cache even though no stage ever COPYs it
        // into the final image.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        for pattern in [".terraform/", "*.tfstate", "terraform.tfvars"] {
            assert!(
                content.contains(pattern),
                ".dockerignore must exclude {pattern:?} so terraform.tfstate is never sent \
                 to the Docker build context: {content}"
            );
        }
    }

    // ── signing-secret smoke gate ─────────────────────────────────────────────

    #[test]
    fn production_config_template_documents_signing_secret_env_var() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("autumn.production.toml.example")).unwrap();
        assert!(
            content.contains("AUTUMN_SECURITY__SIGNING_SECRET"),
            "production config template must document the signing-secret env var"
        );
    }

    #[test]
    fn production_config_template_documents_openssl_rand_command() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("autumn.production.toml.example")).unwrap();
        assert!(
            content.contains("openssl rand -hex 32"),
            "production config template must show the secret generation command"
        );
    }

    #[test]
    fn production_config_template_mentions_signing_secrets_guide() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("autumn.production.toml.example")).unwrap();
        assert!(
            content.contains("signing-secrets.md"),
            "production config template must link to the signing-secrets guide"
        );
    }

    #[test]
    fn smoke_gate_passes_for_valid_config() {
        let content = r#"
# This is a comment with secret = "ignored"
[server]
port = 3000
"#;
        assert!(check_production_config_signing_secret(content).is_ok());
    }

    #[test]
    fn smoke_gate_fails_when_secret_literal_committed() {
        let content = r#"
[security.signing_secret]
secret = "my-actual-secret-value-here"
"#;
        let err = check_production_config_signing_secret(content).unwrap_err();
        assert!(err.contains("committed signing secret literal"));
    }

    #[test]
    fn smoke_gate_ignores_commented_secret_lines() {
        // Comments are allowed to mention the key name for documentation.
        let content = r#"
# secret = "example-value-for-docs"
# Set AUTUMN_SECURITY__SIGNING_SECRET instead
"#;
        assert!(check_production_config_signing_secret(content).is_ok());
    }

    #[test]
    fn smoke_gate_passes_for_empty_previous_secrets() {
        let content = "
[security.signing_secret]
previous_secrets = []
";
        assert!(check_production_config_signing_secret(content).is_ok());
    }

    // ── production config content ─────────────────────────────────────────────

    #[test]
    fn production_config_has_placeholder_db_url_not_real_credentials() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("autumn.production.toml.example")).unwrap();
        // Must have a DB URL entry
        assert!(
            content.contains("DATABASE_URL") || content.contains("url"),
            "production config must document the database URL setting"
        );
        // Must not contain real credentials (no 'password' in a non-commented line)
        for line in content.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with('#') {
                continue;
            }
            assert!(
                !trimmed.to_lowercase().contains("secret"),
                "production config must not contain real secrets"
            );
        }
    }

    #[test]
    fn production_config_has_placeholder_for_project_name() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-blog");
        init(&dir, "my-blog", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("autumn.production.toml.example")).unwrap();
        assert!(
            content.contains("my-blog"),
            "production config must substitute project name"
        );
        assert!(
            !content.contains("{{project_name}}"),
            "production config must not contain unsubstituted placeholders"
        );
    }

    #[test]
    fn production_config_documents_port() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("autumn.production.toml.example")).unwrap();
        assert!(
            content.contains("port"),
            "production config must document the port setting"
        );
    }

    // ── --force flag ──────────────────────────────────────────────────────────

    #[test]
    fn init_without_force_errors_if_dockerfile_exists() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join("Dockerfile"), "existing content").unwrap();
        let err = init(&dir, "my-app", false, Target::Default, false).unwrap_err();
        assert!(
            matches!(err, ReleaseError::FileExists(_)),
            "expected FileExists, got: {err}"
        );
        assert!(
            err.to_string().contains("Dockerfile"),
            "error message must name the conflicting file"
        );
    }

    #[test]
    fn init_without_force_errors_if_any_file_exists() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join(".dockerignore"), "existing").unwrap();
        let err = init(&dir, "my-app", false, Target::Default, false).unwrap_err();
        assert!(matches!(err, ReleaseError::FileExists(_)));
    }

    #[test]
    fn init_with_force_overwrites_existing_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join("Dockerfile"), "old content").unwrap();
        init(&dir, "my-app", true, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("Dockerfile")).unwrap();
        assert_ne!(
            content, "old content",
            "Dockerfile must be overwritten with --force"
        );
    }

    // ── --target=fly ──────────────────────────────────────────────────────────

    #[test]
    fn fly_target_creates_fly_toml() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Fly, false).unwrap();
        assert!(
            dir.join("fly.toml").is_file(),
            "fly.toml must be created for --target=fly"
        );
    }

    #[test]
    fn fly_toml_references_dockerfile() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Fly, false).unwrap();
        let content = fs::read_to_string(dir.join("fly.toml")).unwrap();
        assert!(
            content.contains("Dockerfile"),
            "fly.toml must reference the Dockerfile"
        );
    }

    #[test]
    fn fly_toml_has_app_name() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-blog");
        init(&dir, "my-blog", false, Target::Fly, false).unwrap();
        let content = fs::read_to_string(dir.join("fly.toml")).unwrap();
        assert!(
            content.contains("my-blog"),
            "fly.toml must contain the app name"
        );
        assert!(
            !content.contains("{{project_name}}"),
            "fly.toml must not contain unsubstituted placeholders"
        );
    }

    #[test]
    fn fly_toml_has_liveness_probe() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Fly, false).unwrap();
        let content = fs::read_to_string(dir.join("fly.toml")).unwrap();
        assert!(
            content.contains("path") && content.contains("/live"),
            "fly.toml must wire /live as the liveness health check"
        );
    }

    #[test]
    fn fly_toml_has_readiness_probe() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Fly, false).unwrap();
        let content = fs::read_to_string(dir.join("fly.toml")).unwrap();
        assert!(
            content.contains("path") && content.contains("/ready"),
            "fly.toml must wire /ready as the readiness check so Fly stops routing \
             traffic before the listener closes during graceful shutdown"
        );
    }

    #[test]
    fn fly_toml_has_kill_timeout() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Fly, false).unwrap();
        let content = fs::read_to_string(dir.join("fly.toml")).unwrap();
        assert!(
            content.contains("kill_timeout"),
            "fly.toml must set kill_timeout so Fly waits at least \
             prestop_grace_secs + shutdown_timeout_secs before sending SIGKILL"
        );
        assert!(
            content.contains("kill_signal"),
            "fly.toml must set kill_signal = \"SIGTERM\" so Autumn receives the expected signal"
        );
    }

    #[test]
    fn fly_toml_has_metrics_endpoint() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Fly, false).unwrap();
        let content = fs::read_to_string(dir.join("fly.toml")).unwrap();
        assert!(
            content.contains("[metrics]"),
            "fly.toml must include a [metrics] section for Fly's Prometheus scraper"
        );
        assert!(
            content.contains("/actuator/prometheus"),
            "fly.toml [metrics] must point to /actuator/prometheus (Prometheus text format), \
             not /actuator/metrics (JSON)"
        );
    }

    #[test]
    fn fly_toml_documents_deploy_release_command() {
        // release_command is commented out by default: autumn migrate exits non-zero
        // when no database URL is configured, which would break non-DB app deploys.
        // The template documents the opt-in pattern so DB users can uncomment it.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Fly, false).unwrap();
        let content = fs::read_to_string(dir.join("fly.toml")).unwrap();
        assert!(
            content.contains("autumn migrate"),
            "fly.toml must document the autumn migrate release command so DB users \
             can uncomment it"
        );
        assert!(
            content.contains("release_command"),
            "fly.toml must document release_command so DB users know where to uncomment"
        );
    }

    #[test]
    fn default_target_does_not_create_fly_toml() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        assert!(
            !dir.join("fly.toml").exists(),
            "fly.toml must NOT be created for the default target"
        );
    }

    // ── --target=docker-compose ───────────────────────────────────────────────

    #[test]
    fn docker_compose_target_creates_docker_compose_yml() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::DockerCompose, false).unwrap();
        assert!(
            dir.join("docker-compose.yml").is_file(),
            "docker-compose.yml must be created for --target=docker-compose"
        );
    }

    #[test]
    fn docker_compose_has_app_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::DockerCompose, false).unwrap();
        let content = fs::read_to_string(dir.join("docker-compose.yml")).unwrap();
        assert!(
            content.contains("app:"),
            "docker-compose.yml must have an 'app' service"
        );
    }

    #[test]
    fn docker_compose_has_postgres_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::DockerCompose, false).unwrap();
        let content = fs::read_to_string(dir.join("docker-compose.yml")).unwrap();
        assert!(
            content.contains("postgres") || content.contains("db:"),
            "docker-compose.yml must have a Postgres service"
        );
    }

    #[test]
    fn docker_compose_app_depends_on_db() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::DockerCompose, false).unwrap();
        let content = fs::read_to_string(dir.join("docker-compose.yml")).unwrap();
        assert!(
            content.contains("depends_on"),
            "docker-compose.yml app service must depend_on the db"
        );
    }

    #[test]
    fn docker_compose_app_requires_signing_secret_env() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::DockerCompose, false).unwrap();
        let content = fs::read_to_string(dir.join("docker-compose.yml")).unwrap();

        assert!(
            content.contains(
                r#"AUTUMN_SECURITY__SIGNING_SECRET: "${AUTUMN_SECURITY__SIGNING_SECRET:?set it first}""#
            ),
            "app service must pass the required production signing secret: {content}"
        );
    }

    #[test]
    fn docker_compose_runs_one_shot_migration_before_app() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::DockerCompose, false).unwrap();
        let content = fs::read_to_string(dir.join("docker-compose.yml")).unwrap();

        assert!(
            content.contains("migrate:"),
            "docker-compose.yml must include a one-shot migration service"
        );
        assert!(
            content.contains("autumn migrate"),
            "migration service must run autumn migrate"
        );
        assert!(
            content.contains("condition: service_completed_successfully"),
            "app service must wait for the migration job to complete successfully"
        );
    }

    #[test]
    fn docker_compose_substitutes_project_name() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-blog");
        init(&dir, "my-blog", false, Target::DockerCompose, false).unwrap();
        let content = fs::read_to_string(dir.join("docker-compose.yml")).unwrap();
        assert!(
            content.contains("my-blog"),
            "docker-compose.yml must substitute project name"
        );
        assert!(
            !content.contains("{{project_name}}"),
            "docker-compose.yml must not contain unsubstituted placeholders"
        );
    }

    #[test]
    fn default_target_does_not_create_docker_compose() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        assert!(
            !dir.join("docker-compose.yml").exists(),
            "docker-compose.yml must NOT be created for the default target"
        );
    }

    // ── --target=azure-container-apps ─────────────────────────────────────────

    #[test]
    fn azure_target_creates_all_expected_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        for name in [
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
            ".github/workflows/azure-deploy.yml",
        ] {
            assert!(
                dir.join(name).is_file(),
                "{name} must be created for --target=azure-container-apps"
            );
        }
        // Base scaffolding is still emitted alongside the Azure-specific files.
        assert!(dir.join("Dockerfile").is_file());
        assert!(dir.join(".dockerignore").is_file());
        assert!(dir.join("autumn.production.toml.example").is_file());
    }

    #[test]
    fn azure_target_returns_nested_workflow_path_in_created_list() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        let files = init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        assert!(
            files
                .iter()
                .any(|f| f == ".github/workflows/azure-deploy.yml"),
            "created-files list must include the nested workflow path: {files:?}"
        );
    }

    #[test]
    fn default_target_does_not_create_azure_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        for name in [
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
            ".github/workflows/azure-deploy.yml",
        ] {
            assert!(
                !dir.join(name).exists(),
                "{name} must NOT be created for the default target"
            );
        }
    }

    #[test]
    fn main_tf_has_resource_group_and_registry() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("azurerm_resource_group"),
            "main.tf must provision a resource group: {content}"
        );
        assert!(
            content.contains("azurerm_container_registry"),
            "main.tf must provision an Azure Container Registry: {content}"
        );
    }

    #[test]
    fn main_tf_has_container_app_environment_and_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("azurerm_container_app_environment"),
            "main.tf must provision a Container Apps environment: {content}"
        );
        assert!(
            content.contains("resource \"azurerm_container_app\""),
            "main.tf must provision the Container App service: {content}"
        );
    }

    #[test]
    fn main_tf_has_postgres_flexible_server() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("azurerm_postgresql_flexible_server"),
            "main.tf must provision Azure Database for PostgreSQL Flexible Server: {content}"
        );
    }

    #[test]
    fn main_tf_postgres_server_does_not_hardcode_availability_zone() {
        // Not every Container Apps region offers Postgres Flexible Server
        // availability zone 1 — a hardcoded `zone = "1"` fails
        // `terraform apply` in those regions even though an unzoned server
        // would succeed. Omitting `zone` lets Azure pick a placement itself.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let server_block = content
            .split("resource \"azurerm_postgresql_flexible_server\" \"this\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the postgresql_flexible_server resource");
        // Check for an actual `zone = ...` attribute assignment, not just
        // the substring "zone" — the resource's own explanatory comment
        // about why zone is omitted legitimately mentions the word.
        assert!(
            !server_block.lines().any(|l| {
                let t = l.trim_start();
                !t.starts_with('#') && t.starts_with("zone ")
            }),
            "the Postgres Flexible Server must not pin an availability zone: {server_block}"
        );
    }

    #[test]
    fn main_tf_postgres_database_name_is_length_bounded() {
        // Postgres database identifiers are capped at 63 bytes; a 64
        // -character Cargo package name (valid) would otherwise overflow
        // it since the name was previously passed through unbounded.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let db_block = content
            .split("resource \"azurerm_postgresql_flexible_server_database\" \"this\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the postgresql_flexible_server_database resource");
        let name_line = db_block
            .lines()
            .find(|l| l.trim_start().starts_with("name"))
            .expect("the database resource must set a name");
        assert!(
            name_line.contains("local.postgres_database_name"),
            "the database resource must use the bounded/reserved-name-guarded local: {name_line}"
        );
        let raw_local_line = content
            .lines()
            .find(|l| l.trim_start().starts_with("postgres_database_name_raw"))
            .expect("main.tf must declare a postgres_database_name_raw local");
        assert!(
            raw_local_line.contains("substr(") && raw_local_line.contains(", 63)"),
            "the Postgres database name must be truncated to 63 characters: {raw_local_line}"
        );
    }

    #[test]
    fn main_tf_postgres_database_name_avoids_reserved_names() {
        // A fresh Flexible Server already owns "postgres", "azure_maintenance",
        // and "azure_sys" as Azure-specific system databases, plus
        // "template0"/"template1" — every Postgres cluster, on any host, is
        // initialized with those two as its own templates. A Cargo package
        // literally named one of those (or "azure-sys"/"template0", which
        // sanitize to the same underscored form) must not collide with them,
        // or `terraform apply` fails trying to create/manage a database that
        // already exists.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        for reserved in [
            "postgres",
            "azure_maintenance",
            "azure_sys",
            "template0",
            "template1",
        ] {
            assert!(
                content.contains(&format!("\"{reserved}\"")),
                "the reserved-name guard must list {reserved:?}: {content}"
            );
        }
        let database_name_local = content
            .lines()
            .skip_while(|l| !l.trim_start().starts_with("postgres_database_name ="))
            .take_while(|l| !l.trim_start().starts_with('}'))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(
            database_name_local.contains("contains(") && database_name_local.contains("_prod"),
            "postgres_database_name must fall back to a suffixed name when the \
             sanitized value collides with a reserved database name: {content}"
        );
    }

    #[test]
    fn main_tf_has_key_vault_with_database_and_signing_secrets() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("azurerm_key_vault"),
            "main.tf must provision a Key Vault secrets store: {content}"
        );
        assert!(
            content.contains("AUTUMN_DATABASE__PRIMARY_URL"),
            "main.tf must wire the primary DB URL env var from Key Vault: {content}"
        );
        assert!(
            content.contains("AUTUMN_SECURITY__SIGNING_SECRET"),
            "main.tf must wire the signing secret env var from Key Vault: {content}"
        );
    }

    #[test]
    fn main_tf_wires_trusted_hosts_so_prod_actually_binds() {
        // AUTUMN_PROFILE=prod makes fail_fast_on_invalid_trusted_hosts exit
        // the process immediately when security.trusted_hosts.hosts is
        // empty (see docs/guide/deployment.md's "Trusted hosts" section).
        // Without this, the container never binds after the first real
        // deploy — it would crash-loop instead of serving traffic.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("AUTUMN_SECURITY__TRUSTED_HOSTS__HOSTS"),
            "main.tf must set AUTUMN_SECURITY__TRUSTED_HOSTS__HOSTS on the Container \
             App: {content}"
        );
        assert!(
            content.contains("azurerm_container_app_environment.this.default_domain"),
            "the trusted host must be derived from the environment's default_domain \
             (known before the app is created), not the app's own \
             latest_revision_fqdn (which would be a circular self-reference): {content}"
        );
    }

    #[test]
    fn main_tf_grants_key_vault_access_to_terraform_identity() {
        // Access-policy-model Key Vaults grant NO data-plane access by
        // default (subscription-level Owner/Contributor does not imply Key
        // Vault secret access), so without a policy for Terraform's own
        // caller identity, `terraform apply` fails at the
        // azurerm_key_vault_secret resources with a 403.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("data.azurerm_client_config.current.object_id"),
            "main.tf must grant Key Vault access to Terraform's own caller \
             identity (data.azurerm_client_config.current.object_id), not \
             just the container app's identity: {content}"
        );
    }

    #[test]
    fn main_tf_acr_pull_role_assignment_skips_aad_check() {
        // The AcrPull grant targets the identity the SAME apply just
        // created above it — Entra ID replication lag can make the role
        // assignment fail with PrincipalNotFound before that object has
        // propagated, even though the identity itself was created
        // successfully. skip_service_principal_aad_check exists for
        // exactly this "newly provisioned principal" case; a user-assigned
        // identity's principal_id IS backed by a Service Principal object
        // in Entra ID, so it applies here.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let role_assignment = content
            .split("resource \"azurerm_role_assignment\" \"acr_pull\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the acr_pull role assignment");
        assert!(
            role_assignment.contains("skip_service_principal_aad_check = true"),
            "the AcrPull role assignment on the freshly-created identity must set \
             skip_service_principal_aad_check to avoid an intermittent \
             PrincipalNotFound failure from AAD replication lag: {role_assignment}"
        );
    }

    #[test]
    fn main_tf_key_vault_name_never_exceeds_azure_length_limit() {
        // Azure Key Vault names are capped at 24 characters. Extract the
        // substr() bound and random_id byte_length from the template and
        // verify the worst-case rendered name (prefix + "kv" + hex suffix)
        // fits, so a future edit to either constant can't silently regress
        // past the limit.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();

        let substr_bound: usize = content
            .split("substr(local.app_name_alnum, 0, ")
            .nth(1)
            .and_then(|rest| rest.split(')').next())
            .and_then(|n| n.trim().parse().ok())
            .expect("main.tf must call substr(local.app_name_alnum, 0, N) for the vault name");
        let byte_length: usize = content
            .split("byte_length = ")
            .nth(1)
            .and_then(|rest| rest.lines().next())
            .and_then(|n| n.trim().parse().ok())
            .expect("main.tf must declare random_id.suffix's byte_length");
        let hex_len = byte_length * 2;
        let worst_case_len = substr_bound + "kv".len() + hex_len;
        assert!(
            worst_case_len <= 24,
            "worst-case Key Vault name is {worst_case_len} chars (substr={substr_bound} + \
             \"kv\" + {hex_len} hex chars), exceeding Azure's 24-char limit: {content}"
        );
    }

    #[test]
    fn main_tf_sanitizes_names_consistently_via_shared_local() {
        // Postgres Flexible Server and Redis Cache names must use the same
        // sanitized local as ACR/Key Vault, not raw var.app_name — a Cargo
        // package name may contain underscores/uppercase that are invalid
        // in those resource names.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();

        let postgres_block = content
            .split("resource \"azurerm_postgresql_flexible_server\" \"this\"")
            .nth(1)
            .unwrap();
        let postgres_name_line = postgres_block
            .lines()
            .find(|l| l.trim_start().starts_with("name"))
            .unwrap();
        assert!(
            postgres_name_line.contains("local.app_name_alnum"),
            "Postgres server name must use the sanitized local: {postgres_name_line}"
        );

        let redis_block = content
            .split("resource \"azurerm_redis_cache\" \"this\"")
            .nth(1)
            .unwrap();
        let redis_name_line = redis_block
            .lines()
            .find(|l| l.trim_start().starts_with("name"))
            .unwrap();
        assert!(
            redis_name_line.contains("local.app_name_alnum"),
            "Redis cache name must use the sanitized local: {redis_name_line}"
        );
    }

    #[test]
    fn main_tf_derives_database_url_from_created_postgres_server() {
        // A single `terraform apply` must be enough: the connection string
        // is computed from the Postgres server this same apply creates
        // (its FQDN + the admin password variable), never taken as a
        // separate pre-computed `var.database_url` input.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("azurerm_postgresql_flexible_server.this.fqdn"),
            "main.tf must derive the database URL from the Postgres server's own FQDN: {content}"
        );
        assert!(
            !content.contains("var.database_url"),
            "main.tf must not reference a database_url variable: {content}"
        );
    }

    #[test]
    fn main_tf_postgres_admin_login_is_alphanumeric() {
        // Azure Database for PostgreSQL Flexible Server rejects
        // administrator_login values containing anything but letters and
        // digits (no underscore, no hyphen) — `terraform apply` fails
        // while creating the server otherwise. Also assert the server
        // resource and the derived database_url secret share a single
        // local so they can't drift out of sync with each other.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();

        let login_local_line = content
            .lines()
            .find(|l| l.trim_start().starts_with("postgres_admin_login"))
            .expect("main.tf must declare a postgres_admin_login local");
        let login_value = login_local_line
            .split('=')
            .nth(1)
            .unwrap()
            .trim()
            .trim_matches('"');
        assert!(
            !login_value.is_empty() && login_value.chars().all(|c| c.is_ascii_alphanumeric()),
            "postgres_admin_login must be alphanumeric-only, got {login_value:?}: \
             {login_local_line}"
        );

        let admin_login_attr = content
            .lines()
            .find(|l| l.trim_start().starts_with("administrator_login"))
            .expect("main.tf must set administrator_login on the Postgres server");
        assert!(
            admin_login_attr.contains("local.postgres_admin_login"),
            "the Postgres server resource must set administrator_login from \
             local.postgres_admin_login: {admin_login_attr}"
        );
        assert!(
            content.contains("postgres://${local.postgres_admin_login}:"),
            "the database_url secret must reuse the same admin-login local as the \
             server resource, not a separately hardcoded username: {content}"
        );
    }

    #[test]
    fn main_tf_container_apps_family_resources_use_sanitized_name() {
        // Log Analytics, the Container Apps environment, the app, its
        // container, and the migration job all require lowercase
        // alphanumerics-and-hyphens — unlike ACR/Key Vault/Postgres/Redis,
        // they DO allow hyphens, so they share `local.app_name_safe` (not
        // `app_name_alnum`, which strips hyphens too) rather than raw
        // `var.app_name`, which may contain underscores/uppercase from a
        // Cargo package name.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();

        for resource in [
            "azurerm_log_analytics_workspace\" \"this",
            "azurerm_container_app_environment\" \"this",
            "azurerm_container_app\" \"this",
            "azurerm_container_app_job\" \"migrate",
        ] {
            let block = content
                .split(&format!("resource \"{resource}\""))
                .nth(1)
                .unwrap_or_else(|| {
                    panic!("main.tf must declare resource \"{resource}\": {content}")
                });
            let name_line = block
                .lines()
                .find(|l| l.trim_start().starts_with("name"))
                .unwrap_or_else(|| panic!("{resource} must set a name: {block}"));
            assert!(
                name_line.contains("local.app_name_safe"),
                "{resource} must use the sanitized local.app_name_safe, not raw \
                 var.app_name or a literal project name: {name_line}"
            );
        }

        assert!(
            !content.contains("\"{{project_name}}\""),
            "main.tf must not hardcode the raw (unsanitized) project name as a \
             resource identifier: {content}"
        );
    }

    #[test]
    fn main_tf_app_name_safe_collapses_and_trims_hyphens() {
        // Naively mapping every invalid character to "-" turns "my__app"
        // into "my--app" (consecutive hyphens, invalid) and "my-" into a
        // name with a trailing hyphen (also invalid — Container App names
        // must end in an alphanumeric character). The locals must collapse
        // hyphen runs and trim leading/trailing hyphens after substitution.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let locals_block = content
            .split("locals {")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare a locals block");
        assert!(
            locals_block.contains("\"/-+/\""),
            "app_name derivation must collapse runs of hyphens to one via a regex like \
             /-+/: {locals_block}"
        );
        assert!(
            locals_block.matches("trim(").count() >= 2,
            "app_name derivation must trim leading/trailing hyphens both after collapsing \
             and after any length truncation: {locals_block}"
        );
    }

    #[test]
    fn main_tf_app_name_safe_is_length_bounded() {
        // Azure Container Apps-family names must be 2-32 characters. A
        // 1-character Cargo package name (valid) would produce a
        // below-minimum app name; a >24-character one would push
        // "${app_name_safe}-migrate" (8-char suffix) past the 32-char cap.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let locals_block = content
            .split("locals {")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare a locals block");
        assert!(
            locals_block.contains("substr("),
            "app_name_safe must truncate to leave headroom for the longest suffix \
             (-migrate, 8 chars) appended to any Container Apps-family resource: \
             {locals_block}"
        );
        assert!(
            locals_block.contains("length(local.app_name_hyphenated) < 2"),
            "app_name_safe must pad a too-short base up to Azure's 2-character minimum: \
             {locals_block}"
        );
    }

    #[test]
    fn main_tf_sanitized_locals_fall_back_when_input_sanitizes_to_nothing_or_a_digit() {
        // A Cargo package name made entirely of characters sanitization
        // strips (e.g. the legal-but-unusual name "_") sanitizes to an
        // empty string, which would otherwise produce a Postgres server
        // name starting with "-" (the "${app_name_alnum}-pg-..." pattern),
        // an empty Postgres database name, and violate resource types that
        // require a letter-led name (Key Vault) rather than just
        // alphanumeric (ACR). Both base locals must fall back to a fixed
        // alphabetic prefix whenever sanitization leaves nothing, or
        // leaves a value not starting with a letter (a leading digit
        // survives sanitization but several consumers don't accept it).
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let locals_block = content
            .split("locals {")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare a locals block");

        assert!(
            locals_block.contains("app_name_alnum_raw == \"\" ? \"app\"")
                && locals_block.contains("app_name_hyphenated_raw == \"\" ? \"app\""),
            "both base locals must fall back to a non-empty alphabetic value when \
             sanitization leaves nothing: {locals_block}"
        );
        assert!(
            locals_block.matches("regex(\"^[a-z]\"").count() >= 2,
            "both base locals must check for (and fall back on) a non-letter-leading \
             sanitized value, not just an empty one: {locals_block}"
        );
    }

    #[test]
    fn main_tf_app_name_alnum_is_length_bounded() {
        // ACR names are capped at 50 characters; "${app_name_alnum}acr" +
        // an 8-hex-char suffix reserves 11, so an unbounded app_name_alnum
        // (Cargo package names may be much longer than 39 characters)
        // overflows it. Postgres (63) and Redis (63) are more permissive
        // but derive from the same local, so bounding it once covers all
        // three rather than truncating per-resource.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let locals_block = content
            .split("locals {")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare a locals block");
        let alnum_line = locals_block
            .lines()
            .find(|l| l.trim_start().starts_with("app_name_alnum ="))
            .expect("locals must declare app_name_alnum");
        assert!(
            alnum_line.contains("substr("),
            "app_name_alnum must be length-bounded so ACR's 50-character limit (after \
             the fixed \"acr\" + 8-hex-char suffix) can never be exceeded: {alnum_line}"
        );
    }

    #[test]
    fn main_tf_resource_group_and_identity_use_bounded_name() {
        // Resource groups (90-char limit) and the user-assigned identity
        // (128-char limit) are far more permissive than Container
        // Apps-family resources, but a Cargo package name is unbounded —
        // one longer than 87 characters overflows the resource group's own
        // limit once "-rg" is appended. Both must use the already
        // length-safe local.app_name_safe, not raw var.app_name.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();

        for resource in [
            "azurerm_resource_group\" \"this",
            "azurerm_user_assigned_identity\" \"this",
        ] {
            let block = content
                .split(&format!("resource \"{resource}\""))
                .nth(1)
                .unwrap_or_else(|| {
                    panic!("main.tf must declare resource \"{resource}\": {content}")
                });
            let name_line = block
                .lines()
                .find(|l| l.trim_start().starts_with("name"))
                .unwrap_or_else(|| panic!("{resource} must set a name: {block}"));
            assert!(
                name_line.contains("local.app_name_safe"),
                "{resource} must use the length-bounded local.app_name_safe, not raw \
                 var.app_name (unbounded — a long Cargo package name would overflow \
                 this resource's own name-length limit): {name_line}"
            );
        }
    }

    #[test]
    fn main_tf_uses_bootstrap_image_and_ignores_later_image_drift() {
        // Container Apps must pull an image to create the app's/job's first
        // revision, but a brand-new ACR has none yet, so Terraform points
        // both at a public placeholder and then ignores further image
        // changes so a later `terraform apply` doesn't revert a live
        // `az containerapp update`/job deploy back to the placeholder.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("image  = var.bootstrap_image")
                || content.contains("image = var.bootstrap_image"),
            "main.tf must set the container image to var.bootstrap_image: {content}"
        );
        assert_eq!(
            content
                .matches("ignore_changes = [template[0].container[0].image]")
                .count(),
            2,
            "both the app and the migration job must ignore image drift after bootstrap: {content}"
        );
        assert!(
            !content.contains("${var.image_tag}"),
            "main.tf must not build the Terraform-managed image from var.image_tag — \
             CI manages the real image out-of-band after bootstrap: {content}"
        );
    }

    #[test]
    fn main_tf_has_migration_job() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("azurerm_container_app_job\" \"migrate\""),
            "main.tf must provision a one-shot migration Container Apps Job: {content}"
        );
        assert!(
            content.contains("manual_trigger_config"),
            "the migration job must only run when CI explicitly starts it: {content}"
        );
        assert!(
            content.contains("autumn migrate"),
            "the migration job must run `autumn migrate`: {content}"
        );
        assert!(
            content.contains("AUTUMN_DATABASE__PRIMARY_URL"),
            "the migration job must be wired to the same database secret as the app: {content}"
        );
    }

    #[test]
    fn main_tf_wires_redis_url_into_container_app_when_enabled() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("azurerm_key_vault_secret\" \"redis_url\""),
            "main.tf must store the Redis connection string in Key Vault: {content}"
        );
        // Autumn's actual config path is `[cache.redis] url` (env:
        // AUTUMN_CACHE__REDIS__URL, double underscore before URL) — not
        // AUTUMN_CACHE__REDIS_URL, which Autumn never reads.
        assert!(
            content.contains("AUTUMN_CACHE__REDIS__URL"),
            "main.tf must wire AUTUMN_CACHE__REDIS__URL into the Container App \
             when enable_redis_cache is true: {content}"
        );
        assert!(
            !content.contains("AUTUMN_CACHE__REDIS_URL\""),
            "main.tf must not use the single-underscore variant, which Autumn ignores: {content}"
        );
        // Without selecting the backend, Autumn stays on its default
        // in-memory cache and never reads the URL at all.
        assert!(
            content.contains("name  = \"AUTUMN_CACHE__BACKEND\"")
                || content.contains("name = \"AUTUMN_CACHE__BACKEND\""),
            "main.tf must set AUTUMN_CACHE__BACKEND=redis so Autumn actually selects the \
             Redis cache backend: {content}"
        );
        assert!(
            content.contains("value = \"redis\""),
            "AUTUMN_CACHE__BACKEND must be set to \"redis\": {content}"
        );
    }

    #[test]
    fn main_tf_urlencodes_redis_access_key() {
        // Azure Redis access keys are base64-like and may contain "/" — raw
        // in a URL's userinfo segment, that would terminate the authority
        // before "@hostname" and produce a malformed URL depending on the
        // randomly issued key.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("urlencode(azurerm_redis_cache.this[0].primary_access_key)"),
            "the Redis access key must be urlencode()'d before being interpolated into \
             the rediss:// URL, the same way database_admin_password already is: {content}"
        );
    }

    #[test]
    fn main_tf_documents_redis_cache_requires_app_level_plugin() {
        // Provisioning the cache and wiring its env vars is infrastructure
        // only — Autumn's cache subsystem has no built-in Redis
        // implementation (unlike sessions/channels/jobs), so the app must
        // ALSO depend on autumn-cache-redis and register RedisCachePlugin,
        // or the env vars are silently never read.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let main_tf = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            main_tf.contains("RedisCachePlugin"),
            "main.tf must document that the app needs RedisCachePlugin registered, not \
             just the env vars set: {main_tf}"
        );
        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        assert!(
            variables_tf.to_lowercase().contains("infrastructure only"),
            "variables.tf's enable_redis_cache description must warn this is \
             infrastructure-only: {variables_tf}"
        );
    }

    #[test]
    fn main_tf_has_optional_redis_cache_gated_by_variable() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("azurerm_redis_cache"),
            "main.tf must optionally provision a Redis Cache: {content}"
        );
        assert!(
            content.contains("enable_redis_cache"),
            "the Redis Cache must be gated by an opt-in feature-flag variable: {content}"
        );
    }

    #[test]
    fn main_tf_never_contains_secret_literals() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        for line in content.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with('#') {
                continue;
            }
            let lower = trimmed.to_lowercase();
            assert!(
                !lower.contains("password = \"") && !lower.contains("password=\""),
                "main.tf must never assign a literal secret value (only var./data. \
                 references are allowed): {trimmed}"
            );
        }
    }

    #[test]
    fn main_tf_substitutes_project_name() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-blog");
        init(&dir, "my-blog", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("my-blog"),
            "main.tf must substitute the project name: {content}"
        );
        assert!(
            !content.contains("{{"),
            "main.tf must not contain unsubstituted template placeholders: {content}"
        );
    }

    #[test]
    fn variables_tf_declares_expected_inputs() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("variables.tf")).unwrap();
        for var in [
            "variable \"app_name\"",
            "variable \"location\"",
            "variable \"image_tag\"",
            "variable \"db_sku\"",
            "variable \"min_replicas\"",
            "variable \"max_replicas\"",
            "variable \"enable_redis_cache\"",
        ] {
            assert!(
                content.contains(var),
                "variables.tf must declare {var}: {content}"
            );
        }
        assert!(
            !content.contains("{{"),
            "variables.tf must not contain unsubstituted template placeholders: {content}"
        );
    }

    #[test]
    fn azure_container_apps_defaults_to_zero_replicas_for_bootstrap_safety() {
        // The initial Terraform-managed app revision uses a public placeholder image
        // solely because Container Apps requires an image before the user's ACR has
        // one. Keep it scaled to zero by default so that placeholder is not started
        // with production secret refs or the app's Key Vault-capable identity.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();

        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        let min_replicas_block = variables_tf
            .split("variable \"min_replicas\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("variables.tf must declare min_replicas");
        assert!(
            min_replicas_block.contains("default     = 0")
                || min_replicas_block.contains("default = 0"),
            "min_replicas must default to 0 so the public bootstrap image is not run \
             with production secrets before the first real deploy: {variables_tf}"
        );

        let tfvars = fs::read_to_string(dir.join("terraform.tfvars.example")).unwrap();
        assert!(
            tfvars.contains("min_replicas        = 0") || tfvars.contains("min_replicas = 0"),
            "terraform.tfvars.example must preserve the safe bootstrap default: {tfvars}"
        );
    }

    #[test]
    fn variables_tf_marks_secret_inputs_sensitive_with_no_default() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("variables.tf")).unwrap();
        assert!(
            content.contains("variable \"database_admin_password\""),
            "variables.tf must declare a database_admin_password secret variable: {content}"
        );
        assert!(
            content.contains("variable \"signing_secret\""),
            "variables.tf must declare a signing_secret secret variable: {content}"
        );
        assert!(
            content.contains("sensitive   = true") || content.contains("sensitive = true"),
            "secret variables must be marked sensitive so Terraform redacts them in \
             plan/apply output: {content}"
        );
        assert!(
            !content.to_lowercase().contains("default     = \"postgres")
                && !content.contains("default = \"CHANGE_ME\""),
            "secret variables must not ship a literal default value: {content}"
        );
    }

    #[test]
    fn variables_tf_does_not_declare_database_url() {
        // A single `terraform apply` must succeed without a second, targeted
        // apply to fill in a value that depends on a resource the same apply
        // is about to create. main.tf derives the connection string from the
        // Postgres server it creates instead of requiring this as an input —
        // regression guard against reintroducing that two-apply footgun.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("variables.tf")).unwrap();
        assert!(
            !content.contains("variable \"database_url\""),
            "variables.tf must NOT declare a database_url variable — it must be derived \
             in main.tf from the Postgres server the same apply creates: {content}"
        );
    }

    #[test]
    fn variables_tf_declares_bootstrap_image() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("variables.tf")).unwrap();
        assert!(
            content.contains("variable \"bootstrap_image\""),
            "variables.tf must declare bootstrap_image, the placeholder image Terraform \
             uses before any real image has been pushed to ACR: {content}"
        );
    }

    #[test]
    fn main_tf_provider_sets_subscription_id() {
        // AzureRM v4 made subscription_id mandatory for plan/apply, even
        // under `az login` CLI auth — without it, `terraform apply` fails
        // before provisioning anything.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let main_tf = fs::read_to_string(dir.join("main.tf")).unwrap();
        let provider_block = main_tf
            .split("provider \"azurerm\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the azurerm provider block");
        assert!(
            provider_block.contains("subscription_id = var.subscription_id"),
            "the azurerm provider block must set subscription_id: {provider_block}"
        );

        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        assert!(
            variables_tf.contains("variable \"subscription_id\""),
            "variables.tf must declare subscription_id: {variables_tf}"
        );

        let tfvars = fs::read_to_string(dir.join("terraform.tfvars.example")).unwrap();
        assert!(
            tfvars.contains("subscription_id"),
            "terraform.tfvars.example must mention subscription_id so operators don't \
             discover the AzureRM v4 requirement only after `terraform apply` fails: {tfvars}"
        );
    }

    #[test]
    fn tfvars_example_generates_postgres_compliant_admin_password() {
        // Azure Postgres Flexible Server requires the admin password to use
        // characters from at least 3 of {uppercase, lowercase, digit,
        // symbol}. `openssl rand -hex` only ever produces lowercase hex
        // digits (2 of 4 categories); even `-base64` alone only samples its
        // alphabet randomly and could still land on just 2 categories. The
        // command must deterministically guarantee coverage, not rely on
        // probability, by appending a fixed suffix containing all 4.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("terraform.tfvars.example")).unwrap();
        assert!(
            content.contains("TF_VAR_database_admin_password=\"$(openssl rand -base64 18)Aa1!\""),
            "the documented database_admin_password generator must append a fixed \
             upper/lower/digit/symbol suffix so all 4 character classes are guaranteed, \
             not merely probable: {content}"
        );
    }

    #[test]
    fn outputs_tf_has_app_fqdn_and_acr_login_server() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        assert!(
            content.contains("output \"app_fqdn\""),
            "outputs.tf must expose the app's FQDN: {content}"
        );
        assert!(
            content.contains("output \"acr_login_server\""),
            "outputs.tf must expose the ACR login server: {content}"
        );
        assert!(
            content.contains("output \"migrate_job_name\""),
            "outputs.tf must expose the migration job's name so CI can start it: {content}"
        );
        assert!(
            content.contains("output \"app_name\""),
            "outputs.tf must expose the sanitized Container App name so CI never has to \
             hardcode it: {content}"
        );
        assert!(
            !content.contains("{{"),
            "outputs.tf must not contain unsubstituted template placeholders: {content}"
        );
    }

    #[test]
    fn outputs_tf_app_fqdn_is_the_stable_ingress_hostname_not_revision_specific() {
        // azurerm_container_app.this.latest_revision_fqdn names a specific
        // *revision*, not the app's stable ingress hostname — visiting it
        // sends a different Host header than AUTUMN_SECURITY__
        // TRUSTED_HOSTS__HOSTS allows (400), and it would go stale as soon
        // as CI creates a new revision outside Terraform. Must use the same
        // local.app_fqdn already wired into the trusted-hosts env var.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        let app_fqdn_block = content
            .split("output \"app_fqdn\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("outputs.tf must declare the app_fqdn output");
        assert!(
            app_fqdn_block.contains("local.app_fqdn"),
            "app_fqdn must be local.app_fqdn, not a revision-specific attribute: \
             {app_fqdn_block}"
        );
        assert!(
            !app_fqdn_block.contains("latest_revision_fqdn"),
            "app_fqdn must not use latest_revision_fqdn, which names a specific \
             revision rather than the stable ingress hostname: {app_fqdn_block}"
        );
    }

    #[test]
    fn tfvars_example_documents_non_secret_defaults_without_committing_secrets() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-blog");
        init(&dir, "my-blog", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join("terraform.tfvars.example")).unwrap();
        assert!(
            content.contains("my-blog"),
            "terraform.tfvars.example must substitute the project name: {content}"
        );
        assert!(
            content.contains("app_name"),
            "terraform.tfvars.example must document app_name: {content}"
        );
        for line in content.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with('#') || trimmed.is_empty() {
                continue;
            }
            assert!(
                !trimmed.starts_with("database_url") && !trimmed.starts_with("signing_secret"),
                "terraform.tfvars.example must never assign a literal secret value: {trimmed}"
            );
        }
        assert!(
            !content.contains("{{"),
            "terraform.tfvars.example must not contain unsubstituted placeholders: {content}"
        );
    }

    #[test]
    fn azure_workflow_triggers_on_tag_push_and_manual_dispatch() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("tags:"),
            "azure-deploy.yml must trigger on tag push: {content}"
        );
        assert!(
            content.contains("workflow_dispatch:"),
            "azure-deploy.yml must also support manual dispatch: {content}"
        );
    }

    #[test]
    fn azure_workflow_documents_resource_group_scope_rbac() {
        // RBAC granted on the Container App does not inherit to the
        // sibling migration Container Apps Job — a service principal with
        // Contributor scoped only to the app would 403 when the migration
        // step tries to start the job. The header must say resource-group
        // scope, not "on the Container App".
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        // Scope to the header comment (before the workflow body) — the body
        // legitimately contains `--resource-group` CLI flags, which would
        // make a bare substring check pass regardless of whether the header
        // actually documents the RBAC scoping requirement.
        let header = content
            .split("\nname: azure-deploy")
            .next()
            .expect("azure-deploy.yml must have a header comment before `name:`");
        let header_lower = header.to_lowercase();
        assert!(
            header_lower.contains("resource-group") || header_lower.contains("resource group"),
            "azure-deploy.yml's header must document resource-group-scoped Contributor \
             access, covering both the app and the migration job: {header}"
        );
        assert!(
            header_lower.contains("contributor"),
            "azure-deploy.yml's header must mention the Contributor role: {header}"
        );
    }

    #[test]
    fn azure_workflow_builds_pushes_to_acr_and_deploys() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("docker build") || content.contains("docker/build-push-action"),
            "azure-deploy.yml must build the release image: {content}"
        );
        assert!(
            content.contains("azurecr.io"),
            "azure-deploy.yml must push to the Azure Container Registry: {content}"
        );
        assert!(
            content.contains("az containerapp update")
                || content.contains("containerapps-deploy-action"),
            "azure-deploy.yml must deploy the new image to the Container App: {content}"
        );
    }

    #[test]
    fn azure_workflow_passes_git_provenance_build_args_to_docker() {
        // The Dockerfile's AUTUMN_BUILD_* ARGs default to empty unless
        // passed at `docker build` time, and .dockerignore excludes .git
        // from the build context — so without these, every image this
        // workflow builds reports null git provenance at /actuator/info.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        for arg in [
            "AUTUMN_BUILD_GIT_SHA",
            "AUTUMN_BUILD_GIT_SHA_SHORT",
            "AUTUMN_BUILD_GIT_BRANCH",
            "AUTUMN_BUILD_GIT_DIRTY",
            "AUTUMN_BUILD_TIMESTAMP",
        ] {
            assert!(
                content.contains(&format!("--build-arg {arg}=")),
                "azure-deploy.yml's docker build must pass --build-arg {arg}: {content}"
            );
        }
    }

    #[test]
    fn azure_workflow_updates_migration_job_image_before_starting_it() {
        // `az containerapp job start --image ...` sends an execution-TEMPLATE
        // OVERRIDE, which Azure treats as a full replacement, not a merge —
        // an override containing only --image drops the Terraform-configured
        // `command` (autumn migrate) and the AUTUMN_DATABASE__PRIMARY_URL
        // secret env, so the execution would run the container's default
        // command with no DB URL instead of applying migrations. The image
        // must instead be persisted onto the job's stored template via
        // `job update --image` BEFORE a bare `job start` (no --image) runs
        // that complete, up-to-date template.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();

        let migration_step = content
            .split("Run database migrations")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("a 'Run database migrations' step must exist");

        let update_pos = migration_step
            .find("az containerapp job update \\")
            .expect("the migration job's image must be persisted via `job update` first");
        let start_pos = migration_step
            .find("az containerapp job start \\")
            .expect("`job start` must follow to actually run the now-updated template");
        assert!(
            update_pos < start_pos,
            "the job's image must be updated BEFORE it's started: {migration_step}"
        );

        let update_block = &migration_step[update_pos..start_pos];
        assert!(
            update_block.contains("--image"),
            "`job update` must be the one that carries --image: {update_block}"
        );

        // `job start`'s own invocation (up to its `EXECUTION=$(...)` closing
        // paren) must be bare — no --image — since sending one there would
        // reintroduce the template-override bug this test guards against.
        let start_block_end = migration_step[start_pos..]
            .find("--query name -o tsv)")
            .map(|i| start_pos + i)
            .expect("job start must capture the execution name via --query");
        let start_block = &migration_step[start_pos..start_block_end];
        assert!(
            !start_block.contains("--image"),
            "`job start` must not carry --image — that overrides (not merges) the \
             execution template, dropping the command/secret env `job update` just set: \
             {start_block}"
        );
    }

    #[test]
    fn azure_workflow_migration_poll_budget_exceeds_job_timeout() {
        // main.tf sets replica_timeout_in_seconds = 600 on the migration
        // job; Azure self-terminates the execution at that point
        // regardless. Polling for any less risks reporting "timed out" on
        // a migration that's still validly running (and would have
        // succeeded), while leaving it to keep mutating the schema in the
        // background after this workflow has already given up.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let main_tf = fs::read_to_string(dir.join("main.tf")).unwrap();
        let job_timeout: u64 = main_tf
            .split("replica_timeout_in_seconds = ")
            .nth(1)
            .and_then(|rest| rest.lines().next())
            .and_then(|n| n.trim().parse().ok())
            .expect("main.tf must declare the migration job's replica_timeout_in_seconds");

        let workflow = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        let iterations: u64 = workflow
            .split("seq 1 ")
            .nth(1)
            .and_then(|rest| rest.split([')', ' ']).next())
            .and_then(|n| n.trim().parse().ok())
            .expect("the migration poll loop must use `seq 1 N`");
        let sleep_secs: u64 = workflow
            .split("sleep ")
            .nth(1)
            .and_then(|rest| rest.lines().next())
            .and_then(|s| s.trim().parse().ok())
            .expect("the migration poll loop must sleep a fixed number of seconds per iteration");
        let poll_budget = iterations * sleep_secs;

        assert!(
            poll_budget > job_timeout,
            "the poll budget ({poll_budget}s = {iterations} x {sleep_secs}s) must exceed \
             the migration job's own replica_timeout_in_seconds ({job_timeout}s), or a \
             still-valid migration can be falsely reported as timed out: {workflow}"
        );
    }

    #[test]
    fn azure_workflow_never_hardcodes_credentials() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("secrets."),
            "azure-deploy.yml must source credentials from GitHub Actions secrets, \
             never hardcode them: {content}"
        );
    }

    #[test]
    fn azure_workflow_runs_migrations_before_updating_the_app() {
        // The generated production config sets auto_migrate_in_production =
        // false, so nothing else runs migrations; without this step, new
        // replicas would start against an unmigrated schema on any release
        // that includes a migration.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("az containerapp job start"),
            "azure-deploy.yml must start the one-shot migration job: {content}"
        );
        assert!(
            content.contains("AZURE_MIGRATE_JOB_NAME"),
            "azure-deploy.yml must reference the migration job by its Terraform output: {content}"
        );

        // Match the actual invocations (with their line-continuation
        // backslash), not just the bare phrase — an explanatory comment
        // elsewhere (e.g. about concurrency) may legitimately mention
        // "az containerapp update" in prose without a trailing "\".
        let job_pos = content
            .find("az containerapp job start \\")
            .expect("migration job start must be present");
        let deploy_pos = content
            .find("az containerapp update \\")
            .expect("deploy step must be present");
        assert!(
            job_pos < deploy_pos,
            "the migration job must run BEFORE the app is updated to the new image: {content}"
        );
    }

    #[test]
    fn azure_workflow_aborts_deploy_on_migration_failure() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        let migration_step = content
            .split("Run database migrations")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("a 'Run database migrations' step must exist");
        assert!(
            migration_step.contains("set -euo pipefail") || migration_step.contains("exit 1"),
            "the migration step must fail the job (and therefore never reach the deploy \
             step) when the job execution doesn't succeed: {migration_step}"
        );
    }

    #[test]
    fn azure_workflow_sources_app_name_from_terraform_not_hardcoded() {
        // If an operator edits `app_name` in terraform.tfvars after
        // scaffolding, Terraform renames the Container App to match — the
        // workflow must follow that rename, not target whatever the Cargo
        // package was called when `autumn release init` ran. So it reads
        // AZURE_APP_NAME from `terraform output app_name` (like
        // AZURE_RESOURCE_GROUP/AZURE_MIGRATE_JOB_NAME) rather than having
        // any project-derived name baked in at scaffold time.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "My_Test_App");
        init(
            &dir,
            "My_Test_App",
            false,
            Target::AzureContainerApps,
            false,
        )
        .unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("AZURE_APP_NAME"),
            "azure-deploy.yml must reference AZURE_APP_NAME: {content}"
        );
        assert!(
            content.contains("vars.AZURE_APP_NAME"),
            "AZURE_APP_NAME must be sourced from a repository variable \
             (terraform output app_name), not hardcoded: {content}"
        );
        assert!(
            !content.contains("My_Test_App") && !content.contains("my-test-app"),
            "azure-deploy.yml must not bake in any form of the project name as an \
             Azure resource identifier: {content}"
        );
        assert!(
            !content.contains("{{azure_app_name}}") && !content.contains("{{project_name}}"),
            "azure-deploy.yml must not contain unsubstituted placeholders: {content}"
        );
    }

    #[test]
    fn azure_workflow_sanitizes_ref_name_for_docker_tag() {
        // A `v*` push tag or a workflow_dispatch branch name may contain
        // characters Docker tags reject beyond just "/" (a branch like
        // "feature/login") — e.g. "+" (a valid SemVer tag like
        // "v1.2.3+build"). Docker tags only allow [A-Za-z0-9_.-], so every
        // other character must be sanitized, not just "/" special-cased.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("tr -c 'A-Za-z0-9_.-' '-'"),
            "azure-deploy.yml must map every character outside Docker's tag charset \
             (not just \"/\") to \"-\": {content}"
        );
        assert!(
            !content.contains(":${GITHUB_REF_NAME}") && !content.contains(":$GITHUB_REF_NAME"),
            "no docker/az command may use the raw, unsanitized ref as an image tag: {content}"
        );
    }

    #[test]
    fn azure_workflow_image_tag_never_starts_with_invalid_character() {
        // Docker's tag grammar requires the first character to be a word
        // character ([A-Za-z0-9_]) — "." and "-" are valid elsewhere but
        // not at position zero. `tr` mapping invalid characters to "-" can
        // leave one at the start (e.g. a ref beginning with "+"), which
        // `docker build` rejects.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("sed -E 's/^[.-]+//'"),
            "azure-deploy.yml must strip a leading \".\"/\"-\" left by sanitization: {content}"
        );
        assert!(
            content.contains("[ -z \"$SAFE_REF\" ] && SAFE_REF=\"build\""),
            "azure-deploy.yml must fall back to a valid literal if stripping leaves an \
             empty ref (a ref made entirely of invalid characters): {content}"
        );
    }

    #[test]
    fn azure_workflow_image_tag_is_unique_per_execution() {
        // Two workflow_dispatch runs on the same branch would otherwise
        // compute the identical tag despite different commits — the commit
        // SHA guards against that. But re-running workflow_dispatch on the
        // same branch, or clicking "Re-run jobs" on an existing run, reuses
        // the identical ref AND commit while still producing a genuinely
        // different build (a fresh AUTUMN_BUILD_TIMESTAMP, possibly
        // different base-image bytes) — so the tag must also include
        // GITHUB_RUN_ID (unique per trigger) and GITHUB_RUN_ATTEMPT
        // (disambiguates re-runs of that same trigger) to be unique per
        // actual execution, not just per commit. Re-pushing bytes under a
        // tag Azure already has configured on the Container App isn't
        // guaranteed to register as a revision-scope change, so the old
        // binary could keep serving against a newly migrated schema.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("${GITHUB_SHA:0:12}"),
            "the computed image tag must include the commit SHA: {content}"
        );
        assert!(
            content.contains("${GITHUB_RUN_ID}") && content.contains("${GITHUB_RUN_ATTEMPT}"),
            "the computed image tag must also include the run ID and run attempt, so \
             a re-run of the same trigger (same ref, same commit) never collides with \
             the original run's tag: {content}"
        );
        // Docker tags cap at 128 characters; reserve room for the
        // SHA/run-id/run-attempt suffix rather than letting the sanitized
        // ref alone consume it.
        assert!(
            content.contains("cut -c1-80"),
            "the sanitized ref portion must leave headroom for the rest of the tag \
             within Docker's 128-character limit: {content}"
        );
    }

    #[test]
    fn azure_workflow_serializes_overlapping_runs() {
        // Two overlapping runs (e.g. two rapid tag pushes, or a tag push
        // racing a manual dispatch) must not interleave: the older run's
        // later `az containerapp update` could execute after the newer one
        // and roll production back.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("concurrency:"),
            "azure-deploy.yml must define a concurrency group so overlapping runs \
             queue instead of racing: {content}"
        );
        assert!(
            content.contains("cancel-in-progress: false"),
            "cancel-in-progress must be false — killing a run mid-migration or \
             mid-cutover is worse than making the next run wait: {content}"
        );
    }

    #[test]
    fn azure_workflow_guards_against_superseded_run_before_migrating() {
        // GitHub does not document strict FIFO ordering for which queued
        // run in a concurrency group goes next. A same-ref check isn't
        // enough either: two DIFFERENT immutable tags (e.g. v1 then v2)
        // each trigger their own run against their own never-moving ref,
        // so the guard must be ref-agnostic — comparing run_number (GitHub's
        // own monotonic-in-trigger-order counter) against other runs of
        // this workflow, not "has my own ref moved".
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert!(
            content.contains("actions: read"),
            "azure-deploy.yml must grant actions: read to query other workflow runs: {content}"
        );
        assert!(
            content.contains("run_number > ${{ github.run_number }}"),
            "the guard must compare against other runs' run_number, not just whether \
             this run's own ref has moved: {content}"
        );
        // The guard must NOT filter by status/conclusion (e.g. only
        // "in_progress"/"queued", or "completed" + conclusion == "success")
        // — a newer run can migrate (the actual point of no return, since
        // the schema is advanced at that point) and then fail on a LATER
        // step, reporting an overall conclusion of "failure". There's no
        // cheap way to tell "failed before migrating" apart from "failed
        // after migrating" from a run's top-level status, so the mere
        // existence of any newer run must be disqualifying, full stop.
        let guard_step = content
            .split("Abort if a newer run of this workflow exists")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("the run_number staleness guard step must be present");
        assert!(
            !guard_step.contains("in_progress")
                && !guard_step.contains("queued")
                && !guard_step.contains("conclusion"),
            "the guard must not filter by status or conclusion — any newer run_number \
             must disqualify this run regardless of outcome: {guard_step}"
        );

        let guard_pos = content
            .find("gh api")
            .expect("the run_number staleness guard must be present");
        let job_pos = content
            .find("az containerapp job start \\")
            .expect("migration job start must be present");
        let deploy_pos = content
            .find("az containerapp update \\")
            .expect("deploy step must be present");
        assert!(
            guard_pos < job_pos && job_pos < deploy_pos,
            "the staleness guard must run BEFORE migration, which must run BEFORE \
             deploy: {content}"
        );
    }

    #[test]
    fn init_without_force_errors_if_azure_workflow_file_exists() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::create_dir_all(dir.join(".github/workflows")).unwrap();
        fs::write(dir.join(".github/workflows/azure-deploy.yml"), "existing").unwrap();
        let err = init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap_err();
        assert!(matches!(err, ReleaseError::FileExists(_)));
    }

    #[test]
    fn azure_target_adds_terraform_gitignore_entries_when_missing() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".gitignore")).unwrap();
        for line in TERRAFORM_GITIGNORE_ENTRIES {
            assert!(
                content.lines().any(|l| l.trim() == *line),
                "azure target must add `{line}` to .gitignore: {content}"
            );
        }
    }

    #[test]
    fn azure_target_gitignore_merge_preserves_existing_lines_and_is_idempotent() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join(".gitignore"), "/target\n.env\n").unwrap();

        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let after_first = fs::read_to_string(dir.join(".gitignore")).unwrap();
        assert!(after_first.contains("/target"), "{after_first}");
        assert!(after_first.contains(".env"), "{after_first}");
        for line in TERRAFORM_GITIGNORE_ENTRIES {
            assert!(
                after_first.lines().any(|l| l.trim() == *line),
                "{after_first}"
            );
        }

        // Re-running (e.g. under --force) must not duplicate entries.
        init(&dir, "my-app", true, Target::AzureContainerApps, false).unwrap();
        let after_second = fs::read_to_string(dir.join(".gitignore")).unwrap();
        for line in TERRAFORM_GITIGNORE_ENTRIES {
            let count = after_second.lines().filter(|l| l.trim() == *line).count();
            assert_eq!(
                count, 1,
                "`{line}` must appear exactly once: {after_second}"
            );
        }
    }

    #[test]
    fn azure_target_reasserts_a_gitignore_entry_negated_after_its_earlier_occurrence() {
        // Git applies the LAST matching rule: an existing .gitignore with
        // "terraform.tfvars" followed later by "!terraform.tfvars" makes
        // the file trackable, even though the literal pattern is present.
        // A naive "is this line already there" check would wrongly treat
        // it as already protected and add nothing, letting an operator
        // commit the plaintext secrets this scaffold exists to keep out.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(
            dir.join(".gitignore"),
            "terraform.tfvars\nsome-other-line\n!terraform.tfvars\n",
        )
        .unwrap();

        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".gitignore")).unwrap();

        // The negation must not have been touched (still there, doing its
        // job for whatever the operator originally wanted un-ignored)...
        assert!(content.contains("!terraform.tfvars"), "{content}");
        // ...but "terraform.tfvars" must be re-asserted AFTER it (not just
        // matched as a substring of "!terraform.tfvars" itself), since
        // that's the only way to make it the final (and therefore
        // effective) matching rule again.
        let after_negation = content.find("!terraform.tfvars").unwrap() + "!terraform.tfvars".len();
        assert!(
            content[after_negation..].contains("terraform.tfvars"),
            "terraform.tfvars must be re-added after the negation that defeated it: {content}"
        );
    }

    #[test]
    fn azure_target_reasserts_entry_after_a_broader_wildcard_negation() {
        // A wildcard negation like "!*.tfvars" or a blanket "!*" also
        // un-ignores "terraform.tfvars" under git's own semantics, not
        // just an exact "!terraform.tfvars" match. Matching gitignore's
        // full glob syntax is out of scope, so any negation line
        // appearing after the pattern must conservatively be treated as
        // potentially applying to it.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join(".gitignore"), "terraform.tfvars\n!*.tfvars\n").unwrap();

        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".gitignore")).unwrap();

        let after_negation = content.find("!*.tfvars").unwrap() + "!*.tfvars".len();
        assert!(
            content[after_negation..].contains("terraform.tfvars"),
            "terraform.tfvars must be re-added after a broader wildcard negation too: \
             {content}"
        );
    }

    #[test]
    fn azure_target_propagates_gitignore_read_errors_instead_of_clobbering_it() {
        // A read failure that ISN'T "file doesn't exist" (invalid UTF-8,
        // permission denied, ...) must not be silently treated as "no
        // file" — doing so would make the merge overwrite the operator's
        // real (if unreadable-by-us) .gitignore with just the Terraform
        // entries, destroying whatever rules were already there.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join(".gitignore"), [0xFF, 0xFE, 0x00, 0xFF]).unwrap();

        let err = init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap_err();
        assert!(
            matches!(err, ReleaseError::Io(_)),
            "an unreadable .gitignore must surface as an I/O error, not be silently \
             replaced: {err}"
        );
        // The original (if unreadable) content must be left untouched.
        let raw = fs::read(dir.join(".gitignore")).unwrap();
        assert_eq!(raw, vec![0xFF, 0xFE, 0x00, 0xFF]);
    }

    #[test]
    fn azure_target_gitignore_failure_leaves_no_partial_scaffold() {
        // The .gitignore merge must be validated BEFORE any scaffold file
        // is written — otherwise a read failure here leaves a partial,
        // complete-looking scaffold on disk, and a retry without --force
        // immediately fails on the files this very call just created.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join(".gitignore"), [0xFF, 0xFE, 0x00, 0xFF]).unwrap();

        init(&dir, "my-app", false, Target::AzureContainerApps, false).unwrap_err();

        for name in [
            "Dockerfile",
            ".dockerignore",
            "autumn.production.toml.example",
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
            ".github/workflows/azure-deploy.yml",
        ] {
            assert!(
                !dir.join(name).exists(),
                "{name} must not exist after init() fails on the .gitignore merge \
                 (found a partial scaffold, which blocks retrying without --force)"
            );
        }
    }

    #[test]
    fn non_azure_targets_do_not_modify_gitignore() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join(".gitignore"), "/target\n").unwrap();
        init(&dir, "my-app", false, Target::DockerCompose, false).unwrap();
        let content = fs::read_to_string(dir.join(".gitignore")).unwrap();
        assert_eq!(
            content, "/target\n",
            "non-azure targets must leave .gitignore untouched"
        );
    }

    #[test]
    fn init_with_force_overwrites_nested_azure_workflow_file() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::create_dir_all(dir.join(".github/workflows")).unwrap();
        fs::write(
            dir.join(".github/workflows/azure-deploy.yml"),
            "old content",
        )
        .unwrap();
        init(&dir, "my-app", true, Target::AzureContainerApps, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/azure-deploy.yml")).unwrap();
        assert_ne!(content, "old content");
    }

    // ── --split-workers (opt-in split topology) ───────────────────────────────

    #[test]
    fn docker_compose_default_omits_worker_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::DockerCompose, false).unwrap();
        let content = fs::read_to_string(dir.join("docker-compose.yml")).unwrap();
        assert!(
            !content.contains("worker:"),
            "default docker-compose.yml must NOT scaffold a worker service: {content}"
        );
        assert!(
            !content.contains("AUTUMN_ROLE"),
            "default (combined) docker-compose.yml must not pin a process role: {content}"
        );
        // No leftover template placeholders.
        assert!(
            !content.contains("{{"),
            "docker-compose.yml must not contain unsubstituted placeholders: {content}"
        );
    }

    #[test]
    fn docker_compose_split_workers_emits_worker_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::DockerCompose, true).unwrap();
        let content = fs::read_to_string(dir.join("docker-compose.yml")).unwrap();

        assert!(
            content.contains("worker:"),
            "--split-workers must scaffold a worker service: {content}"
        );
        // Worker runs the SAME image as the web/app service.
        assert_eq!(
            content.matches("build: .").count(),
            3,
            "app, worker, and migrate services must all build the same image: {content}"
        );
        assert!(
            content.contains("AUTUMN_ROLE: worker"),
            "worker service must run the worker role: {content}"
        );
        assert!(
            content.contains("AUTUMN_ROLE: web"),
            "app service must run the web role when split: {content}"
        );
        // Both tiers must use a durable jobs backend (not in-process `local`).
        assert_eq!(
            content.matches("AUTUMN_JOBS__BACKEND: postgres").count(),
            2,
            "both app and worker must share the durable postgres jobs backend: {content}"
        );
        // Worker shares the migration gate and signing secret.
        assert!(
            content.contains("condition: service_completed_successfully"),
            "worker must wait for the one-shot migration job: {content}"
        );
        assert!(
            content.contains(
                r#"AUTUMN_SECURITY__SIGNING_SECRET: "${AUTUMN_SECURITY__SIGNING_SECRET:?set it first}""#
            ),
            "worker must pass the required signing secret like the app service: {content}"
        );
        // Placeholders (including the worker block's own project name) resolved.
        assert!(
            !content.contains("{{"),
            "split docker-compose.yml must not contain unsubstituted placeholders: {content}"
        );
        assert!(
            content.contains("my-app_prod"),
            "worker DB URL must substitute the project name: {content}"
        );
    }

    #[test]
    fn split_workers_only_affects_docker_compose_output() {
        // The split-topology flag is scoped to the compose file; the Dockerfile
        // and other scaffolds are byte-for-byte identical regardless.
        let default_dockerfile = render(templates::DOCKERFILE, "my-app", false, false);
        let split_dockerfile = render(templates::DOCKERFILE, "my-app", false, true);
        assert_eq!(
            default_dockerfile, split_dockerfile,
            "--split-workers must not change the Dockerfile"
        );
    }

    // ── workspace root error ──────────────────────────────────────────────────

    #[test]
    fn workspace_root_gives_actionable_hint() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path().to_path_buf();
        fs::write(
            dir.join("Cargo.toml"),
            "[workspace]\nmembers = [\"my-app\"]\n",
        )
        .unwrap();
        let err = read_project_name(&dir).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("workspace"),
            "error must mention workspace: {msg}"
        );
        assert!(
            msg.contains("member"),
            "error must hint to run from a member directory: {msg}"
        );
    }

    // ── nested workflow discoverability (git root vs. workspace member) ───────

    #[test]
    fn nested_workflow_relocation_warning_is_silent_at_the_git_root() {
        // The common case: `dir` IS the git repository root (a single-crate
        // repo, or a workspace member the user happens to be running from
        // the top of anyway). `.github/workflows/azure-deploy.yml` lands
        // exactly where GitHub looks for it — nothing to warn about.
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path().to_path_buf();
        fs::create_dir_all(dir.join(".git")).unwrap();
        assert_eq!(
            nested_workflow_relocation_warning(&dir, ".github/workflows/azure-deploy.yml"),
            None
        );
    }

    #[test]
    fn nested_workflow_relocation_warning_flags_a_nested_workspace_member() {
        // `autumn release init` explicitly supports running from a Cargo
        // workspace member directory (read_project_name only rejects the
        // workspace root itself) — but GitHub Actions only discovers
        // workflows under the git repository ROOT's .github/workflows/, so
        // a workflow written under a member subdirectory would silently
        // never fire. This must be flagged, not silently mis-scaffolded.
        let tmp = TempDir::new().unwrap();
        let git_root = tmp.path().to_path_buf();
        fs::create_dir_all(git_root.join(".git")).unwrap();
        let member_dir = git_root.join("examples").join("blog");
        fs::create_dir_all(&member_dir).unwrap();

        let warning =
            nested_workflow_relocation_warning(&member_dir, ".github/workflows/azure-deploy.yml")
                .expect("a workflow nested under a workspace member must be flagged");
        assert!(
            warning.contains(&git_root.display().to_string()),
            "the warning must name the actual git root: {warning}"
        );
        assert!(
            warning.contains("working-directory: examples/blog"),
            "the warning must give the exact working-directory override needed so \
             the relocated workflow's docker build step still finds this crate's \
             Dockerfile: {warning}"
        );
    }

    // ── auto-migration config ─────────────────────────────────────────────────

    #[test]
    fn production_config_disables_startup_migrations_by_default() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        let content = fs::read_to_string(dir.join("autumn.production.toml.example")).unwrap();
        assert!(
            content.contains("auto_migrate_in_production = false"),
            "production config must leave web replicas out of migration ownership"
        );
        assert!(
            content.contains("primary_url"),
            "production config must name the primary/write database role"
        );
        assert!(
            content.contains("autumn migrate"),
            "production config must point operators at the one-shot migration command"
        );
    }

    // ── target parsing ────────────────────────────────────────────────────────

    #[test]
    fn parse_target_fly() {
        assert_eq!("fly".parse::<Target>().unwrap(), Target::Fly);
    }

    #[test]
    fn parse_target_docker_compose() {
        assert_eq!(
            "docker-compose".parse::<Target>().unwrap(),
            Target::DockerCompose
        );
    }

    #[test]
    fn parse_target_azure_container_apps() {
        assert_eq!(
            "azure-container-apps".parse::<Target>().unwrap(),
            Target::AzureContainerApps
        );
    }

    #[test]
    fn parse_target_aws_app_runner() {
        assert_eq!(
            "aws-app-runner".parse::<Target>().unwrap(),
            Target::AwsAppRunner
        );
    }

    #[test]
    fn parse_target_aws_ecs() {
        assert_eq!("aws-ecs".parse::<Target>().unwrap(), Target::AwsEcs);
    }

    #[test]
    fn parse_target_gcp_cloud_run() {
        assert_eq!(
            "gcp-cloud-run".parse::<Target>().unwrap(),
            Target::GcpCloudRun
        );
    }

    #[test]
    fn parse_target_unknown_is_error() {
        assert!("kubernetes".parse::<Target>().is_err());
    }

    #[test]
    fn parse_target_unknown_error_mentions_all_targets() {
        let err = "kubernetes".parse::<Target>().unwrap_err();
        for name in [
            "fly",
            "docker-compose",
            "azure-container-apps",
            "aws-app-runner",
            "aws-ecs",
            "gcp-cloud-run",
        ] {
            assert!(err.contains(name), "error must mention '{name}': {err}");
        }
    }

    // ── --target=aws-app-runner ─────────────────────────────────────────────────

    #[test]
    fn aws_app_runner_target_creates_all_expected_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        for name in [
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
        ] {
            assert!(
                dir.join(name).is_file(),
                "{name} must be created for --target=aws-app-runner"
            );
        }
        assert!(dir.join("Dockerfile").is_file());
        assert!(dir.join(".dockerignore").is_file());
        assert!(dir.join("autumn.production.toml.example").is_file());
    }

    #[test]
    fn aws_app_runner_target_does_not_create_a_workflow() {
        // Per the issue: aws-app-runner is the fast/minimal path with no CI
        // workflow (unlike azure-container-apps and aws-ecs).
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        let files = init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        assert!(
            !files.iter().any(|f| f.contains(".github/workflows")),
            "aws-app-runner must not emit a CI workflow: {files:?}"
        );
        assert!(!dir.join(".github").exists());
    }

    #[test]
    fn default_target_does_not_create_aws_app_runner_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        for name in [
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
        ] {
            assert!(
                !dir.join(name).exists(),
                "{name} must NOT be created for the default target"
            );
        }
    }

    #[test]
    fn aws_app_runner_main_tf_has_ecr_and_app_runner_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("aws_ecr_repository"),
            "main.tf must provision an ECR repository: {content}"
        );
        assert!(
            content.contains("resource \"aws_apprunner_service\""),
            "main.tf must provision the App Runner service: {content}"
        );
    }

    #[test]
    fn aws_app_runner_main_tf_has_vpc_and_vpc_connector() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("resource \"aws_vpc\""),
            "main.tf must provision a VPC: {content}"
        );
        assert!(
            content.contains("aws_apprunner_vpc_connector"),
            "main.tf must provision an App Runner VPC connector so App Runner can reach RDS \
             privately: {content}"
        );
    }

    #[test]
    fn aws_app_runner_main_tf_has_nat_gateway_for_general_egress() {
        // network_configuration.egress_configuration.egress_type = "VPC"
        // routes ALL of the app's own outbound traffic through the private
        // subnets, not just RDS-bound traffic — without a NAT gateway, any
        // outbound call the app itself makes would silently hang or fail.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("aws_nat_gateway"),
            "main.tf must provision a NAT gateway so the app's own outbound traffic \
             (not just RDS-bound traffic) keeps working once egress_type = \"VPC\": {content}"
        );
        assert!(
            content.contains("egress_type       = \"VPC\"")
                || content.contains("egress_type = \"VPC\""),
            "main.tf must route App Runner egress through the VPC connector: {content}"
        );
    }

    #[test]
    fn aws_app_runner_main_tf_has_rds_postgres() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("resource \"aws_db_instance\""),
            "main.tf must provision an RDS Postgres instance: {content}"
        );
        assert!(
            content.contains("engine         = \"postgres\"")
                || content.contains("engine = \"postgres\""),
            "the RDS instance must use the postgres engine: {content}"
        );
        assert!(
            content.contains("publicly_accessible    = false")
                || content.contains("publicly_accessible = false"),
            "RDS must not be publicly accessible: {content}"
        );
    }

    #[test]
    fn aws_app_runner_provisions_a_reachable_one_shot_migration_task() {
        // App Runner has no release-phase hook of its own, and RDS is
        // private with no public entry point — main.tf must provision SOME
        // real, reachable compute for `autumn migrate` to run against,
        // rather than leaving that to unwritten operator infrastructure.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        for resource in [
            "resource \"aws_ecs_cluster\" \"migrate\"",
            "resource \"aws_ecs_task_definition\" \"migrate\"",
            "resource \"aws_iam_role\" \"migrate_execution\"",
            "resource \"aws_iam_role\" \"migrate_task\"",
        ] {
            assert!(
                content.contains(resource),
                "main.tf must declare {resource} so the migration task has somewhere \
                 to run: {content}"
            );
        }
        let task_def_block = content
            .split("resource \"aws_ecs_task_definition\" \"migrate\"")
            .nth(1)
            .expect("main.tf must declare the migrate task definition");
        assert!(
            task_def_block.contains("autumn migrate"),
            "the migrate task must actually run `autumn migrate`: {task_def_block}"
        );
        assert!(
            task_def_block.contains("aws_secretsmanager_secret.database_url.arn"),
            "the migrate task must be able to read the derived database URL: {task_def_block}"
        );
    }

    #[test]
    fn aws_app_runner_migration_task_reuses_the_vpc_connector_security_group() {
        // RDS's ingress rule (aws_security_group.database) only trusts
        // aws_security_group.vpc_connector as a source — the migration
        // task must be run with that SAME security group attached (via the
        // vpc_connector_security_group_id output) rather than provisioning
        // a second, parallel security group RDS's rule doesn't know about.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let outputs = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        for name in [
            "migrate_cluster_name",
            "migrate_task_family",
            "private_subnet_ids",
            "vpc_connector_security_group_id",
        ] {
            assert!(
                outputs.contains(&format!("output \"{name}\"")),
                "outputs.tf must declare output \"{name}\" for the migration task's \
                 `aws ecs run-task` call: {outputs}"
            );
        }
        assert!(
            outputs.contains("aws_security_group.vpc_connector.id"),
            "vpc_connector_security_group_id must reuse the SAME security group RDS's \
             ingress rule trusts, not a new one: {outputs}"
        );
    }

    #[test]
    fn aws_app_runner_main_tf_derives_database_url_not_a_variable() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let main_tf = fs::read_to_string(dir.join("main.tf")).unwrap();
        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        assert!(
            main_tf.contains("AUTUMN_DATABASE__PRIMARY_URL"),
            "main.tf must wire the primary DB URL: {main_tf}"
        );
        assert!(
            main_tf.contains("aws_db_instance.this.address"),
            "the database URL must be derived from the RDS instance this apply creates, \
             not pre-computed: {main_tf}"
        );
        assert!(
            !variables_tf.contains("variable \"database_url\""),
            "there must be no database_url variable — a single apply must be able to \
             derive it end-to-end: {variables_tf}"
        );
    }

    #[test]
    fn aws_app_runner_secrets_have_no_default_and_are_sensitive() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("variables.tf")).unwrap();
        for var in ["database_admin_password", "signing_secret"] {
            let block = content
                .split(&format!("variable \"{var}\""))
                .nth(1)
                .and_then(|rest| rest.split('}').next())
                .unwrap_or_else(|| {
                    panic!("variables.tf must declare variable \"{var}\": {content}")
                });
            assert!(
                block.contains("sensitive   = true") || block.contains("sensitive = true"),
                "{var} must be sensitive: {block}"
            );
            assert!(
                !block.contains("default"),
                "{var} must not have a default (never commit a real secret value): {block}"
            );
        }
    }

    #[test]
    fn aws_app_runner_no_committed_secret_literals() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        for name in ["main.tf", "variables.tf", "terraform.tfvars.example"] {
            let content = fs::read_to_string(dir.join(name)).unwrap();
            assert!(
                !content.to_lowercase().contains("password =\""),
                "{name} must not contain a literal password assignment: {content}"
            );
        }
    }

    #[test]
    fn aws_app_runner_service_bootstraps_from_public_placeholder_and_ignores_drift() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let service_block = content
            .split("resource \"aws_apprunner_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare the App Runner service");
        assert!(
            service_block.contains("var.bootstrap_image"),
            "the App Runner service must start from the bootstrap placeholder image: {service_block}"
        );
        assert!(
            service_block
                .contains("ignore_changes = [source_configuration, instance_configuration, health_check_configuration]"),
            "the App Runner service must ignore source_configuration drift once CI/the manual \
             walkthrough deploys the real image: {service_block}"
        );
    }

    #[test]
    fn aws_app_runner_bootstrap_health_check_matches_the_placeholder_not_the_real_app() {
        // aws_apprunner_service blocks `terraform apply` until the service
        // reaches a stable state — declaring port 3000 / path "/health"
        // against a bootstrap image that doesn't serve either would hang
        // the very first apply. The placeholder (nginx) listens on 80 and
        // returns 200 for "/" by default; the real port/path are restored
        // by the deploy walkthrough's cutover call, which owns App
        // Runner's mutable, service-level health check configuration.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("port = \"80\""),
            "the bootstrap image_configuration must declare port 80 (nginx's real default), \
             not 3000: {content}"
        );
        let health_check_block = content
            .split("health_check_configuration {")
            .nth(1)
            .and_then(|rest| rest.split('}').next())
            .expect("main.tf must declare health_check_configuration");
        assert!(
            health_check_block.contains("path     = \"/\""),
            "the bootstrap health check must probe \"/\" (nginx's default 200 response), \
             not \"/health\": {health_check_block}"
        );
    }

    #[test]
    fn aws_app_runner_ignores_health_check_drift_after_cutover_restores_it() {
        // The cutover call (docs/guide/deployment.md) switches
        // health_check_configuration from the bootstrap's "/" to the real
        // app's "/health" — without ignoring this block too, a later
        // `terraform apply` would see that as drift from this resource's
        // own declared "/" and revert it, breaking the real app's health
        // check.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let service_block = content
            .split("resource \"aws_apprunner_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare the App Runner service");
        assert!(
            service_block
                .contains("ignore_changes = [source_configuration, instance_configuration, health_check_configuration]"),
            "the App Runner service must ignore health_check_configuration drift alongside \
             source_configuration: {service_block}"
        );
    }

    #[test]
    fn aws_app_runner_instance_role_secrets_policy_is_scoped_not_wildcard() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let policy_block = content
            .split("data \"aws_iam_policy_document\" \"apprunner_instance_secrets\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}\n").next())
            .expect("main.tf must declare the instance role's secrets policy document");
        assert!(
            !policy_block.contains("resources = [\"*\"]") && !policy_block.contains("\"*\""),
            "the instance role's secrets access must be scoped to specific secret ARNs, \
             never a wildcard: {policy_block}"
        );
        assert!(
            policy_block.contains("aws_secretsmanager_secret.database_url.arn"),
            "the secrets policy must reference the database_url secret ARN: {policy_block}"
        );
    }

    #[test]
    fn aws_app_runner_public_bootstrap_has_no_production_secrets_or_runtime_role() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let service = content
            .split("resource \"aws_apprunner_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare the App Runner service");
        assert!(!service.contains("runtime_environment_secrets ="));
        let instance = service
            .split("instance_configuration {")
            .nth(1)
            .and_then(|block| block.split('}').next())
            .expect("service must configure its bootstrap instance size");
        assert!(!instance.contains("instance_role_arn"));

        let outputs = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        assert!(outputs.contains("output \"apprunner_instance_role_arn\""));
    }

    #[test]
    fn aws_app_runner_public_bootstrap_does_not_wait_for_or_access_secrets() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let service_block = content
            .split("resource \"aws_apprunner_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare aws_apprunner_service.this");
        assert!(!service_block.contains("aws_iam_role_policy.apprunner_instance_secrets"));
        assert!(!service_block.contains("aws_secretsmanager_secret_version.database_url"));
        assert!(!service_block.contains("aws_secretsmanager_secret_version.signing_secret"));
    }

    #[test]
    fn aws_app_runner_names_are_sanitized_for_underscored_uppercase_project_name() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "My_Test_App");
        init(&dir, "My_Test_App", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("lower(replace(var.app_name"),
            "main.tf must lowercase and sanitize app_name for AWS resource names: {content}"
        );
    }

    #[test]
    fn aws_app_runner_postgres_database_name_avoids_reserved_names() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        for reserved in ["postgres", "rdsadmin", "template0", "template1"] {
            assert!(
                content.contains(&format!("\"{reserved}\"")),
                "the reserved-name guard must list {reserved:?}: {content}"
            );
        }
    }

    #[test]
    fn aws_app_runner_target_adds_terraform_gitignore_entries() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join(".gitignore")).unwrap();
        for line in TERRAFORM_GITIGNORE_ENTRIES {
            assert!(content.lines().any(|l| l.trim() == *line), "{content}");
        }
    }

    #[test]
    fn aws_app_runner_dockerignore_excludes_terraform_state() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        for pattern in [".terraform/", "*.tfstate", "terraform.tfvars"] {
            assert!(content.contains(pattern), "{content}");
        }
    }

    #[test]
    fn aws_app_runner_outputs_tf_declares_expected_outputs_with_no_unsubstituted_placeholders() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        for name in [
            "region",
            "app_url",
            "service_arn",
            "service_name",
            "ecr_repository_url",
            "apprunner_access_role_arn",
            "database_url_secret_arn",
            "signing_secret_secret_arn",
        ] {
            assert!(
                content.contains(&format!("output \"{name}\"")),
                "outputs.tf must declare output \"{name}\": {content}"
            );
        }
        assert!(
            !content.contains("{{"),
            "outputs.tf must not contain any unsubstituted template placeholders: {content}"
        );
    }

    #[test]
    fn aws_app_runner_region_output_reflects_the_configured_region_variable() {
        // The deploy walkthrough sources AWS_REGION from this output so
        // every `aws` CLI call targets the same region Terraform
        // provisioned into, regardless of the operator's ambient CLI
        // config — it must echo var.region, not a hardcoded default.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        assert!(content.contains("value       = var.region"), "{content}");
    }

    #[test]
    fn aws_app_runner_secret_arn_outputs_reference_the_real_secrets() {
        // The deploy walkthrough's cutover `update-service` call must
        // re-supply RuntimeEnvironmentSecrets (the call replaces the image
        // configuration wholesale, not merges it) — these outputs are how
        // it gets the ARNs, so they must point at the actual secrets, not
        // just exist as a name.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        assert!(
            content.contains("aws_secretsmanager_secret.database_url.arn"),
            "{content}"
        );
        assert!(
            content.contains("aws_secretsmanager_secret.signing_secret.arn"),
            "{content}"
        );
    }

    #[test]
    fn aws_app_runner_gitignore_merge_is_idempotent_on_repeat_init() {
        // The merge function itself is exhaustively tested against the
        // shared azure-container-apps target (idempotency, negation
        // re-assertion, I/O-failure propagation); this pins that the
        // aws-app-runner call site is actually wired to it under --force,
        // not just on a fresh project.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join(".gitignore"), "/target\n.env\n").unwrap();

        init(&dir, "my-app", false, Target::AwsAppRunner, false).unwrap();
        init(&dir, "my-app", true, Target::AwsAppRunner, false).unwrap();
        let content = fs::read_to_string(dir.join(".gitignore")).unwrap();
        assert!(
            content.contains("/target") && content.contains(".env"),
            "{content}"
        );
        for line in TERRAFORM_GITIGNORE_ENTRIES {
            let count = content.lines().filter(|l| l.trim() == *line).count();
            assert_eq!(count, 1, "`{line}` must appear exactly once: {content}");
        }
    }

    // ── --target=aws-ecs ─────────────────────────────────────────────────────────

    #[test]
    fn aws_ecs_target_creates_all_expected_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        for name in [
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
            ".github/workflows/aws-deploy.yml",
        ] {
            assert!(
                dir.join(name).is_file(),
                "{name} must be created for --target=aws-ecs"
            );
        }
        assert!(dir.join("Dockerfile").is_file());
        assert!(dir.join(".dockerignore").is_file());
        assert!(dir.join("autumn.production.toml.example").is_file());
    }

    #[test]
    fn aws_ecs_target_returns_nested_workflow_path_in_created_list() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        let files = init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        assert!(
            files
                .iter()
                .any(|f| f == ".github/workflows/aws-deploy.yml"),
            "created-files list must include the nested workflow path: {files:?}"
        );
    }

    #[test]
    fn default_target_does_not_create_aws_ecs_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        for name in [
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
            ".github/workflows/aws-deploy.yml",
        ] {
            assert!(
                !dir.join(name).exists(),
                "{name} must NOT be created for the default target"
            );
        }
    }

    #[test]
    fn aws_ecs_main_tf_has_vpc_with_public_and_private_subnets_across_two_azs() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(content.contains("resource \"aws_vpc\""), "{content}");
        assert!(
            content.contains("resource \"aws_subnet\" \"public\"")
                && content.contains("count                   = 2"),
            "main.tf must provision 2 public subnets: {content}"
        );
        assert!(
            content.contains("resource \"aws_subnet\" \"private\"")
                && content.contains("count             = 2"),
            "main.tf must provision 2 private subnets: {content}"
        );
        assert!(
            content.contains("data \"aws_availability_zones\""),
            "subnets must be spread across AZs discovered at apply time, not hardcoded: {content}"
        );
    }

    #[test]
    fn aws_ecs_main_tf_has_alb_with_https_redirect_and_dns_validated_cert() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("resource \"aws_lb\" \"this\""),
            "main.tf must provision an ALB: {content}"
        );
        assert!(
            content.contains("resource \"aws_acm_certificate\""),
            "main.tf must provision an ACM certificate: {content}"
        );
        assert!(
            content.contains("validation_method = \"DNS\""),
            "the ACM certificate must use DNS validation: {content}"
        );
        assert!(
            content.contains("aws_acm_certificate_validation"),
            "main.tf must wait for certificate validation before wiring the HTTPS listener: {content}"
        );
        let http_listener = content
            .split("resource \"aws_lb_listener\" \"http\"")
            .nth(1)
            .and_then(|rest| rest.split("\nresource").next())
            .expect("main.tf must declare the HTTP listener");
        assert!(
            http_listener.contains("HTTP_301") && http_listener.contains("HTTPS"),
            "the HTTP listener must redirect to HTTPS: {http_listener}"
        );
    }

    #[test]
    fn aws_ecs_alb_and_target_group_names_never_exceed_32_char_limit() {
        // ALB and target group names are capped at 32 characters by AWS.
        // Extract the substr() bound in the sanitization local and verify
        // the worst-case rendered name (base + longest suffix) fits.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let base_cap_line = content
            .lines()
            .find(|l| l.trim_start().starts_with("app_name_safe = trim(substr("))
            .expect("main.tf must declare app_name_safe via a bounded substr()");
        assert!(
            base_cap_line.contains(", 20)"),
            "expected a 20-char base cap: {base_cap_line}"
        );
        // 20 (base) + "-migrate-tg" (11) = 31 <= 32.
        assert!(20 + "-migrate-tg".len() <= 32);
        assert!(
            content.contains("\"${local.app_name_safe}-alb\""),
            "the ALB must be named from the sanitized/capped local: {content}"
        );
        assert!(
            content.contains("\"${local.app_name_safe}-tg\""),
            "the target group must be named from the sanitized/capped local: {content}"
        );
    }

    #[test]
    fn aws_ecs_main_tf_has_ecs_fargate_cluster_task_and_service_with_circuit_breaker() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("resource \"aws_ecs_cluster\""),
            "{content}"
        );
        assert!(
            content.contains("resource \"aws_ecs_task_definition\" \"app\"")
                && content.contains("FARGATE"),
            "{content}"
        );
        assert!(
            content.contains("resource \"aws_ecs_service\" \"this\""),
            "{content}"
        );
        let service_block = content
            .split("resource \"aws_ecs_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare the ECS service");
        assert!(
            service_block.contains("deployment_circuit_breaker")
                && service_block.contains("rollback = true"),
            "the ECS service must enable circuit-breaker rollback: {service_block}"
        );
    }

    #[test]
    fn aws_ecs_service_waits_for_secret_versions_before_starting() {
        // local.container_secrets only references the secret CONTAINERS
        // (aws_secretsmanager_secret.*.arn), so Terraform's implicit
        // dependency graph doesn't wait for the *_version resources that
        // actually write the secret values — without an explicit
        // depends_on, the service can schedule tasks before RDS's derived
        // database_url value (or, when enabled, Redis's) exists, and the
        // deployment circuit breaker can permanently fail the first
        // deployment as a result.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let service_block = content
            .split("resource \"aws_ecs_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare the ECS service");
        for dep in [
            "aws_secretsmanager_secret_version.database_url",
            "aws_secretsmanager_secret_version.signing_secret",
            "aws_secretsmanager_secret_version.redis_url",
        ] {
            assert!(
                service_block.contains(dep),
                "aws_ecs_service.this must depend on {dep}, not just the secret \
                 container it references by ARN: {service_block}"
            );
        }
    }

    #[test]
    fn aws_ecs_service_waits_for_execution_role_policies_before_starting() {
        // The task definition's execution_role_arn only orders creation
        // after the ROLE itself, not the policies attached to it — without
        // an explicit depends_on, tasks could be scheduled before the
        // execution role can actually pull the image, ship logs, or inject
        // secrets, failing during resource initialization.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let service_block = content
            .split("resource \"aws_ecs_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare the ECS service");
        for dep in [
            "aws_iam_role_policy_attachment.execution_managed",
            "aws_iam_role_policy.execution_secrets",
        ] {
            assert!(
                service_block.contains(dep),
                "aws_ecs_service.this must depend on {dep}, not just the execution \
                 role itself: {service_block}"
            );
        }
    }

    #[test]
    fn aws_ecs_task_definitions_bootstrap_from_placeholder_and_ignore_drift() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        for family in ["app", "migrate"] {
            let block = content
                .split(&format!(
                    "resource \"aws_ecs_task_definition\" \"{family}\""
                ))
                .nth(1)
                .and_then(|rest| rest.split("\nresource").next())
                .unwrap_or_else(|| panic!("main.tf must declare the {family} task definition"));
            assert!(
                block.contains("var.bootstrap_image"),
                "{family} task must use the bootstrap image: {block}"
            );
            assert!(
                block.contains("ignore_changes = [container_definitions]"),
                "{family} task definition must ignore container_definitions drift once CI \
                 registers real revisions: {block}"
            );
        }
        let service_block = content
            .split("resource \"aws_ecs_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare the ECS service");
        assert!(
            service_block.contains("ignore_changes = [task_definition, desired_count]"),
            "the ECS service must ignore task_definition drift so CI-registered revisions \
             aren't reverted by a later `terraform apply`: {service_block}"
        );
    }

    #[test]
    fn aws_ecs_public_app_bootstrap_has_no_secrets_and_deploy_restores_them() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, true).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let app = content
            .split("resource \"aws_ecs_task_definition\" \"app\"")
            .nth(1)
            .and_then(|block| block.split("\nresource").next())
            .expect("main.tf must declare the app task definition");
        assert!(app.contains("secrets     = []"));

        let workflow = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(workflow.contains("--argjson SECRETS"));
        assert!(workflow.contains(".containerDefinitions[0].secrets = $SECRETS"));
    }

    #[test]
    fn aws_ecs_ignores_desired_count_drift_managed_by_autoscaling() {
        // Application Auto Scaling changes desired_count directly at
        // runtime — without ignoring it, a later `terraform apply` would
        // treat a live scaling decision as drift from var.desired_count
        // and forcibly reset capacity, either yanking it during a load
        // spike or adding unwanted tasks right after a scale-in.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let service_block = content
            .split("resource \"aws_ecs_service\" \"this\"")
            .nth(1)
            .expect("main.tf must declare the ECS service");
        assert!(
            service_block.contains("desired_count]"),
            "the ECS service must ignore desired_count drift once Application Auto Scaling \
             owns it: {service_block}"
        );
    }

    #[test]
    fn aws_ecs_app_bootstrap_container_actually_listens_on_the_alb_health_check_port() {
        // Unlike App Runner, the ALB target group's health check (port
        // 3000, path /health) is a PERMANENT Terraform-managed resource —
        // there's no separate "swap it back after cutover" step available,
        // so the bootstrap container must satisfy it directly. The public
        // placeholder (nginx) doesn't do this out of the box; the "app"
        // container must override its entrypoint/command to configure it.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let app_block = content
            .split("resource \"aws_ecs_task_definition\" \"app\"")
            .nth(1)
            .and_then(|rest| rest.split("\nresource").next())
            .expect("main.tf must declare the app task definition");
        assert!(
            app_block.contains("entryPoint"),
            "the app container must override entryPoint so it runs regardless of the \
             placeholder image's own default command: {app_block}"
        );
        assert!(
            app_block.contains("listen 3000"),
            "the app container's bootstrap command must configure the placeholder to listen \
             on port 3000, matching the ALB target group: {app_block}"
        );
        assert!(
            app_block.contains("/health"),
            "the app container's bootstrap command must serve the same /health path the \
             target group checks: {app_block}"
        );
        // The "migrate" task's command is already overridden to run `autumn
        // migrate` and is never actually invoked against the bootstrap
        // image in practice (CI always registers a real-image revision
        // first) — it doesn't need the nginx trick.
        let migrate_block = content
            .split("resource \"aws_ecs_task_definition\" \"migrate\"")
            .nth(1)
            .and_then(|rest| rest.split("\nresource").next())
            .expect("main.tf must declare the migrate task definition");
        assert!(migrate_block.contains("autumn migrate"), "{migrate_block}");
    }

    #[test]
    fn aws_ecs_main_tf_derives_database_url_not_a_variable() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let main_tf = fs::read_to_string(dir.join("main.tf")).unwrap();
        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        assert!(
            main_tf.contains("AUTUMN_DATABASE__PRIMARY_URL"),
            "{main_tf}"
        );
        assert!(
            main_tf.contains("aws_db_instance.this.address"),
            "{main_tf}"
        );
        assert!(
            !variables_tf.contains("variable \"database_url\""),
            "there must be no database_url variable: {variables_tf}"
        );
    }

    #[test]
    fn aws_ecs_trusted_hosts_uses_the_required_domain_name_variable() {
        // Unlike App Runner (whose subdomain is only known after the
        // service is created), ECS's ALB serves under the operator-supplied
        // domain_name, known before the apply even starts — so trusted
        // hosts can be set correctly on the very first apply.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("AUTUMN_SECURITY__TRUSTED_HOSTS__HOSTS")
                && content.contains("var.domain_name"),
            "{content}"
        );
    }

    #[test]
    fn aws_ecs_execution_role_secrets_policy_is_scoped_not_wildcard() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let policy_block = content
            .split("data \"aws_iam_policy_document\" \"execution_secrets\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}\n").next())
            .expect("main.tf must declare the execution role's secrets policy document");
        assert!(
            !policy_block.contains("\"*\""),
            "the execution role's secrets access must be scoped, never a wildcard: {policy_block}"
        );
        assert!(
            policy_block.contains("local.secrets_manager_arns"),
            "{policy_block}"
        );
    }

    #[test]
    fn aws_ecs_task_role_and_execution_role_are_distinct() {
        // Least privilege: the execution role (ECS agent — image pull, logs,
        // secrets injection) must be a different principal from the task
        // role (the running app container's own AWS permissions).
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("resource \"aws_iam_role\" \"execution\""),
            "{content}"
        );
        assert!(
            content.contains("resource \"aws_iam_role\" \"task\""),
            "{content}"
        );
    }

    #[test]
    fn aws_ecs_redis_is_off_by_default_and_gated() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        let main_tf = fs::read_to_string(dir.join("main.tf")).unwrap();
        let redis_var_block = variables_tf
            .split("variable \"enable_redis_cache\"")
            .nth(1)
            .and_then(|rest| rest.split('}').next())
            .expect("variables.tf must declare variable \"enable_redis_cache\"");
        assert!(
            redis_var_block.contains("default     = false"),
            "enable_redis_cache must default to false: {redis_var_block}"
        );
        assert!(
            main_tf.contains("count                       = var.enable_redis_cache ? 1 : 0")
                || main_tf.contains("count = var.enable_redis_cache ? 1 : 0"),
            "ElastiCache resources must be gated behind enable_redis_cache: {main_tf}"
        );
        assert!(
            main_tf.contains("autumn-cache-redis"),
            "main.tf must document that Redis wiring alone isn't enough — the app must \
             also depend on autumn-cache-redis: {main_tf}"
        );
    }

    #[test]
    fn aws_ecs_scale_defaults_match_the_issue() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("variables.tf")).unwrap();
        let defaults = [
            ("desired_count", "2"),
            ("min_count", "1"),
            ("max_count", "10"),
        ];
        for (var, expected) in defaults {
            let block = content
                .split(&format!("variable \"{var}\""))
                .nth(1)
                .and_then(|rest| rest.split('}').next())
                .unwrap_or_else(|| {
                    panic!("variables.tf must declare variable \"{var}\": {content}")
                });
            assert!(
                block.contains(&format!("default     = {expected}")),
                "{var} must default to {expected}: {block}"
            );
        }
    }

    #[test]
    fn aws_ecs_secrets_have_no_default_and_are_sensitive() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("variables.tf")).unwrap();
        for var in ["database_admin_password", "signing_secret"] {
            let block = content
                .split(&format!("variable \"{var}\""))
                .nth(1)
                .and_then(|rest| rest.split('}').next())
                .unwrap_or_else(|| {
                    panic!("variables.tf must declare variable \"{var}\": {content}")
                });
            assert!(
                block.contains("sensitive   = true") || block.contains("sensitive = true"),
                "{block}"
            );
            assert!(
                !block.contains("default"),
                "{var} must not have a default: {block}"
            );
        }
    }

    #[test]
    fn aws_ecs_no_committed_secret_literals() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        for name in ["main.tf", "variables.tf", "terraform.tfvars.example"] {
            let content = fs::read_to_string(dir.join(name)).unwrap();
            assert!(
                !content.to_lowercase().contains("password =\""),
                "{name} must not contain a literal password assignment: {content}"
            );
        }
    }

    #[test]
    fn aws_ecs_target_adds_terraform_gitignore_entries() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".gitignore")).unwrap();
        for line in TERRAFORM_GITIGNORE_ENTRIES {
            assert!(content.lines().any(|l| l.trim() == *line), "{content}");
        }
    }

    #[test]
    fn aws_ecs_dockerignore_excludes_terraform_state() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        for pattern in [".terraform/", "*.tfstate", "terraform.tfvars"] {
            assert!(content.contains(pattern), "{content}");
        }
    }

    #[test]
    fn aws_ecs_outputs_tf_declares_expected_outputs_with_no_unsubstituted_placeholders() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        for name in [
            "region",
            "app_url",
            "alb_dns_name",
            "ecr_repository_url",
            "ecs_cluster_name",
            "ecs_service_name",
            "app_task_family",
            "migrate_task_family",
            "private_subnet_ids",
            "ecs_tasks_security_group_id",
            "execution_role_arn",
            "task_role_arn",
        ] {
            assert!(
                content.contains(&format!("output \"{name}\"")),
                "outputs.tf must declare output \"{name}\" — aws-deploy.yml's header \
                 comment tells operators to source CI config/IAM grants from it: {content}"
            );
        }
        assert!(
            !content.contains("{{"),
            "outputs.tf must not contain any unsubstituted template placeholders: {content}"
        );
    }

    #[test]
    fn aws_ecs_region_output_reflects_the_configured_region_variable() {
        // Both the deploy walkthrough's exported AWS_REGION env var and
        // aws-deploy.yml's documented AWS_REGION repo variable are meant to
        // be sourced from this output, so it must echo var.region, not a
        // hardcoded default.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        assert!(content.contains("value       = var.region"), "{content}");
    }

    #[test]
    fn aws_ecs_execution_and_task_role_arns_are_both_exported_for_ci_pass_role_grants() {
        // aws-deploy.yml's header comment tells operators to grant the CI
        // deploy role iam:PassRole on both roles' ARNs "terraform output
        // prints" — that promise is only true if both are actually
        // exported outputs, not just Terraform-internal resources.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let outputs_tf = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        assert!(
            outputs_tf.contains("aws_iam_role.execution.arn"),
            "{outputs_tf}"
        );
        assert!(outputs_tf.contains("aws_iam_role.task.arn"), "{outputs_tf}");
    }

    #[test]
    fn aws_ecs_gitignore_merge_is_idempotent_on_repeat_init() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::write(dir.join(".gitignore"), "/target\n.env\n").unwrap();

        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        init(&dir, "my-app", true, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".gitignore")).unwrap();
        assert!(
            content.contains("/target") && content.contains(".env"),
            "{content}"
        );
        for line in TERRAFORM_GITIGNORE_ENTRIES {
            let count = content.lines().filter(|l| l.trim() == *line).count();
            assert_eq!(count, 1, "`{line}` must appear exactly once: {content}");
        }
    }

    #[test]
    fn init_without_force_errors_if_aws_workflow_file_exists() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::create_dir_all(dir.join(".github/workflows")).unwrap();
        fs::write(dir.join(".github/workflows/aws-deploy.yml"), "existing").unwrap();
        let err = init(&dir, "my-app", false, Target::AwsEcs, false).unwrap_err();
        assert!(matches!(err, ReleaseError::FileExists(_)));
    }

    #[test]
    fn init_with_force_overwrites_nested_aws_workflow_file() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::create_dir_all(dir.join(".github/workflows")).unwrap();
        fs::write(dir.join(".github/workflows/aws-deploy.yml"), "old content").unwrap();
        init(&dir, "my-app", true, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert_ne!(content, "old content");
    }

    // ── aws-deploy.yml workflow ───────────────────────────────────────────────

    #[test]
    fn aws_workflow_triggers_on_tag_push_and_manual_dispatch() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(
            content.contains("tags:") && content.contains("\"v*\""),
            "{content}"
        );
        assert!(content.contains("workflow_dispatch"), "{content}");
    }

    #[test]
    fn aws_workflow_never_hardcodes_credentials() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(
            content.contains("secrets.AWS_ROLE_ARN"),
            "the workflow must source the deploy role from a repository secret: {content}"
        );
        assert!(
            content.contains("id-token: write"),
            "the workflow must request OIDC token permission for AWS login: {content}"
        );
        assert!(
            !content.to_lowercase().contains("aws_secret_access_key"),
            "the workflow must not use long-lived AWS access keys: {content}"
        );
    }

    #[test]
    fn aws_workflow_never_uses_the_invalid_output_none_value() {
        // The AWS CLI's --output only accepts json/yaml/yaml-stream/text/
        // table/off — "none" isn't a valid value and makes the command
        // exit non-zero instead of suppressing output. Discarding via
        // shell redirection works regardless of CLI version (older
        // versions may not support "off" either).
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(
            !content.contains("--output none"),
            "the workflow must not pass the invalid --output none value to the AWS CLI: {content}"
        );
    }

    #[test]
    fn aws_workflow_runs_migrations_before_updating_the_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let migrate_pos = content
            .find("Run database migrations")
            .expect("workflow must run migrations");
        let deploy_pos = content
            .find("Deploy new image to the ECS service")
            .expect("workflow must deploy to ECS");
        assert!(
            migrate_pos < deploy_pos,
            "migrations must run BEFORE the ECS service is updated: {content}"
        );
    }

    #[test]
    fn aws_workflow_aborts_deploy_on_migration_failure() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let migrate_step = content
            .split("Run database migrations")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("must find the migration step");
        assert!(
            migrate_step.contains("exit 1"),
            "the migration step must exit non-zero on failure so the job (and therefore \
             the deploy step after it) aborts: {migrate_step}"
        );
    }

    #[test]
    fn aws_workflow_serializes_overlapping_runs() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(content.contains("concurrency:"), "{content}");
        assert!(content.contains("cancel-in-progress: false"), "{content}");
    }

    #[test]
    fn aws_workflow_guards_against_superseded_run_before_migrating() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let guard_pos = content
            .find("Abort if a newer run of this workflow exists")
            .expect("workflow must guard against superseded runs");
        let migrate_pos = content.find("Run database migrations").unwrap();
        assert!(
            guard_pos < migrate_pos,
            "the staleness guard must run BEFORE migrating: {content}"
        );
    }

    #[test]
    fn aws_workflow_image_tag_is_unique_per_execution() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(content.contains("GITHUB_RUN_ID"), "{content}");
        assert!(content.contains("GITHUB_RUN_ATTEMPT"), "{content}");
    }

    #[test]
    fn aws_workflow_sanitizes_ref_name_for_docker_tag() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(
            content.contains("tr -c 'A-Za-z0-9_.-' '-'"),
            "the workflow must map every character outside Docker's tag charset to '-': {content}"
        );
    }

    #[test]
    fn aws_workflow_image_tag_never_starts_with_invalid_character() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(
            content.contains("sed -E 's/^[.-]+//'"),
            "the workflow must strip a leading '.'/'-' left over from ref sanitization — \
             Docker's tag grammar requires the first character to be a word character: {content}"
        );
    }

    #[test]
    fn aws_workflow_grants_actions_read_for_the_staleness_guard() {
        // Declaring `permissions:` at all switches GITHUB_TOKEN from its
        // default repo scopes to exactly the listed allow-list. The
        // "Abort if a newer run" step calls the Actions API via `gh api`,
        // which needs `actions: read` — without it that step 403s on every
        // single run, after the image has already built and pushed.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let permissions_block = content
            .split("permissions:")
            .nth(1)
            .and_then(|rest| rest.split("\n\n").next())
            .expect("workflow must declare a permissions block");
        assert!(
            permissions_block.contains("actions: read"),
            "the permissions block must grant actions: read for the staleness-guard \
             step's `gh api .../actions/workflows/.../runs` call: {permissions_block}"
        );
    }

    #[test]
    fn aws_workflow_documents_describe_task_definition_permission() {
        // The task-definition re-registration steps call
        // `aws ecs describe-task-definition` before `register-task-definition`
        // — a distinct IAM action the documented CI role policy must list,
        // or the very first deploy 403s before ever reaching migration.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let header = content
            .split("\nname: aws-deploy")
            .next()
            .expect("workflow must have a header comment before `name:`");
        assert!(
            header.contains("DescribeTaskDefinition"),
            "the header's documented IAM policy must include ecs:DescribeTaskDefinition: {header}"
        );
        assert!(
            !header.contains("granted ecr:*"),
            "the header must not recommend granting account-wide ecr:* — scope ECR access \
             to the repository ARN instead: {header}"
        );
    }

    #[test]
    fn aws_workflow_subnet_ids_env_is_used_as_json_directly_not_reparsed_from_csv() {
        // ECS_PRIVATE_SUBNET_IDS is documented as the compact JSON array
        // `terraform output -json private_subnet_ids | jq -c .` prints. The
        // migration step must consume it as JSON directly (a bare
        // `tr ',' '\n' | jq -R . | jq -s -c .` reconstruction would mangle
        // a real JSON array like ["subnet-1","subnet-2"] into garbage).
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(
            content.contains("SUBNETS=$(echo \"$ECS_PRIVATE_SUBNET_IDS\" | jq -c .)"),
            "the migration step must treat ECS_PRIVATE_SUBNET_IDS as JSON already, not \
             reconstruct it from a comma-separated split: {content}"
        );
        assert!(
            !content.contains("tr ',' '\\n' | jq -R ."),
            "the migration step must not reconstruct a JSON array via comma-split: {content}"
        );
    }

    #[test]
    fn aws_workflow_passes_git_provenance_build_args_to_docker() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        for arg in [
            "AUTUMN_BUILD_GIT_SHA",
            "AUTUMN_BUILD_GIT_SHA_SHORT",
            "AUTUMN_BUILD_GIT_BRANCH",
            "AUTUMN_BUILD_GIT_DIRTY",
            "AUTUMN_BUILD_TIMESTAMP",
        ] {
            assert!(content.contains(arg), "{content}");
        }
    }

    #[test]
    fn aws_workflow_registers_new_task_definitions_before_running_or_deploying() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let register_app_pos = content
            .find("register_app")
            .expect("must register the app task def");
        let register_migrate_pos = content
            .find("register_migrate")
            .expect("must register the migrate task def");
        let migrate_pos = content.find("Run database migrations").unwrap();
        let deploy_pos = content.find("Deploy new image to the ECS service").unwrap();
        assert!(register_migrate_pos < migrate_pos, "{content}");
        assert!(register_app_pos < deploy_pos, "{content}");
    }

    #[test]
    fn aws_workflow_strips_bootstrap_entrypoint_only_from_the_app_registration() {
        // Terraform's bootstrap "app" task definition overrides
        // entryPoint/command to make the placeholder nginx image satisfy
        // the ALB health check (main.tf) — describe-task-definition would
        // otherwise carry that override forward onto the REAL image, which
        // has no nginx and runs as an unprivileged user, so the container
        // would exit immediately instead of falling through to its own
        // Dockerfile ENTRYPOINT/CMD. The "migrate" family's own `command`
        // (autumn migrate) is intentional and permanent, so its
        // registration step must NOT strip it.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let app_step = content
            .split("Register a new \"app\" task definition revision")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("must find the app registration step");
        assert!(
            app_step.contains(
                "del(.containerDefinitions[0].entryPoint, .containerDefinitions[0].command)"
            ),
            "the app registration step must strip the bootstrap entryPoint/command before \
             registering the real image: {app_step}"
        );
        let migrate_step = content
            .split("Register a new \"migrate\" task definition revision")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("must find the migrate registration step");
        assert!(
            !migrate_step.contains("del(.containerDefinitions[0].entryPoint"),
            "the migrate registration step must NOT strip its own (intentional, permanent) \
             command: {migrate_step}"
        );
    }

    #[test]
    fn aws_workflow_waits_for_service_stability_after_deploying() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        assert!(
            content.contains("aws ecs wait services-stable"),
            "the workflow must wait for the new deployment to stabilize (so the circuit \
             breaker's rollback has a chance to be observed) rather than exiting immediately \
             after force-new-deployment: {content}"
        );
    }

    #[test]
    fn aws_workflow_detects_circuit_breaker_rollback_after_stabilizing() {
        // `services-stable`'s predicate only requires ONE deployment to
        // have runningCount == desiredCount — if the new revision failed
        // its health checks, the circuit breaker rolls the service back to
        // the PREVIOUS revision, and that older deployment satisfies the
        // waiter just as well. Without an explicit check, this step would
        // report success even though the requested task definition was
        // never actually running.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let deploy_step = content
            .split("Deploy new image to the ECS service")
            .nth(1)
            .and_then(|rest| rest.split("env:").next())
            .expect("must find the deploy step");
        assert!(
            deploy_step.contains("deployments[?status=='PRIMARY']")
                || deploy_step.contains("deployments[?status==`PRIMARY`]"),
            "the deploy step must inspect the PRIMARY deployment's task definition after \
             waiting: {deploy_step}"
        );
        assert!(
            deploy_step.contains("DEPLOYED_TASK_DEF") && deploy_step.contains("APP_TASK_DEF_ARN"),
            "the deploy step must compare the deployed task definition against the requested \
             one and fail if they differ: {deploy_step}"
        );
    }

    #[test]
    fn aws_workflow_stops_migration_task_on_timeout() {
        // ECS tasks have no runtime limit of their own; without an
        // explicit stop-task call, a migration that exceeds its polling
        // budget keeps running in the background after this job gives up
        // and releases the concurrency group — letting a later deploy
        // start a second migration while the timed-out one is still
        // mutating the schema.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let migrate_step = content
            .split("Run database migrations")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("must find the migration step");
        let timeout_block = migrate_step
            .split("did not finish within the time budget")
            .nth(1)
            .expect("must find the timeout-budget-exceeded branch");
        assert!(
            timeout_block.contains("aws ecs stop-task"),
            "the timeout branch must explicitly stop the migration task, not just exit: \
             {timeout_block}"
        );
        assert!(
            timeout_block.contains("wait tasks-stopped"),
            "the timeout branch must wait for the stop to actually take effect before this \
             job (and its concurrency group) releases: {timeout_block}"
        );
    }

    #[test]
    fn aws_workflow_documents_stop_task_permission() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::AwsEcs, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/aws-deploy.yml")).unwrap();
        let header = content
            .split("\nname: aws-deploy")
            .next()
            .expect("workflow must have a header comment before `name:`");
        assert!(
            header.contains("StopTask"),
            "the header's documented IAM policy must include ecs:StopTask, used to kill a \
             timed-out migration task: {header}"
        );
    }

    // ── aws-ecs workflow discoverability (git root vs. workspace member) ──────

    #[test]
    fn aws_ecs_workflow_relocation_warning_flags_a_nested_workspace_member() {
        let tmp = TempDir::new().unwrap();
        let git_root = tmp.path().to_path_buf();
        fs::create_dir_all(git_root.join(".git")).unwrap();
        let member_dir = git_root.join("examples").join("blog");
        fs::create_dir_all(&member_dir).unwrap();

        let warning =
            nested_workflow_relocation_warning(&member_dir, ".github/workflows/aws-deploy.yml")
                .expect("a workflow nested under a workspace member must be flagged");
        assert!(warning.contains("aws-deploy.yml"), "{warning}");
        assert!(
            warning.contains("working-directory: examples/blog"),
            "{warning}"
        );
    }

    #[test]
    fn is_terraform_target_covers_all_terraform_targets() {
        assert!(is_terraform_target(Target::AzureContainerApps));
        assert!(is_terraform_target(Target::AwsAppRunner));
        assert!(is_terraform_target(Target::AwsEcs));
        assert!(is_terraform_target(Target::GcpCloudRun));
        assert!(!is_terraform_target(Target::Default));
        assert!(!is_terraform_target(Target::Fly));
        assert!(!is_terraform_target(Target::DockerCompose));
    }

    // ── --target=gcp-cloud-run ───────────────────────────────────────────────

    #[test]
    fn gcp_cloud_run_target_creates_all_expected_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        for name in [
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
            ".github/workflows/gcp-deploy.yml",
        ] {
            assert!(
                dir.join(name).is_file(),
                "{name} must be created for --target=gcp-cloud-run"
            );
        }
        // Base scaffolding is still emitted alongside the GCP-specific files.
        assert!(dir.join("Dockerfile").is_file());
        assert!(dir.join(".dockerignore").is_file());
        assert!(dir.join("autumn.production.toml.example").is_file());
    }

    #[test]
    fn gcp_cloud_run_target_returns_nested_workflow_path_in_created_list() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        let files = init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        assert!(
            files
                .iter()
                .any(|f| f == ".github/workflows/gcp-deploy.yml"),
            "created-files list must include the nested workflow path: {files:?}"
        );
    }

    #[test]
    fn default_target_does_not_create_gcp_files() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::Default, false).unwrap();
        for name in [
            "main.tf",
            "variables.tf",
            "outputs.tf",
            "terraform.tfvars.example",
            ".github/workflows/gcp-deploy.yml",
        ] {
            assert!(
                !dir.join(name).exists(),
                "{name} must NOT be created for the default target"
            );
        }
    }

    #[test]
    fn gcp_main_tf_has_artifact_registry_and_cloud_run_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_artifact_registry_repository"),
            "main.tf must provision an Artifact Registry repository: {content}"
        );
        assert!(
            content.contains("resource \"google_cloud_run_v2_service\""),
            "main.tf must provision the Cloud Run service: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_has_cloud_sql_with_private_ip_only() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_sql_database_instance"),
            "main.tf must provision a Cloud SQL PostgreSQL instance: {content}"
        );
        let ip_config = content
            .split("ip_configuration {")
            .nth(1)
            .and_then(|rest| rest.split('}').next())
            .expect("main.tf must declare an ip_configuration block");
        assert!(
            ip_config.contains("ipv4_enabled") && ip_config.contains("false"),
            "Cloud SQL must not have a public IPv4 address — private IP only via the \
             VPC connector: {ip_config}"
        );
        assert!(
            ip_config.contains("private_network"),
            "Cloud SQL's ip_configuration must set private_network: {ip_config}"
        );
    }

    #[test]
    fn gcp_main_tf_has_vpc_access_connector_wired_into_cloud_run() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_vpc_access_connector"),
            "main.tf must provision a Serverless VPC Access connector: {content}"
        );
        assert!(
            content.contains("google_vpc_access_connector.this.id"),
            "the Cloud Run service must reference the VPC connector: {content}"
        );
        assert!(
            content.contains("egress    = \"PRIVATE_RANGES_ONLY\"")
                || content.contains("egress = \"PRIVATE_RANGES_ONLY\""),
            "vpc_access egress should stay scoped to private ranges so general internet \
             egress doesn't route through the connector: {content}"
        );
    }

    #[test]
    fn gcp_vpc_connector_name_respects_the_weighted_21_char_limit() {
        // Serverless VPC Access connector names must stay under 21
        // characters where EACH HYPHEN COUNTS AS TWO characters toward that
        // limit (confirmed against Google's own docs: "must be less than
        // 21 characters long, and ... hyphens (-) count as two
        // characters") — an undocumented-in-the-Terraform-provider quirk
        // distinct from the usual RFC 1035 label rule every other resource
        // here follows. A 10-letter, hyphen-free base ("abcdefghij") plus
        // the fixed "-connector" suffix already sits at the weighted
        // boundary (10 + 2 + 9 = 21, i.e. rejected), so a naive raw
        // character cap (this scaffold's previous 15-character cap) is far
        // too permissive, and any cap that doesn't first strip hyphens
        // from the base can blow the budget on hyphens alone.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains(
                "connector_name_safe = substr(replace(local.app_name_hyphenated, \"-\", \"\"), 0, 9)"
            ),
            "connector_name_safe must strip hyphens from the base before capping it (so \
             weighted length equals raw length), and cap the hyphen-free base at 9 \
             characters — 9 (base) + 11 (the \"-connector\" suffix's weighted length: \
             2 for its hyphen + 9 for \"connector\") = 20, just under the 21-character \
             weighted limit: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_vpc_network_waits_for_compute_api_activation() {
        // Nothing in google_compute_network.this references an attribute of
        // google_project_service.apis, so without an explicit depends_on,
        // Terraform has no reason to order network creation after API
        // enablement — on a fresh project it can race the enablement and
        // fail the first `terraform apply`.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let network_block = content
            .split("resource \"google_compute_network\" \"this\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the google_compute_network resource");
        assert!(
            network_block.contains("depends_on")
                && network_block.contains("google_project_service.apis"),
            "google_compute_network.this must depend on google_project_service.apis: \
             {network_block}"
        );
    }

    #[test]
    fn gcp_redis_instance_name_stays_under_memorystores_40_char_limit() {
        // app_name_safe alone is already capped at 40 (Memorystore's own
        // limit) — appending the fixed "-redis" suffix on top of it would
        // overflow the limit for any sanitized name longer than 34
        // characters, exactly the class of bug the VPC connector's own
        // shorter budget already guards against.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("redis_name_safe = trim(substr(local.app_name_hyphenated, 0, 34)"),
            "main.tf must derive a length-bounded local for the Redis instance name, \
             capped so the fixed \"-redis\" suffix still fits under Memorystore's \
             40-character limit: {content}"
        );
        assert!(
            content.contains("\"${local.redis_name_safe}-redis\""),
            "the Redis instance must use the length-bounded local, not app_name_safe \
             directly: {content}"
        );
    }

    #[test]
    fn gcp_service_account_waits_for_iam_api_activation() {
        // Nothing in google_service_account.cloud_run references an
        // attribute of google_project_service.apis, so without an explicit
        // depends_on, Terraform has no reason to order it after the IAM API
        // is enabled — on a fresh project this fails with SERVICE_DISABLED.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let sa_block = content
            .split("resource \"google_service_account\" \"cloud_run\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the google_service_account resource");
        assert!(
            sa_block.contains("depends_on") && sa_block.contains("google_project_service.apis"),
            "google_service_account.cloud_run must depend on \
             google_project_service.apis: {sa_block}"
        );
    }

    #[test]
    fn gcp_main_tf_postgres_database_name_is_length_bounded() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let raw_local_line = content
            .lines()
            .find(|l| l.trim_start().starts_with("postgres_database_name_raw"))
            .expect("main.tf must declare a postgres_database_name_raw local");
        assert!(
            raw_local_line.contains("substr(") && raw_local_line.contains(", 63)"),
            "the Postgres database name must be truncated to 63 characters: {raw_local_line}"
        );
    }

    #[test]
    fn gcp_main_tf_postgres_database_name_avoids_reserved_names() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        for reserved in ["postgres", "cloudsqladmin", "template0", "template1"] {
            assert!(
                content.contains(&format!("\"{reserved}\"")),
                "the reserved-name guard must list {reserved:?}: {content}"
            );
        }
        let database_name_local = content
            .lines()
            .skip_while(|l| !l.trim_start().starts_with("postgres_database_name ="))
            .take_while(|l| !l.trim_start().starts_with('}'))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(
            database_name_local.contains("contains(") && database_name_local.contains("_prod"),
            "postgres_database_name must fall back to a suffixed name when the \
             sanitized value collides with a reserved database name: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_has_secret_manager_with_database_and_signing_secrets() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_secret_manager_secret"),
            "main.tf must provision Secret Manager secrets: {content}"
        );
        assert!(
            content.contains("AUTUMN_DATABASE__PRIMARY_URL"),
            "main.tf must wire the primary DB URL env var from Secret Manager: {content}"
        );
        assert!(
            content.contains("AUTUMN_SECURITY__SIGNING_SECRET"),
            "main.tf must wire the signing secret env var from Secret Manager: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_secret_refs_pin_the_created_version_not_latest() {
        // Cloud Run resolves a secret_key_ref's version once, when a NEW
        // revision is created — an already-running revision's already-
        // running instances never re-read "latest". If the env block kept
        // the literal string "latest", rotating database_admin_password/
        // signing_secret via Terraform (which creates a new
        // google_secret_manager_secret_version) wouldn't change anything
        // Terraform sees as a diff in the Cloud Run service/job spec, so no
        // new revision would roll out at all — leaving the whole fleet
        // pinned to the OLD secret value while Cloud SQL had already
        // started requiring the NEW password. Referencing the version
        // resource's own `.version` attribute means a rotation IS a diff,
        // and rolls out as a new revision.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            !content.contains("version = \"latest\""),
            "no secret_key_ref may pin to the \"latest\" alias — Cloud Run only \
             resolves it once per revision, so a Terraform-driven secret rotation \
             would silently fail to roll out to already-running instances: {content}"
        );
        for version_ref in [
            "version = google_secret_manager_secret_version.database_url.version",
            "version = google_secret_manager_secret_version.signing_secret.version",
            "version = google_secret_manager_secret_version.redis_url[0].version",
        ] {
            assert!(
                content.contains(version_ref),
                "main.tf must pin every secret_key_ref to its version resource's \
                 `.version` attribute: expected to find `{version_ref}`: {content}"
            );
        }
    }

    #[test]
    fn gcp_database_url_secret_version_waits_for_sql_user_password_update() {
        // Unlike AWS RDS (master password lives on the DB instance
        // resource itself, so referencing its .address here already
        // creates an implicit ordering edge), Cloud SQL splits the
        // instance and its user credentials into two independent
        // resources: google_sql_database_instance.this (referenced for
        // the private IP) and google_sql_user.this (which actually owns
        // `password`). Without an explicit dependency, rotating
        // database_admin_password gives Terraform no reason to apply the
        // google_sql_user.this password update before this secret
        // version — the Cloud Run revision this secret version triggers
        // (via the pinned .version) could then start using the NEW
        // password before Cloud SQL has actually accepted it.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let secret_version_block = content
            .split("resource \"google_secret_manager_secret_version\" \"database_url\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the database_url secret version resource");
        assert!(
            secret_version_block.contains("depends_on")
                && secret_version_block.contains("google_sql_user.this"),
            "google_secret_manager_secret_version.database_url must depend on \
             google_sql_user.this so a password rotation updates Cloud SQL before the \
             secret version (and the Cloud Run revision it triggers) picks up the new \
             value: {secret_version_block}"
        );
    }

    #[test]
    fn gcp_main_tf_secret_access_is_scoped_per_secret_not_project_wide() {
        // A project-wide `roles/secretmanager.secretAccessor` grant (via
        // google_project_iam_member) would let a compromised container read
        // every secret in the project — the grant must instead be scoped to
        // exactly the secrets this app uses via
        // google_secret_manager_secret_iam_member.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_secret_manager_secret_iam_member"),
            "secret access must be granted per-secret via \
             google_secret_manager_secret_iam_member: {content}"
        );
        assert!(
            !content.contains("google_project_iam_member\" \"secret"),
            "must not grant a project-wide secretAccessor role: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_wires_trusted_hosts_so_prod_actually_binds() {
        // AUTUMN_PROFILE=prod makes fail_fast_on_invalid_trusted_hosts exit
        // the process immediately when security.trusted_hosts.hosts is
        // empty (see docs/guide/deployment.md's "Trusted hosts" section).
        // Without this, the container never binds after the first real
        // deploy — it would crash-loop instead of serving traffic.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("AUTUMN_SECURITY__TRUSTED_HOSTS__HOSTS"),
            "main.tf must set AUTUMN_SECURITY__TRUSTED_HOSTS__HOSTS on the Cloud Run \
             service: {content}"
        );
        assert!(
            content.contains("local.service_url_host"),
            "the trusted host must be derived from local.service_url_host (known at \
             plan time from the project number), not require a second apply: {content}"
        );
        assert!(
            content.contains("data.google_project.this.number"),
            "service_url_host must be derived from the project NUMBER (Cloud Run's \
             default URL format), not the project ID: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_grants_public_invoker_access() {
        // Cloud Run services default to requiring IAM-authenticated
        // invocations — without an explicit allUsers invoker grant, the
        // deployed app would 403 every request from a browser.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_cloud_run_v2_service_iam_member"),
            "main.tf must grant an IAM invoker binding on the Cloud Run service: {content}"
        );
        assert!(
            content.contains("roles/run.invoker") && content.contains("allUsers"),
            "main.tf must grant roles/run.invoker to allUsers for public ingress: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_service_account_scoped_to_cloudsql_client_not_broader() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_service_account"),
            "main.tf must provision a dedicated runtime service account: {content}"
        );
        assert!(
            content.contains("roles/cloudsql.client"),
            "the runtime service account must be granted roles/cloudsql.client: {content}"
        );
        assert!(
            !content.contains("roles/editor") && !content.contains("roles/owner"),
            "the runtime service account must never be granted a broad primitive role: \
             {content}"
        );
    }

    #[test]
    fn gcp_main_tf_has_one_shot_migration_job() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_cloud_run_v2_job") && content.contains("\"migrate\""),
            "main.tf must provision a one-shot Cloud Run Job for migrations: {content}"
        );
        assert!(
            content.contains("autumn migrate"),
            "the migration job must run `autumn migrate`: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_redis_api_is_gated_behind_enable_redis_cache() {
        // Every core resource (VPC, Artifact Registry, service account,
        // Secret Manager) depends on the ENTIRE google_project_service.apis
        // for_each set — unconditionally including redis.googleapis.com
        // would mean a project/org policy that merely disallows Redis
        // (even with enable_redis_cache left false, Redis never requested)
        // fails the whole apply, not just the optional Redis piece.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("var.enable_redis_cache ? [\"redis.googleapis.com\"] : []"),
            "redis.googleapis.com must only be added to required_apis when \
             enable_redis_cache is true: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_psa_range_is_explicit_and_wont_collide_with_connector_cidr() {
        // Both the PSA global address and the VPC connector are
        // independently scheduled after network creation; if Google
        // auto-picked the PSA /16 and it happened to contain the
        // connector's CIDR, one of the two creations would fail with an
        // overlap error. An explicit, documented address removes the
        // ambiguity entirely.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let psa_block = content
            .split("resource \"google_compute_global_address\" \"private_service_access\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the private_service_access resource");
        assert!(
            psa_block.contains("address       = \"10.100.0.0\""),
            "the PSA range must be pinned to an explicit address, not left for \
             Google to auto-allocate: {psa_block}"
        );
        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        let default_line = variables_tf
            .lines()
            .skip_while(|l| !l.contains("variable \"vpc_connector_cidr\""))
            .find(|l| l.trim_start().starts_with("default"))
            .expect("vpc_connector_cidr must declare a default");
        assert!(
            default_line.contains("10.8.0.0/28"),
            "the default connector CIDR must not have silently changed to \
             something that could overlap the pinned 10.100.0.0/16 PSA range: \
             {default_line}"
        );
    }

    #[test]
    fn gcp_main_tf_redis_is_off_by_default_and_gated() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let main_tf = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            main_tf.contains("google_redis_instance"),
            "main.tf must provision an optional Memorystore Redis instance: {main_tf}"
        );
        assert!(
            main_tf.contains("var.enable_redis_cache ? 1 : 0"),
            "the Redis instance must be gated behind enable_redis_cache via count: {main_tf}"
        );

        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        let default_line = variables_tf
            .lines()
            .skip_while(|l| !l.contains("variable \"enable_redis_cache\""))
            .find(|l| l.trim_start().starts_with("default"))
            .expect("enable_redis_cache must declare a default");
        assert!(
            default_line.contains("false"),
            "enable_redis_cache must default to false: {default_line}"
        );
    }

    #[test]
    fn gcp_main_tf_wires_redis_env_vars_into_cloud_run_service() {
        // Provisioning the Redis instance alone does nothing — Autumn's
        // cache subsystem only activates it via these two env vars. A
        // regression dropping the `dynamic "env"` blocks would silently
        // leave the (paid) Memorystore instance unused while every other
        // "redis is gated" assertion still passes.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("AUTUMN_CACHE__BACKEND"),
            "main.tf must wire AUTUMN_CACHE__BACKEND into the Cloud Run service when \
             Redis is enabled: {content}"
        );
        assert!(
            content.contains("AUTUMN_CACHE__REDIS__URL")
                && content.contains("google_secret_manager_secret.redis_url[0].secret_id"),
            "main.tf must wire AUTUMN_CACHE__REDIS__URL from the redis_url secret: {content}"
        );
    }

    #[test]
    fn gcp_main_tf_has_private_services_access_peering_for_cloud_sql() {
        // Cloud SQL's (and Redis's) private IP depends entirely on this
        // peering; ip_configuration alone doesn't provision it.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_compute_global_address") && content.contains("VPC_PEERING"),
            "main.tf must reserve a VPC peering range for private services access: \
             {content}"
        );
        assert!(
            content.contains("google_service_networking_connection"),
            "main.tf must establish the private services access peering connection: \
             {content}"
        );
    }

    #[test]
    fn gcp_main_tf_enables_required_apis() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("google_project_service"),
            "main.tf must enable the GCP APIs this scaffold's resources depend on, so a \
             fresh project works without a manual `gcloud services enable` step: {content}"
        );
        for api in [
            "run.googleapis.com",
            "sqladmin.googleapis.com",
            "secretmanager.googleapis.com",
            "vpcaccess.googleapis.com",
            "artifactregistry.googleapis.com",
            "servicenetworking.googleapis.com",
            // google_compute_network / google_compute_global_address are
            // Compute Engine resources — a fresh project without this API
            // pre-enabled would fail the very first apply at VPC creation.
            "compute.googleapis.com",
            // google_service_account.cloud_run fails with SERVICE_DISABLED
            // on a fresh project without this API pre-enabled.
            "iam.googleapis.com",
            // data.google_project.this reads project metadata through the
            // Cloud Resource Manager API — a fresh project without this API
            // pre-enabled fails at plan time with SERVICE_DISABLED.
            "cloudresourcemanager.googleapis.com",
        ] {
            assert!(
                content.contains(api),
                "main.tf must enable {api}: {content}"
            );
        }
    }

    #[test]
    fn gcp_main_tf_project_data_source_waits_for_resourcemanager_api_activation() {
        // data.google_project.this is a data source, which Terraform will
        // otherwise try to read during the initial refresh/plan phase —
        // before google_project_service.apis has run — on a fresh project
        // where cloudresourcemanager.googleapis.com isn't enabled yet, that
        // read fails with SERVICE_DISABLED before any resource here even
        // starts creating. depends_on defers a data source's read until
        // after its dependencies have applied.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let project_data_block = content
            .split("data \"google_project\" \"this\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the google_project data source");
        assert!(
            project_data_block.contains("depends_on")
                && project_data_block.contains("google_project_service.apis"),
            "data.google_project.this must depend on google_project_service.apis: \
             {project_data_block}"
        );
    }

    #[test]
    fn gcp_main_tf_redis_avoids_a_tls_mode_the_client_cant_verify() {
        // Memorystore's SERVER_AUTHENTICATION mode presents a private,
        // instance-specific CA — not a publicly-trusted one — and
        // autumn-cache-redis's RedisCache::connect has no hook to trust a
        // custom CA. Unlike AWS ElastiCache/Azure Redis Cache (both use
        // publicly-trusted certs and work with the same client), enabling
        // that mode here would generate a rediss:// URL the app can never
        // actually connect with. Traffic stays inside the private VPC
        // regardless, so AUTH-only (no transit encryption) is correct here.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            content.contains("transit_encryption_mode = \"DISABLED\""),
            "google_redis_instance must not claim SERVER_AUTHENTICATION — the client \
             can't verify Memorystore's private CA: {content}"
        );
        assert!(
            content.contains("auth_enabled            = true"),
            "google_redis_instance must still require AUTH: {content}"
        );
        let redis_url_version = content
            .split("resource \"google_secret_manager_secret_version\" \"redis_url\"")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("main.tf must declare the redis_url secret version");
        assert!(
            redis_url_version.contains("\"redis://:"),
            "the derived redis_url secret must use the redis:// scheme, matching \
             transit_encryption_mode = DISABLED, not rediss://: {redis_url_version}"
        );
    }

    #[test]
    fn gcp_scale_defaults_match_the_issue() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        for (var_name, expected) in [("min_instances", "1"), ("max_instances", "10")] {
            let default_line = variables_tf
                .lines()
                .skip_while(|l| !l.contains(&format!("variable \"{var_name}\"")))
                .find(|l| l.trim_start().starts_with("default"))
                .unwrap_or_else(|| panic!("{var_name} must declare a default"));
            assert!(
                default_line.contains(expected),
                "{var_name} must default to {expected}: {default_line}"
            );
        }

        let main_tf = fs::read_to_string(dir.join("main.tf")).unwrap();
        assert!(
            main_tf.contains("max_instance_request_concurrency = 80"),
            "main.tf must set concurrency to 80 requests per instance (Cloud Run's own \
             default, made explicit and tunable): {main_tf}"
        );
    }

    #[test]
    fn gcp_variables_tf_documents_db_tier_connection_budget_vs_max_instances() {
        // db-f1-micro's small max_connections ceiling (~25) combined with
        // pool_size=10 (autumn.production.toml.example) and the default
        // max_instances of 10 means scaling out under real load can exhaust
        // the tier's connection budget well before hitting max_instances —
        // not an apply-time failure, but a documented gotcha operators must
        // resize for before relying on autoscaling in production.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let variables_tf = fs::read_to_string(dir.join("variables.tf")).unwrap();
        assert!(
            variables_tf.contains("max_connections") && variables_tf.contains("pool_size"),
            "variables.tf must document the relationship between db_tier's \
             max_connections ceiling and max_instances * pool_size, so operators know \
             to resize db_tier before scaling out in production: {variables_tf}"
        );
    }

    #[test]
    fn gcp_main_tf_documents_always_allocated_cpu_option() {
        // Issue #1280 asks for a "CPU: always-allocated option commented
        // out for latency-sensitive workloads" — Cloud Run's cost-optimized
        // default only allocates CPU while a request is in flight
        // (cpu_idle = true); the always-allocated alternative must be
        // present, just not the active setting.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("main.tf")).unwrap();
        let resources_block = content
            .split("resources {")
            .nth(1)
            .and_then(|rest| rest.split("\n      }").next())
            .expect("the Cloud Run service container must declare a resources block");
        assert!(
            resources_block.contains("cpu_idle = true"),
            "cpu_idle must default to true (cost-optimized, scale-to-zero-friendly): \
             {resources_block}"
        );
        assert!(
            resources_block
                .lines()
                .any(|l| l.trim_start().starts_with("# cpu_idle = false")),
            "an always-allocated-CPU option (cpu_idle = false) must be present, \
             commented out, for latency-sensitive workloads: {resources_block}"
        );
    }

    #[test]
    fn gcp_secrets_have_no_default_and_are_sensitive() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("variables.tf")).unwrap();
        for var_name in ["database_admin_password", "signing_secret"] {
            let block = content
                .split(&format!("variable \"{var_name}\""))
                .nth(1)
                .and_then(|rest| rest.split('}').next())
                .unwrap_or_else(|| panic!("variables.tf must declare {var_name}"));
            assert!(
                block.contains("sensitive") && block.contains("true"),
                "{var_name} must be marked sensitive: {block}"
            );
            assert!(
                !block.contains("default"),
                "{var_name} must have no default value: {block}"
            );
        }
    }

    #[test]
    fn gcp_no_committed_secret_literals() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let tfvars = fs::read_to_string(dir.join("terraform.tfvars.example")).unwrap();
        // The documented placeholder ("# database_admin_password = (set via ...)") is a
        // commented-out line, matching the azure-container-apps/aws-* targets' identical
        // convention — only a non-comment assignment would be a real committed secret.
        for line in tfvars.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with('#') {
                continue;
            }
            assert!(
                !trimmed.starts_with("database_admin_password")
                    && !trimmed.starts_with("signing_secret"),
                "terraform.tfvars.example must not assign the secret variables directly \
                 on a non-comment line: {line:?} in {tfvars}"
            );
        }
        assert!(
            tfvars.contains("TF_VAR_database_admin_password")
                && tfvars.contains("TF_VAR_signing_secret"),
            "terraform.tfvars.example must document the TF_VAR_* env var pattern instead: \
             {tfvars}"
        );
    }

    #[test]
    fn gcp_target_adds_terraform_gitignore_entries() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let gitignore = fs::read_to_string(dir.join(".gitignore")).unwrap();
        for pattern in [
            ".terraform/",
            "*.tfstate",
            "*.tfstate.*",
            "terraform.tfvars",
        ] {
            assert!(
                gitignore.contains(pattern),
                ".gitignore must contain {pattern:?} for the gcp-cloud-run target: {gitignore}"
            );
        }
    }

    #[test]
    fn gcp_dockerignore_excludes_terraform_state() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".dockerignore")).unwrap();
        for pattern in [".terraform/", "*.tfstate", "terraform.tfvars"] {
            assert!(
                content.contains(pattern),
                ".dockerignore must exclude {pattern:?} so terraform.tfstate is never sent \
                 to the Docker build context: {content}"
            );
        }
    }

    #[test]
    fn gcp_outputs_tf_declares_expected_outputs_with_no_unsubstituted_placeholders() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-blog");
        init(&dir, "my-blog", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join("outputs.tf")).unwrap();
        for output in [
            "service_url",
            "service_name",
            "artifact_registry_repository_url",
            "migrate_job_name",
            "service_account_email",
            "project_id",
        ] {
            assert!(
                content.contains(&format!("output \"{output}\"")),
                "outputs.tf must declare output {output:?}: {content}"
            );
        }
        assert!(
            content.contains("my-blog"),
            "outputs.tf must substitute the project name: {content}"
        );
        assert!(
            !content.contains("{{"),
            "outputs.tf must not contain unsubstituted placeholders: {content}"
        );
    }

    #[test]
    fn init_without_force_errors_if_gcp_workflow_file_exists() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        fs::create_dir_all(dir.join(".github/workflows")).unwrap();
        fs::write(dir.join(".github/workflows/gcp-deploy.yml"), "existing").unwrap();
        let err = init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap_err();
        assert!(matches!(err, ReleaseError::FileExists(_)));
    }

    #[test]
    fn gcp_target_gitignore_merge_is_idempotent_on_repeat_init() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        init(&dir, "my-app", true, Target::GcpCloudRun, false).unwrap();
        let gitignore = fs::read_to_string(dir.join(".gitignore")).unwrap();
        let terraform_dir_count = gitignore
            .lines()
            .filter(|l| l.trim() == ".terraform/")
            .count();
        assert_eq!(
            terraform_dir_count, 1,
            "re-running init with --force must not duplicate gitignore entries: {gitignore}"
        );
    }

    // ── gcp-cloud-run workflow ────────────────────────────────────────────────

    #[test]
    fn gcp_workflow_triggers_on_tag_push_and_manual_dispatch() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("tags:"),
            "gcp-deploy.yml must trigger on tag push: {content}"
        );
        assert!(
            content.contains("workflow_dispatch:"),
            "gcp-deploy.yml must also support manual dispatch: {content}"
        );
    }

    #[test]
    fn gcp_workflow_authenticates_via_workload_identity_federation() {
        // No long-lived service account key: the workflow must use OIDC via
        // google-github-actions/auth with a workload identity provider, not
        // a downloaded JSON key.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("google-github-actions/auth@v2"),
            "gcp-deploy.yml must authenticate via google-github-actions/auth: {content}"
        );
        assert!(
            content.contains("workload_identity_provider"),
            "gcp-deploy.yml must use Workload Identity Federation, not a static key: {content}"
        );
        assert!(
            !content.to_lowercase().contains("credentials_json"),
            "gcp-deploy.yml must not authenticate via a downloaded service-account key: \
             {content}"
        );
    }

    #[test]
    fn gcp_workflow_selects_the_configured_target_project() {
        // WIF authentication alone doesn't set gcloud's active project — if
        // the deployer service account lives in a different project than
        // the one this scaffold's resources were created in (e.g. a shared
        // CI project), every gcloud command below would target the wrong
        // project (or fail to find the resources) without this.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        let auth_step = content
            .split("uses: google-github-actions/auth@v2")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("the auth step must be present");
        assert!(
            auth_step.contains("project_id: ${{ vars.GCP_PROJECT_ID }}"),
            "the auth step must pass project_id so gcloud targets the configured \
             project, not wherever the deployer service account happens to live: \
             {auth_step}"
        );
    }

    #[test]
    fn gcp_workflow_sets_up_gcloud_and_configures_docker_before_building() {
        // ubuntu-latest doesn't ship gcloud, and `docker push` to Artifact
        // Registry needs a configured credential helper — without both,
        // every build/push step in this workflow fails.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        let auth_pos = content
            .find("google-github-actions/auth@v2")
            .expect("auth step must be present");
        let setup_pos = content
            .find("google-github-actions/setup-gcloud@v2")
            .expect("gcp-deploy.yml must set up the gcloud CLI");
        let configure_docker_pos = content
            .find("run: gcloud auth configure-docker")
            .expect("gcp-deploy.yml must configure Docker for Artifact Registry");
        let build_pos = content
            .find("docker build \\")
            .expect("build step must be present");
        assert!(
            auth_pos < setup_pos
                && setup_pos < configure_docker_pos
                && configure_docker_pos < build_pos,
            "auth, then gcloud setup, then Docker configuration must all precede the \
             build step: {content}"
        );
    }

    #[test]
    fn gcp_workflow_builds_pushes_to_artifact_registry_and_deploys() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("docker build"),
            "gcp-deploy.yml must build the release image: {content}"
        );
        assert!(
            content.contains("docker push"),
            "gcp-deploy.yml must push to Artifact Registry: {content}"
        );
        assert!(
            content.contains("gcloud run services update"),
            "gcp-deploy.yml must deploy the new image to the Cloud Run service: {content}"
        );
    }

    #[test]
    fn gcp_workflow_passes_git_provenance_build_args_to_docker() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        for arg in [
            "AUTUMN_BUILD_GIT_SHA",
            "AUTUMN_BUILD_GIT_SHA_SHORT",
            "AUTUMN_BUILD_GIT_BRANCH",
            "AUTUMN_BUILD_GIT_DIRTY",
            "AUTUMN_BUILD_TIMESTAMP",
        ] {
            assert!(
                content.contains(&format!("--build-arg {arg}=")),
                "gcp-deploy.yml's docker build must pass --build-arg {arg}: {content}"
            );
        }
    }

    #[test]
    fn gcp_workflow_updates_migration_job_image_before_executing_it() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();

        let migration_step = content
            .split("Run database migrations")
            .nth(1)
            .and_then(|rest| rest.split("- name:").next())
            .expect("a 'Run database migrations' step must exist");

        let update_pos = migration_step
            .find("gcloud run jobs update")
            .expect("the migration job's image must be updated first");
        let execute_pos = migration_step
            .find("gcloud run jobs execute")
            .expect("`jobs execute` must follow to actually run the now-updated job");
        assert!(
            update_pos < execute_pos,
            "the job's image must be updated BEFORE it's executed: {migration_step}"
        );

        let update_block = &migration_step[update_pos..execute_pos];
        assert!(
            update_block.contains("--image"),
            "`jobs update` must be the one that carries --image: {update_block}"
        );

        let execute_block = &migration_step[execute_pos..];
        assert!(
            !execute_block.contains("--image"),
            "`jobs execute` must not carry --image: {execute_block}"
        );
        assert!(
            execute_block.contains("--wait"),
            "`jobs execute` must block until the execution finishes via --wait, so the \
             deploy step below never runs against an unmigrated schema: {execute_block}"
        );
    }

    #[test]
    fn gcp_workflow_never_hardcodes_credentials() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("secrets."),
            "gcp-deploy.yml must source credentials from GitHub Actions secrets, never \
             hardcode them: {content}"
        );
    }

    #[test]
    fn gcp_workflow_runs_migrations_before_updating_the_service() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        let migrate_pos = content
            .find("gcloud run jobs execute \"$GCP_MIGRATE_JOB_NAME\"")
            .expect("migration job execute must be present");
        let deploy_pos = content
            .find("gcloud run services update \"$GCP_SERVICE_NAME\"")
            .expect("deploy step must be present");
        assert!(
            migrate_pos < deploy_pos,
            "the migration job must run BEFORE the service is updated to the new image: \
             {content}"
        );
    }

    #[test]
    fn gcp_workflow_sanitizes_ref_name_for_docker_tag() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("tr -c 'A-Za-z0-9_.-' '-'"),
            "gcp-deploy.yml must map every character outside Docker's tag charset to \"-\": \
             {content}"
        );
        assert!(
            !content.contains(":${GITHUB_REF_NAME}") && !content.contains(":$GITHUB_REF_NAME"),
            "no docker/gcloud command may use the raw, unsanitized ref as an image tag: \
             {content}"
        );
    }

    #[test]
    fn gcp_workflow_image_tag_is_unique_per_execution() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("${GITHUB_SHA:0:12}"),
            "the computed image tag must include the commit SHA: {content}"
        );
        assert!(
            content.contains("${GITHUB_RUN_ID}") && content.contains("${GITHUB_RUN_ATTEMPT}"),
            "the computed image tag must also include the run ID and run attempt: {content}"
        );
    }

    #[test]
    fn gcp_workflow_serializes_overlapping_runs() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("concurrency:"),
            "gcp-deploy.yml must define a concurrency group so overlapping runs queue \
             instead of racing: {content}"
        );
        assert!(
            content.contains("cancel-in-progress: false"),
            "cancel-in-progress must be false — killing a run mid-migration or \
             mid-cutover is worse than making the next run wait: {content}"
        );
    }

    #[test]
    fn gcp_workflow_guards_against_superseded_run_before_migrating() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("actions: read"),
            "gcp-deploy.yml must grant actions: read to query other workflow runs: {content}"
        );
        assert!(
            content.contains("run_number > ${{ github.run_number }}"),
            "the guard must compare against other runs' run_number: {content}"
        );

        let guard_pos = content
            .find("gh api")
            .expect("the run_number staleness guard must be present");
        let migrate_pos = content
            .find("gcloud run jobs execute \"$GCP_MIGRATE_JOB_NAME\"")
            .expect("migration job execute must be present");
        let deploy_pos = content
            .find("gcloud run services update \"$GCP_SERVICE_NAME\"")
            .expect("deploy step must be present");
        assert!(
            guard_pos < migrate_pos && migrate_pos < deploy_pos,
            "the staleness guard must run BEFORE migration, which must run BEFORE \
             deploy: {content}"
        );
    }

    #[test]
    fn gcp_workflow_documents_that_repo_variables_need_manual_resync() {
        // GCP_SERVICE_NAME/GCP_MIGRATE_JOB_NAME/GCP_ARTIFACT_REGISTRY_URL are
        // GitHub repository variables — a one-time snapshot of `terraform
        // output`, not a live link to Terraform state. If app_name/region
        // changes after the workflow is configured, Terraform renames the
        // underlying GCP resources but GitHub has no way to know that
        // happened; the header comment must say so explicitly rather than
        // implying this stays in sync automatically.
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "my-app");
        init(&dir, "my-app", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("not a live link to Terraform") && content.contains("manually re-run"),
            "gcp-deploy.yml's header comment must warn that the repository variables \
             are a manual snapshot that goes stale after an app_name/region change, \
             not something kept in sync automatically: {content}"
        );
    }

    #[test]
    fn gcp_workflow_sources_service_name_from_terraform_not_hardcoded() {
        let tmp = TempDir::new().unwrap();
        let dir = make_project(&tmp, "My_Test_App");
        init(&dir, "My_Test_App", false, Target::GcpCloudRun, false).unwrap();
        let content = fs::read_to_string(dir.join(".github/workflows/gcp-deploy.yml")).unwrap();
        assert!(
            content.contains("vars.GCP_SERVICE_NAME"),
            "GCP_SERVICE_NAME must be sourced from a repository variable (terraform \
             output service_name), not hardcoded: {content}"
        );
        assert!(
            !content.contains("My_Test_App") && !content.contains("my-test-app"),
            "gcp-deploy.yml must not bake in any form of the project name as a GCP \
             resource identifier: {content}"
        );
        assert!(
            !content.contains("{{project_name}}"),
            "gcp-deploy.yml must not contain unsubstituted placeholders: {content}"
        );
    }
}