flux-platform 1.0.0

A local-first, AI-native developer automation platform: build, test, package, and deploy from a single .flux file, and make your repository legible to AI agents.
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
//! End-to-end tests that drive the compiled `flux` binary against throwaway
//! project directories.

use std::path::{Path, PathBuf};
use std::process::Command;

/// Path to the freshly built binary (provided by Cargo for integration tests).
fn flux() -> Command {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_flux"));
    // Keep output deterministic for assertions.
    cmd.env("NO_COLOR", "1");
    cmd
}

/// Create a unique, empty temp directory for one test.
fn temp_project(tag: &str) -> PathBuf {
    let mut dir = std::env::temp_dir();
    dir.push(format!("flux-it-{}-{}", tag, std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

fn write(dir: &Path, name: &str, contents: &str) {
    std::fs::write(dir.join(name), contents).unwrap();
}

fn run(dir: &Path, args: &[&str]) -> (String, bool) {
    let out = flux().args(args).current_dir(dir).output().unwrap();
    let mut s = String::from_utf8_lossy(&out.stdout).into_owned();
    s.push_str(&String::from_utf8_lossy(&out.stderr));
    (s, out.status.success())
}

#[test]
fn init_detects_rust_and_writes_config() {
    let dir = temp_project("init");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"widget\"\nversion = \"0.1.0\"\n",
    );

    let (out, ok) = run(&dir, &["init"]);
    assert!(ok, "init should succeed: {out}");
    assert!(out.contains("Initializing project"), "{out}");
    assert!(out.contains("AI agents configured"), "{out}");

    let cfg = std::fs::read_to_string(dir.join(".flux")).unwrap();
    assert!(cfg.contains("project \"widget\""), "{cfg}");
    assert!(cfg.contains("language rust"), "{cfg}");
    assert!(cfg.contains("cargo build --release"), "{cfg}");

    // init also scaffolds the AI-platform layer.
    let platform = std::fs::read_to_string(dir.join("flux.yaml")).unwrap();
    assert!(platform.contains("name: widget"), "{platform}");
    assert!(dir.join(".flux.d").join("agents").is_dir());

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn build_runs_pipeline_then_caches() {
    let dir = temp_project("build");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"c\"\nversion = \"0.1.0\"\n",
    );
    write(&dir, "src.txt", "hello");
    write(
        &dir,
        ".flux",
        "project \"c\"\nlanguage rust\npipeline {\n  step build { command \"echo compiling\" }\n}\n",
    );

    let (first, ok1) = run(&dir, &["build"]);
    assert!(ok1, "first build should succeed: {first}");
    assert!(
        first.contains("compiling"),
        "should run the command: {first}"
    );
    assert!(first.contains("Build completed"), "{first}");

    let (second, ok2) = run(&dir, &["build"]);
    assert!(ok2, "second build should succeed: {second}");
    assert!(
        second.contains("cached"),
        "second build should be cached: {second}"
    );

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn failing_step_stops_pipeline_with_nonzero_exit() {
    let dir = temp_project("fail");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"f\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        "project \"f\"\nlanguage rust\npipeline {\n  step build { command \"exit 3\" }\n  step test { command \"echo should-not-run\" }\n}\n",
    );

    let (out, ok) = run(&dir, &["build"]);
    assert!(!ok, "build should fail: {out}");
    assert!(out.contains("Build failed"), "{out}");
    assert!(
        !out.contains("should-not-run"),
        "later steps must not run: {out}"
    );

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn dependency_graph_runs_needs_and_propagates_failure() {
    let dir = temp_project("dag");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"d\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        "project \"d\"\nlanguage rust\npipeline {\n\
           step frontend { command \"echo fe\" }\n\
           step backend  { command \"echo be\" }\n\
           step tests { needs [ frontend, backend ] command \"exit 1\" }\n\
           step package { needs tests command \"echo packaging\" }\n\
         }\n",
    );

    let (out, ok) = run(&dir, &["build"]);
    assert!(!ok, "build should fail when tests fail: {out}");
    // The explicit-graph banner should appear.
    assert!(out.contains("dependency graph"), "{out}");
    // package needs tests, which failed → package must be skipped.
    assert!(!out.contains("packaging"), "dependent must not run: {out}");
    assert!(out.contains("skipped (dependency failed)"), "{out}");

    let _ = std::fs::remove_dir_all(&dir);
}

/// Expand an injected environment variable in a shell-appropriate way: `%VAR%`
/// on the Windows `cmd` shell, `$VAR` on the POSIX `sh` shell.
fn echo_token() -> &'static str {
    if cfg!(windows) {
        "echo token=%TOKEN%"
    } else {
        "echo token=$TOKEN"
    }
}

#[test]
fn secret_is_injected_by_the_secrets_field() {
    let dir = temp_project("secret");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"s\"\nversion = \"0.1.0\"\n",
    );
    let echo = echo_token();
    write(
        &dir,
        ".flux",
        &format!(
            "project \"s\"\nlanguage rust\nsecret TOKEN\npipeline {{\n\
               step show {{ command \"{echo}\" secrets [ TOKEN ] }}\n\
             }}\n"
        ),
    );

    let (set_out, set_ok) = run(&dir, &["secret", "set", "TOKEN", "abc123"]);
    assert!(set_ok, "secret set should succeed: {set_out}");

    let (out, ok) = run(&dir, &["build"]);
    assert!(ok, "build should succeed: {out}");
    assert!(out.contains("token=abc123"), "secret not injected: {out}");

    let _ = std::fs::remove_dir_all(&dir);
}

/// `env` is the previous name for `secrets`. For one release it still works,
/// warns with the file and line, and is rewritten by `flux format`.
#[test]
fn deprecated_env_field_still_injects_and_warns() {
    let dir = temp_project("envdep");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"s\"\nversion = \"0.1.0\"\n",
    );
    let echo = echo_token();
    write(
        &dir,
        ".flux",
        &format!(
            "project \"s\"\nlanguage rust\nsecret TOKEN\npipeline {{\n\
               step show {{ command \"{echo}\" env [ TOKEN ] }}\n\
             }}\n"
        ),
    );

    let (set_out, set_ok) = run(&dir, &["secret", "set", "TOKEN", "abc123"]);
    assert!(set_ok, "{set_out}");

    let (out, ok) = run(&dir, &["build"]);
    assert!(ok, "a deprecated field must not break the build: {out}");
    assert!(out.contains("token=abc123"), "secret not injected: {out}");
    assert!(out.contains("warning:"), "no deprecation warning: {out}");
    assert!(out.contains("'env' field is deprecated"), "{out}");
    // The step sits on line 5, and the warning must say so.
    assert!(out.contains("line 5"), "{out}");

    // Formatting migrates the file.
    let (fmt_out, fmt_ok) = run(&dir, &["format"]);
    assert!(fmt_ok, "{fmt_out}");
    let cfg = std::fs::read_to_string(dir.join(".flux")).unwrap();
    assert!(cfg.contains("secrets [ TOKEN ]"), "{cfg}");
    assert!(!cfg.contains("env [ TOKEN ]"), "{cfg}");

    // And the migrated file is warning-free.
    let (after, after_ok) = run(&dir, &["validate"]);
    assert!(after_ok, "{after}");
    assert!(!after.contains("warning:"), "{after}");

    let _ = std::fs::remove_dir_all(&dir);
}

/// The two removed keywords fail loudly and point at what replaced them,
/// instead of parsing into something nothing reads.
#[test]
fn removed_keywords_are_parse_errors_naming_their_replacement() {
    let dir = temp_project("retired");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"r\"\nversion = \"0.1.0\"\n",
    );

    write(
        &dir,
        ".flux",
        "project \"r\"\npipeline { step build { command \"echo x\" pool \"gpu\" } }\n",
    );
    let (pool_out, pool_ok) = run(&dir, &["validate"]);
    assert!(!pool_ok, "step 'pool' must be rejected: {pool_out}");
    assert!(pool_out.contains("'pool' was removed"), "{pool_out}");
    assert!(pool_out.contains("runners"), "{pool_out}");

    write(
        &dir,
        ".flux",
        "project \"r\"\nimport shared-ci\npipeline { step build { command \"echo x\" } }\n",
    );
    let (imp_out, imp_ok) = run(&dir, &["validate"]);
    assert!(!imp_ok, "top-level 'import' must be rejected: {imp_out}");
    assert!(imp_out.contains("directive was removed"), "{imp_out}");
    assert!(imp_out.contains("use <name>"), "{imp_out}");

    let _ = std::fs::remove_dir_all(&dir);
}

/// `only_if` accepts exactly three variables. A fourth is a parse error listing
/// them, not a condition that quietly compares against the empty string.
#[test]
fn only_if_namespace_is_closed_and_documented() {
    let dir = temp_project("onlyif");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"o\"\nversion = \"0.1.0\"\n",
    );

    write(
        &dir,
        ".flux",
        "project \"o\"\npipeline {\n\
           step a { command \"echo a\" only_if branch != \"\" }\n\
           step b { command \"echo b\" only_if tag == \"v1\" }\n\
           step c { command \"echo c\" only_if flux_env == \"default\" }\n\
         }\n",
    );
    let (good, good_ok) = run(&dir, &["validate"]);
    assert!(good_ok, "every documented variable must parse: {good}");

    write(
        &dir,
        ".flux",
        "project \"o\"\npipeline { step a { command \"echo a\" only_if brunch == \"main\" } }\n",
    );
    let (bad, bad_ok) = run(&dir, &["validate"]);
    assert!(!bad_ok, "an unknown variable must be rejected: {bad}");
    assert!(bad.contains("unknown only_if variable"), "{bad}");
    for var in ["branch", "tag", "flux_env"] {
        assert!(bad.contains(var), "error should list '{var}': {bad}");
    }

    let _ = std::fs::remove_dir_all(&dir);
}

/// A command that blocks for roughly `secs` seconds on either shell. Windows
/// `cmd` has no `sleep`, and its `timeout` builtin refuses to run with stdin
/// redirected, so `ping` to the loopback is the portable stand-in.
fn sleep_cmd(secs: u32) -> String {
    if cfg!(windows) {
        format!("ping -n {} 127.0.0.1 >nul", secs + 1)
    } else {
        format!("sleep {secs}")
    }
}

/// Output arrives tagged with the step that produced it, for both streams, so a
/// parallel run can be read.
#[test]
fn build_streams_output_attributed_to_each_step() {
    let dir = temp_project("stream");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"s\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        "project \"s\"\nlanguage rust\npipeline {\n\
           step frontend { command \"echo bundling\" }\n\
           step backend  { command \"echo warming 1>&2\" }\n\
           step tests { needs [ frontend, backend ] command \"echo 3 passed\" }\n\
         }\n",
    );

    let (out, ok) = run(&dir, &["build"]);
    assert!(ok, "{out}");
    // stdout is tagged with '│', stderr with '┆'; both name their step.
    assert!(
        out.contains("frontend \u{2502} bundling"),
        "stdout not attributed: {out}"
    );
    assert!(
        out.contains("backend \u{2506} warming"),
        "stderr not attributed: {out}"
    );
    assert!(out.contains("tests \u{2502} 3 passed"), "{out}");

    let _ = std::fs::remove_dir_all(&dir);
}

/// The headline: a hung step is killed, the build ends, and the reason is on
/// screen. Without the timeout this test would hang for a minute.
#[test]
fn a_step_timeout_kills_the_command_and_fails_the_build() {
    let dir = temp_project("timeout");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"t\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        &format!(
            "project \"t\"\nlanguage rust\npipeline {{\n\
               step hang {{ command \"{}\" timeout 1 }}\n\
               step after {{ needs hang command \"echo should-not-run\" }}\n\
             }}\n",
            sleep_cmd(60)
        ),
    );

    let start = std::time::Instant::now();
    let (out, ok) = run(&dir, &["build"]);
    let elapsed = start.elapsed();

    assert!(!ok, "a timed-out build must exit non-zero: {out}");
    assert!(
        out.contains("timed out after 1s"),
        "the reason must be reported: {out}"
    );
    assert!(out.contains("timed out"), "{out}");
    assert!(
        !out.contains("should-not-run"),
        "dependents of a killed step must be skipped: {out}"
    );
    assert!(
        elapsed < std::time::Duration::from_secs(30),
        "the command should have been killed at 1s, not waited out: {elapsed:?}"
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// The limit is inherited from `pipeline { timeout … }` and a step may override
/// it in either direction.
#[test]
fn pipeline_timeout_is_the_default_and_steps_override_it() {
    let dir = temp_project("timeoutdefault");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"t\"\nversion = \"0.1.0\"\n",
    );
    // `patient` sleeps past the pipeline's 1s limit but declares its own, so it
    // survives; `impatient` inherits the 1s and is killed.
    write(
        &dir,
        ".flux",
        &format!(
            "project \"t\"\nlanguage rust\npipeline {{\n\
               timeout 1\n\
               step patient {{ command \"{} && echo patient-done\" timeout \"2m\" }}\n\
               step impatient {{ needs patient command \"{}\" }}\n\
             }}\n",
            sleep_cmd(2),
            sleep_cmd(60)
        ),
    );

    let (out, ok) = run(&dir, &["build"]);
    assert!(!ok, "{out}");
    assert!(out.contains("step timeout: 1s"), "{out}");
    assert!(
        out.contains("patient-done"),
        "a step with its own longer timeout must finish: {out}"
    );
    assert!(
        out.contains("impatient") && out.contains("timed out after 1s"),
        "a step inheriting the pipeline timeout must be killed: {out}"
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// `parallel 1` really serializes: each step's output completes before the next
/// one starts. With the cap ignored, the sleeping steps would interleave.
#[test]
fn parallel_caps_how_many_steps_run_at_once() {
    let dir = temp_project("parallel");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"p\"\nversion = \"0.1.0\"\n",
    );
    let sleep = sleep_cmd(1);
    write(
        &dir,
        ".flux",
        &format!(
            "project \"p\"\nlanguage rust\npipeline {{\n\
               parallel 1\n\
               step a {{ command \"echo a-start && {sleep} && echo a-end\" }}\n\
               step b {{ command \"echo b-start && {sleep} && echo b-end\" }}\n\
               step c {{ command \"echo c-start && {sleep} && echo c-end\" }}\n\
               step all {{ needs [ a, b, c ] command \"echo all\" }}\n\
             }}\n"
        ),
    );

    let (out, ok) = run(&dir, &["build"]);
    assert!(ok, "{out}");
    assert!(out.contains("up to 1 step in parallel"), "{out}");
    let at = |needle: &str| {
        out.find(needle)
            .unwrap_or_else(|| panic!("missing '{needle}' in:\n{out}"))
    };
    for (first, second) in [("a-end", "b-start"), ("b-end", "c-start")] {
        assert!(
            at(first) < at(second),
            "with parallel 1, '{first}' must precede '{second}':\n{out}"
        );
    }

    // The command line overrides the file for this machine.
    let (wide, wide_ok) = run(&dir, &["build", "--parallel", "3"]);
    assert!(wide_ok, "{wide}");
    assert!(wide.contains("up to 3 steps in parallel"), "{wide}");

    let _ = std::fs::remove_dir_all(&dir);
}

/// `flux format` has to round-trip the new fields, or formatting a file would
/// quietly drop the limits it declares.
#[test]
fn format_round_trips_timeout_and_parallel() {
    let dir = temp_project("fmttimeout");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"f\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        "project \"f\"\npipeline{parallel 2 timeout \"10m\" step build{command \"cargo build\" timeout 90}step serve{command \"./serve\" timeout off}}\n",
    );

    let (out, ok) = run(&dir, &["format"]);
    assert!(ok, "{out}");
    let cfg = std::fs::read_to_string(dir.join(".flux")).unwrap();
    assert!(cfg.contains("    timeout \"10m\""), "{cfg}");
    assert!(cfg.contains("    parallel 2"), "{cfg}");
    assert!(cfg.contains("        timeout \"90s\""), "{cfg}");
    assert!(cfg.contains("        timeout off"), "{cfg}");

    let (check, check_ok) = run(&dir, &["format", "--check"]);
    assert!(check_ok, "the formatted file must be stable: {check}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn cyclic_pipeline_is_rejected() {
    let dir = temp_project("cycle");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"c\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        "pipeline {\n\
           step a { needs b command \"echo a\" }\n\
           step b { needs a command \"echo b\" }\n\
         }\n",
    );

    let (out, ok) = run(&dir, &["build"]);
    assert!(!ok, "cyclic pipeline must be rejected: {out}");
    assert!(out.contains("cycle"), "{out}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn intelligent_cache_rebuilds_only_affected() {
    let dir = temp_project("intel");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"m\"\nversion = \"0.1.0\"\n",
    );
    std::fs::create_dir_all(dir.join("frontend")).unwrap();
    std::fs::create_dir_all(dir.join("backend")).unwrap();
    write(&dir, "frontend/a.ts", "v1");
    write(&dir, "backend/b.rs", "v1");
    // The `all` step's `needs` makes this an explicit graph, so frontend and
    // backend are independent roots (not an implicit chain).
    write(
        &dir,
        ".flux",
        "project \"m\"\nlanguage rust\npipeline {\n\
           step frontend { command \"echo fe\" inputs [ \"frontend/**\" ] }\n\
           step backend  { command \"echo be\" inputs [ \"backend/**\" ] }\n\
           step all { needs [ frontend, backend ] command \"echo all\" }\n\
         }\n",
    );

    let (_, ok1) = run(&dir, &["build"]);
    assert!(ok1);
    // Change only a frontend file.
    write(&dir, "frontend/a.ts", "v2");
    let (out, ok2) = run(&dir, &["build"]);
    assert!(ok2, "{out}");
    // backend stays cached (its inputs are unchanged); frontend rebuilds.
    assert!(
        out.contains("backend  (cached"),
        "backend should stay cached: {out}"
    );
    assert!(out.contains("fe"), "frontend should rebuild: {out}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn module_use_splices_reusable_steps() {
    let dir = temp_project("module");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"a\"\nversion = \"0.1.0\"\n",
    );
    std::fs::create_dir_all(dir.join("modules")).unwrap();
    write(
        &dir,
        "modules/base.flux",
        "pipeline {\n  step compile { command \"echo compiling\" }\n  step test { command \"echo testing\" }\n}\n",
    );
    write(
        &dir,
        ".flux",
        "project \"a\"\nlanguage rust\npipeline {\n  use base\n  step package { command \"echo packaging\" }\n}\n",
    );

    let (out, ok) = run(&dir, &["build"]);
    assert!(ok, "{out}");
    for step in ["compile", "test", "package"] {
        assert!(out.contains(step), "module step '{step}' missing: {out}");
    }

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn workspace_builds_only_affected_members() {
    let dir = temp_project("ws");
    for member in ["shared", "auth"] {
        let mroot = dir.join(member);
        std::fs::create_dir_all(&mroot).unwrap();
        std::fs::write(
            mroot.join("Cargo.toml"),
            format!("[package]\nname=\"{member}\"\nversion=\"0.1.0\"\n"),
        )
        .unwrap();
        std::fs::write(
            mroot.join(".flux"),
            format!("project \"{member}\"\nlanguage rust\npipeline {{ step build {{ command \"echo build-{member}\" }} }}\n"),
        )
        .unwrap();
        std::fs::write(mroot.join("src.txt"), "v1").unwrap();
    }
    write(
        &dir,
        "flux.workspace",
        "workspace \"w\"\nmember shared { path \"shared\" }\nmember auth { path \"auth\" needs [ shared ] }\n",
    );

    // First build: both affected.
    let (out1, ok1) = run(&dir, &["workspace", "build"]);
    assert!(ok1, "{out1}");
    assert!(
        out1.contains("build-shared") && out1.contains("build-auth"),
        "{out1}"
    );

    // Change only `auth`; rebuild should skip `shared`.
    std::fs::write(dir.join("auth").join("src.txt"), "v2").unwrap();
    let (out2, ok2) = run(&dir, &["workspace", "build"]);
    assert!(ok2, "{out2}");
    assert!(
        out2.contains("shared  skipped (unchanged)"),
        "shared should be skipped: {out2}"
    );
    assert!(out2.contains("build-auth"), "auth should rebuild: {out2}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn policy_blocks_ci_when_violated() {
    let dir = temp_project("policy");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"p\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        "project \"p\"\nlanguage rust\npolicy prod { require tests require security }\n\
         pipeline { step build { command \"echo building\" } }\n",
    );

    let (out, ok) = run(&dir, &["ci"]);
    assert!(!ok, "ci must be blocked by policy: {out}");
    assert!(out.contains("Policy violations"), "{out}");
    assert!(
        !out.contains("building"),
        "the pipeline must not run: {out}"
    );

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn init_template_writes_tailored_pipeline() {
    let dir = temp_project("tpl");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"api\"\nversion = \"0.1.0\"\n",
    );

    let (out, ok) = run(&dir, &["init", "rust-api"]);
    assert!(ok, "{out}");
    let cfg = std::fs::read_to_string(dir.join(".flux")).unwrap();
    assert!(
        cfg.contains("environment { image \"rust:latest\" }"),
        "{cfg}"
    );
    assert!(cfg.contains("target kubernetes"), "{cfg}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn validate_accepts_good_and_rejects_bad() {
    let dir = temp_project("validate");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"v\"\nversion = \"0.1.0\"\n",
    );

    // Valid pipeline.
    write(
        &dir,
        ".flux",
        "project \"v\"\nlanguage rust\npipeline { step build { command \"cargo build\" } }\n",
    );
    let (out, ok) = run(&dir, &["validate"]);
    assert!(ok, "valid .flux should pass: {out}");
    assert!(out.contains(".flux is valid"), "{out}");

    // Syntax error → non-zero exit.
    write(
        &dir,
        ".flux",
        "project \"v\"\npipeline { step build { commnd \"x\" } }\n",
    );
    let (bad, bad_ok) = run(&dir, &["validate"]);
    assert!(!bad_ok, "invalid .flux must fail: {bad}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn format_is_idempotent() {
    let dir = temp_project("fmt");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"f\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        "project \"f\"\nlanguage rust\npipeline{step build{command \"cargo build\"}step test{needs build command \"cargo test\"}}\n",
    );

    let (_, ok) = run(&dir, &["format"]);
    assert!(ok);
    let (out2, ok2) = run(&dir, &["format", "--check"]);
    assert!(ok2, "formatted file should pass --check: {out2}");
    assert!(out2.contains("already formatted"), "{out2}");
    let cfg = std::fs::read_to_string(dir.join(".flux")).unwrap();
    assert!(cfg.contains("    step build {"), "{cfg}");
    assert!(cfg.contains("needs [ build ]"), "{cfg}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn explain_describes_the_pipeline() {
    let dir = temp_project("explain");
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"e\"\nversion = \"0.1.0\"\n",
    );
    write(
        &dir,
        ".flux",
        "project \"e\"\nlanguage rust\npipeline {\n\
           step build { command \"cargo build\" }\n\
           step test { needs build command \"cargo test\" }\n\
         }\n",
    );
    let (out, ok) = run(&dir, &["explain"]);
    assert!(ok, "{out}");
    assert!(out.contains("2 step"), "{out}");
    assert!(out.contains("after build"), "{out}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn plugin_search_filters_catalog() {
    let dir = temp_project("psearch");
    let (out, ok) = run(&dir, &["plugin", "search", "docker"]);
    assert!(ok, "{out}");
    assert!(out.contains("docker"), "{out}");
    assert!(
        !out.contains("terraform"),
        "unrelated plugin should not match: {out}"
    );

    // Built-in plugins live in `plugins::registry()`, not `CATALOG`; searching
    // for one used to report "no matches".
    for builtin in ["rust", "node", "python"] {
        let (out, ok) = run(&dir, &["plugin", "search", builtin]);
        assert!(ok, "{out}");
        assert!(
            out.contains(builtin),
            "built-in '{builtin}' should be found: {out}"
        );
        assert!(
            !out.contains("no matches"),
            "built-in '{builtin}' should not report no matches: {out}"
        );
        assert!(
            out.contains("built-in"),
            "built-in '{builtin}' should be marked as such: {out}"
        );
    }

    let _ = std::fs::remove_dir_all(&dir);
}

// ---- AI-native platform layer ----

/// A helper: a minimal Rust project with a source file.
fn rust_project(tag: &str) -> PathBuf {
    let dir = temp_project(tag);
    write(
        &dir,
        "Cargo.toml",
        "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n\n[dependencies]\nanyhow = \"1\"\n",
    );
    std::fs::create_dir_all(dir.join("src")).unwrap();
    write(&dir, "src/main.rs", "pub fn go() {}\nfn main() {}\n");
    dir
}

#[test]
fn project_reports_intelligence_and_writes_knowledge_graph() {
    let dir = rust_project("project");
    let (out, ok) = run(&dir, &["project"]);
    assert!(ok, "{out}");
    assert!(out.contains("Repository Intelligence"), "{out}");
    assert!(out.contains("Health"), "{out}");
    // The knowledge graph is written for AI/tools to consume.
    let arch = dir.join(".flux-cache/knowledge/architecture.json");
    assert!(arch.is_file(), "architecture.json should exist");
    let arch = std::fs::read_to_string(arch).unwrap();
    assert!(arch.contains("\"components\""), "{arch}");

    // --json emits the architecture graph on stdout.
    let (jout, jok) = run(&dir, &["project", "--json"]);
    assert!(jok, "{jout}");
    assert!(jout.contains("\"project\""), "{jout}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn deps_json_emits_report() {
    let dir = rust_project("depsjson");
    let (out, ok) = run(&dir, &["deps", "--json"]);
    assert!(ok, "deps --json should succeed: {out}");
    assert!(out.contains("\"total\""), "{out}");
    assert!(out.contains("\"outdatedNote\""), "{out}");
    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn agent_list_and_run_maintenance() {
    let dir = rust_project("agent");
    let (list, ok) = run(&dir, &["agent", "list"]);
    assert!(ok, "{list}");
    assert!(list.contains("maintenance"), "{list}");
    assert!(list.contains("reviewer"), "{list}");

    let (run_out, ok) = run(&dir, &["agent", "run", "maintenance"]);
    assert!(ok, "{run_out}");
    assert!(run_out.contains("Flux Maintenance"), "{run_out}");
    assert!(
        dir.join(".flux-cache/reports/maintenance.md").is_file(),
        "the agent should write a report"
    );

    // Unknown agents fail clearly.
    let (bad, ok) = run(&dir, &["agent", "run", "nope"]);
    assert!(!ok, "unknown agent should fail: {bad}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn ask_context_prints_bundle() {
    let dir = rust_project("ask");
    let (out, ok) = run(&dir, &["ask", "--context"]);
    assert!(ok, "{out}");
    assert!(out.contains("Project: demo"), "{out}");
    assert!(out.contains("## Components"), "{out}");

    // A plain question falls back to an offline, labelled answer.
    let (ans, ok) = run(&dir, &["ask", "explain this repository"]);
    assert!(ok, "{ans}");
    assert!(ans.contains("offline heuristic"), "{ans}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn github_init_writes_workflow() {
    let dir = rust_project("gh");
    let (out, ok) = run(&dir, &["github", "init"]);
    assert!(ok, "{out}");
    assert!(dir.join(".github/workflows/flux.yml").is_file());
    assert!(dir.join(".github/pull_request_template.md").is_file());
    let wf = std::fs::read_to_string(dir.join(".github/workflows/flux.yml")).unwrap();
    assert!(wf.contains("flux verify"), "{wf}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn docs_generate_then_check_is_in_sync() {
    let dir = rust_project("docs");
    // Before generation, --check reports drift and exits non-zero.
    let (chk, ok) = run(&dir, &["docs", "--check"]);
    assert!(!ok, "check should fail before generation: {chk}");

    let (gen, ok) = run(&dir, &["docs"]);
    assert!(ok, "{gen}");
    assert!(dir.join("docs/commands.md").is_file());
    assert!(dir.join("docs/manifest.json").is_file());

    let (chk2, ok) = run(&dir, &["docs", "--check"]);
    assert!(ok, "check should pass after generation: {chk2}");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn dashboard_writes_self_contained_html() {
    let dir = rust_project("dash");
    let (out, ok) = run(&dir, &["dashboard"]);
    assert!(ok, "{out}");
    let html = dir.join(".flux-cache/reports/dashboard.html");
    assert!(html.is_file());
    let html = std::fs::read_to_string(html).unwrap();
    assert!(html.starts_with("<!doctype html>"), "{}", &html[..40]);
    assert!(html.contains("demo"), "should name the project");
    // Self-contained: no external resource references.
    assert!(
        !html.contains("http://") && !html.contains("https://"),
        "must not reference the network"
    );

    let _ = std::fs::remove_dir_all(&dir);
}