File size: 25,799 Bytes
b41f3a7
dc6756e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b41f3a7
 
 
 
 
 
 
 
 
 
 
 
5090ba8
b41f3a7
5090ba8
 
 
 
a07daae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5090ba8
 
 
a07daae
 
 
 
b41f3a7
 
 
dc6756e
 
b41f3a7
 
 
 
 
 
 
 
 
dc6756e
 
 
 
b41f3a7
 
 
 
 
 
 
 
 
 
 
 
 
 
621cf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b41f3a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142cbad
 
dc6756e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5090ba8
dc6756e
5090ba8
dc6756e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5090ba8
dc6756e
5090ba8
dc6756e
 
 
 
142cbad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a19eae6
 
f76b6e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5090ba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f76b6e4
 
 
 
 
 
 
 
 
5090ba8
f76b6e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5090ba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f76b6e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a19eae6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import app
from nexus_visual_weaver.planner import build_command_center_run


# --- _checkpoint_seed tests ---

def test_checkpoint_seed_parses_valid_hex_suffix() -> None:
    # "nw-" prefix + "abcdef12" -> int("abcdef12", 16) % 1_000_000
    checkpoint_id = "nw-abcdef12"
    result = app._checkpoint_seed(checkpoint_id)
    assert result == int("abcdef12", 16) % 1_000_000


def test_checkpoint_seed_handles_empty_suffix() -> None:
    assert app._checkpoint_seed("") == 0
    assert app._checkpoint_seed("nw-") == 0


def test_checkpoint_seed_handles_non_hex_suffix() -> None:
    # No hex chars -> return 0
    result = app._checkpoint_seed("nw-zzzzzzzz")
    assert result == 0


def test_checkpoint_seed_ignores_non_hex_chars_in_last_8() -> None:
    # Strips non-hex: "abc-xyz" -> only "abcef" are hex in the last 8 chars ("-xyz" strips to "abc")
    # "0000000g" -> only "0000000" are valid hex
    result = app._checkpoint_seed("0000000g")
    assert result == int("0000000", 16) % 1_000_000
    assert result == 0


def test_checkpoint_seed_returns_value_within_range() -> None:
    checkpoint_id = "nw-deadbeef"
    result = app._checkpoint_seed(checkpoint_id)
    assert 0 <= result < 1_000_000


def test_checkpoint_seed_uses_only_last_8_chars() -> None:
    # Long checkpoint id: only last 8 chars considered
    # "nw-aabbccddee11223344" -> last 8 is "11223344"
    result = app._checkpoint_seed("nw-aabbccddee11223344")
    assert result == int("11223344", 16) % 1_000_000


# --- _wardrobe_summary tests ---

def test_wardrobe_summary_returns_string_with_slot_fields() -> None:
    run = build_command_center_run("gothic patent leather platform boots, crimson hardware")
    summary = app._wardrobe_summary(run)

    # Summary is a semicolon-delimited string of slot info
    assert isinstance(summary, str)
    assert len(summary) > 0
    # Should contain material= and palette= from slot info
    assert "material=" in summary
    assert "palette=" in summary
    assert "locked=" in summary


def test_wardrobe_summary_handles_run_with_no_outfit() -> None:
    class FakeRun:
        outfit = None

    summary = app._wardrobe_summary(FakeRun())
    assert summary == ""


def test_wardrobe_summary_handles_none_run() -> None:
    # Pass something with no outfit attribute
    class Empty:
        pass

    summary = app._wardrobe_summary(Empty())
    assert summary == ""


def test_wardrobe_summary_includes_slot_names() -> None:
    run = build_command_center_run("dark couture archivist, patent leather, platform boots")
    summary = app._wardrobe_summary(run)

    # Slot names like footwear, outerwear, upper_body should appear
    assert any(slot_name in summary for slot_name in ["footwear", "outerwear", "upper_body", "jewelry"])


def test_run_weave_returns_stateful_dashboard_packet() -> None:
    result = app.run_weave(
        "gothic patent leather platform boots, crimson hardware",
        "Strict",
        "Wan2.2 I2V",
        False,
        None,
        "Forge",
    )

    assert len(result) == 19
    assert result[13].checkpoint.checkpoint_id.startswith("nw-")
    assert result[15]["provider_state"] == "dry-run"
    assert result[16]["interactive"] is False
    assert result[17]["interactive"] is False
    assert result[18]["interactive"] is False
    assert result[15]["creator_controls"]["wardrobe"]["footwear"] == "platform boots"
    assert result[15]["generation"]["lora_status"] == "disabled"


def test_run_weave_persists_additive_creator_controls_and_reference_url_metadata() -> None:
    result = app.run_weave(
        "gothic patent leather platform boots, crimson hardware",
        "Frontier",
        "LTX-2.3",
        False,
        None,
        "Wardrobe",
        "layered tactical silhouette",
        "faux fur collar coat",
        "black mesh layer",
        "patent leather heels",
        "obsidian, pearl, crimson",
        "silver occult buckles",
        "https://shop.example.test/item/123",
    )

    state = result[15]
    run = result[13]

    assert state["creator_controls"]["reasoning_mode"] == "Frontier"
    assert state["creator_controls"]["wardrobe"]["footwear"] == "patent leather heels"
    assert state["creator_controls"]["generation"]["seed"] >= 0
    assert state["creator_controls"]["generation"]["style_strength"] == "High Fashion"
    assert state["creator_controls"]["generation"]["aspect"] == "Portrait"
    assert state["reference_metadata"][0]["source"] == "url"
    assert state["reference_metadata"][0]["domain"] == "shop.example.test"
    assert "url_hash" in state["reference_metadata"][0]
    assert run.request.creator_controls["wardrobe"]["outerwear"] == "faux fur collar coat"


def test_operator_actions_transition_checkpoint_export_and_stop() -> None:
    from pathlib import Path

    result = app.run_weave(
        "gothic patent leather platform boots, crimson hardware",
        "Strict",
        "Wan2.2 I2V",
        False,
        None,
        "Forge",
    )
    run = result[13]
    artifact_path = Path("outputs/test-generated-artifact.png")
    artifact_path.parent.mkdir(parents=True, exist_ok=True)
    artifact_path.write_bytes(b"\x89PNG\r\n\x1a\n" + b"\x00" * 100)
    operator_state = {**result[15], "generation": {**result[15]["generation"], "output_path": str(artifact_path)}}
    clean_scan = {"status": "pass", "export_gate": "clear", "findings": [], "purification_actions": []}

    approved = app.approve_checkpoint(run, False, clean_scan, "Forge", operator_state)
    assert approved[13]["checkpoint"] == "approved"
    assert approved[13]["provider_state"] == "export_ready"

    exported = app.export_packet(run, False, clean_scan, "Forge", approved[13])
    assert exported[13]["provider_state"] == "exported"
    assert exported[13]["export"] == "clear"

    stopped = app.stop_provider_job(run, False, clean_scan, "Forge", operator_state)
    assert stopped[13]["provider_state"] == "stopped"


def test_clear_export_ignores_stale_override_reason(monkeypatch) -> None:
    monkeypatch.setenv("NEXUS_EXPORT_DIR", "outputs/test-exports")
    result = app.run_weave(
        "gothic patent leather platform boots, crimson hardware",
        "Strict",
        "Wan2.2 I2V",
        False,
        None,
        "Forge",
    )
    run = result[13]
    artifact_path = app.ROOT / "outputs" / "test-clear-override-artifact.png"
    artifact_path.parent.mkdir(parents=True, exist_ok=True)
    artifact_path.write_bytes(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIEND\xaeB`\x82")
    operator_state = {
        **result[15],
        "generation": {**result[15]["generation"], "output_path": str(artifact_path)},
    }
    clean_scan = {"status": "pass", "export_gate": "clear", "findings": [], "purification_actions": []}

    approved = app.approve_checkpoint(run, False, clean_scan, "Forge", operator_state)
    exported = app.export_packet(run, False, clean_scan, "Forge", approved[13], "stale text from prior blocked run")

    assert exported[13]["provider_state"] == "exported"
    assert exported[13]["export"] == "clear"
    assert "st3gg_override_reason" not in exported[13]


def test_export_blocks_without_checkpoint() -> None:
    result = app.run_weave(
        "gothic patent leather platform boots, crimson hardware",
        "Strict",
        "Wan2.2 I2V",
        False,
        None,
        "Forge",
    )
    clean_scan = {"status": "pass", "export_gate": "clear", "findings": [], "purification_actions": []}

    blocked = app.export_packet(result[13], False, clean_scan, "Forge", result[15])

    assert blocked[13]["provider_state"] == "blocked"
    assert "checkpoint" in blocked[13]["message"].lower()


def test_reference_scan_cannot_clear_blocked_generated_artifact() -> None:
    base = app.ROOT / "outputs" / "test-app-callbacks"
    base.mkdir(parents=True, exist_ok=True)
    blocked_artifact = base / "blocked-generated.png"
    blocked_artifact.write_bytes(
        b"\x89PNG\r\n\x1a\n"
        b"\x00\x00\x00\rIEND\xaeB`\x82"
        b"NEXUS_TRAILING_PAYLOAD"
    )
    clean_reference = base / "clean-reference.png"
    clean_reference.write_bytes(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIEND\xaeB`\x82")
    run = build_command_center_run("gothic patent leather platform boots")
    state = {
        "provider_state": "checkpointed",
        "checkpoint": "pending_review",
        "export": "blocked",
        "generation": {"status": "success", "output_path": str(blocked_artifact)},
        "generated_scan": app.scan_file(str(blocked_artifact)),
    }

    scanned = app.scan_reference(run, False, str(clean_reference), "Forge", state)
    assert scanned[13]["reference_scan"]["export_gate"] == "clear"
    assert scanned[13]["export"] == "blocked"
    assert scanned[17]["export_gate"] == "blocked"

    approved = app.approve_checkpoint(run, False, scanned[17], "Forge", scanned[13])
    assert approved[13]["provider_state"] == "checkpointed"
    assert approved[13]["export"] == "blocked"


def test_blocked_reference_scan_does_not_block_clear_generated_artifact() -> None:
    base = app.ROOT / "outputs" / "test-app-callbacks"
    base.mkdir(parents=True, exist_ok=True)
    generated = base / "clear-generated.png"
    generated.write_bytes(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIEND\xaeB`\x82")
    blocked_reference = base / "blocked-reference.png"
    blocked_reference.write_bytes(
        b"\x89PNG\r\n\x1a\n"
        b"\x00\x00\x00\rIEND\xaeB`\x82"
        b"NEXUS_TRAILING_PAYLOAD"
    )
    run = build_command_center_run("gothic patent leather platform boots")
    state = {
        "provider_state": "checkpointed",
        "checkpoint": "pending_review",
        "export": "clear",
        "generation": {"status": "success", "output_path": str(generated)},
        "generated_scan": app.scan_file(str(generated)),
    }

    scanned = app.scan_reference(run, False, str(blocked_reference), "Forge", state)
    assert scanned[13]["reference_scan"]["export_gate"] == "blocked"
    assert scanned[13]["export"] == "clear"
    assert scanned[17]["export_gate"] == "clear"

    approved = app.approve_checkpoint(run, False, scanned[17], "Forge", scanned[13])
    assert approved[13]["provider_state"] == "export_ready"
    assert approved[13]["export"] == "clear"


def test_checkpoint_blocks_without_generated_artifact() -> None:
    result = app.run_weave(
        "gothic patent leather platform boots, crimson hardware",
        "Strict",
        "Wan2.2 I2V",
        False,
        None,
        "Forge",
    )
    clean_scan = {"status": "pass", "export_gate": "clear", "findings": [], "purification_actions": []}

    blocked = app.approve_checkpoint(result[13], False, clean_scan, "Forge", result[15])

    assert blocked[13]["provider_state"] == "blocked"
    assert "no generated artifact" in blocked[13]["message"].lower()


# --- _safe_file_hash tests ---

def test_safe_file_hash_returns_sha256_and_size_for_valid_file() -> None:
    import hashlib
    from pathlib import Path

    path = app.ROOT / "outputs" / "test-hash-target.png"
    path.parent.mkdir(parents=True, exist_ok=True)
    data = b"\x89PNG\r\n\x1a\n" + b"\x00" * 32
    path.write_bytes(data)

    file_hash, size = app._safe_file_hash(str(path))

    assert file_hash == hashlib.sha256(data).hexdigest()
    assert size == len(data)


def test_safe_file_hash_returns_none_for_none_path() -> None:
    assert app._safe_file_hash(None) == (None, None)


def test_safe_file_hash_returns_none_for_empty_string() -> None:
    assert app._safe_file_hash("") == (None, None)


def test_safe_file_hash_returns_none_for_missing_file() -> None:
    result = app._safe_file_hash("/nonexistent/path/no-such-file.png")
    assert result == (None, None)


def test_safe_file_hash_hash_is_64_hex_chars() -> None:
    from pathlib import Path

    path = app.ROOT / "outputs" / "test-hash-hex.png"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(b"\x89PNG\r\n\x1a\n" + b"\x01" * 8)

    file_hash, _ = app._safe_file_hash(str(path))

    assert file_hash is not None
    assert len(file_hash) == 64
    assert all(c in "0123456789abcdef" for c in file_hash)


# --- _safe_reference_url_metadata tests ---

def test_safe_reference_url_metadata_returns_none_for_falsy() -> None:
    assert app._safe_reference_url_metadata(None) is None
    assert app._safe_reference_url_metadata("") is None


def test_safe_reference_url_metadata_returns_metadata_for_valid_https_url() -> None:
    result = app._safe_reference_url_metadata("https://shop.example.test/item/123")

    assert result is not None
    assert result["status"] == "metadata_only"
    assert result["domain"] == "shop.example.test"
    assert "url_hash" in result
    assert len(result["url_hash"]) == 64


def test_safe_reference_url_metadata_returns_metadata_for_valid_http_url() -> None:
    result = app._safe_reference_url_metadata("http://example.test/path?q=1")

    assert result is not None
    assert result["status"] == "metadata_only"
    assert result["domain"] == "example.test"


def test_safe_reference_url_metadata_rejects_file_scheme() -> None:
    result = app._safe_reference_url_metadata("file:///etc/passwd")

    assert result is not None
    assert result["status"] == "invalid_url"


def test_safe_reference_url_metadata_rejects_ftp_scheme() -> None:
    result = app._safe_reference_url_metadata("ftp://ftp.example.test/file.txt")

    assert result is not None
    assert result["status"] == "invalid_url"


def test_safe_reference_url_metadata_rejects_missing_host() -> None:
    result = app._safe_reference_url_metadata("http:///no-host")

    assert result is not None
    assert result["status"] == "invalid_url"


def test_safe_reference_url_metadata_domain_is_lowercase() -> None:
    result = app._safe_reference_url_metadata("https://SHOP.EXAMPLE.TEST/Item/123")

    assert result is not None
    assert result["domain"] == "shop.example.test"


def test_safe_reference_url_metadata_url_hash_is_stable() -> None:
    url = "https://shop.example.test/stable-hash-test"
    first = app._safe_reference_url_metadata(url)
    second = app._safe_reference_url_metadata(url)

    assert first is not None and second is not None
    assert first["url_hash"] == second["url_hash"]


def test_safe_reference_url_metadata_different_urls_produce_different_hashes() -> None:
    url_a = "https://shop.example.test/item/1"
    url_b = "https://shop.example.test/item/2"

    result_a = app._safe_reference_url_metadata(url_a)
    result_b = app._safe_reference_url_metadata(url_b)

    assert result_a is not None and result_b is not None
    assert result_a["url_hash"] != result_b["url_hash"]


# --- _reference_metadata tests ---

def test_reference_metadata_returns_empty_for_no_inputs() -> None:
    scan = {"status": "pass", "export_gate": "clear"}
    result = app._reference_metadata(None, None, scan)

    assert result == []


def test_reference_metadata_includes_file_record_when_uploaded() -> None:
    from pathlib import Path

    path = app.ROOT / "outputs" / "test-ref-meta.png"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(b"\x89PNG\r\n\x1a\n" + b"\x00" * 16)
    scan = {"status": "pass", "export_gate": "clear", "magic": "PNG", "extension": ".png"}

    result = app._reference_metadata(str(path), None, scan)

    assert len(result) == 1
    assert result[0]["source"] == "upload"
    assert result[0]["basename"] == "test-ref-meta.png"
    assert result[0]["sha256"] is not None
    assert result[0]["st3gg_status"] == "pass"
    assert result[0]["export_gate"] == "clear"
    assert result[0]["magic"] == "PNG"
    assert result[0]["extension"] == ".png"


def test_reference_metadata_includes_url_record_when_url_given() -> None:
    scan = {}
    result = app._reference_metadata(None, "https://shop.example.test/item/99", scan)

    assert len(result) == 1
    assert result[0]["source"] == "url"
    assert result[0]["domain"] == "shop.example.test"


def test_reference_metadata_includes_both_file_and_url() -> None:
    from pathlib import Path

    path = app.ROOT / "outputs" / "test-ref-both.png"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIEND\xaeB`\x82")
    scan = {"status": "pass", "export_gate": "clear"}

    result = app._reference_metadata(str(path), "https://shop.example.test/item/77", scan)

    assert len(result) == 2
    sources = {r["source"] for r in result}
    assert sources == {"upload", "url"}


def test_reference_metadata_size_bytes_is_integer() -> None:
    from pathlib import Path

    data = b"\x89PNG\r\n\x1a\n" + b"\x00" * 24
    path = app.ROOT / "outputs" / "test-ref-size.png"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(data)
    scan = {}

    result = app._reference_metadata(str(path), None, scan)

    assert result[0]["size_bytes"] == len(data)


# --- _creator_controls tests ---

def test_creator_controls_returns_dict_with_wardrobe_and_generation() -> None:
    result = app._creator_controls("Strict", "Wan2.2 I2V")

    assert "wardrobe" in result
    assert "generation" in result
    assert result["reasoning_mode"] == "Strict"
    assert result["video_preset"] == "Wan2.2 I2V"


def test_creator_controls_default_wardrobe_values() -> None:
    result = app._creator_controls("Strict", "Wan2.2 I2V")
    wardrobe = result["wardrobe"]

    assert wardrobe["footwear"] == "platform boots"
    assert wardrobe["outerwear"] == "black patent leather long coat"
    assert wardrobe["palette"] == "black, crimson, cyan neon"


def test_creator_controls_overrides_wardrobe_slots() -> None:
    result = app._creator_controls(
        "Frontier",
        "LTX-2.3",
        footwear="patent leather heels",
        outerwear="faux fur collar coat",
        palette="obsidian, pearl, crimson",
    )
    wardrobe = result["wardrobe"]

    assert wardrobe["footwear"] == "patent leather heels"
    assert wardrobe["outerwear"] == "faux fur collar coat"
    assert wardrobe["palette"] == "obsidian, pearl, crimson"


def test_creator_controls_locked_slots_present() -> None:
    result = app._creator_controls("Strict", "Wan2.2 I2V")
    wardrobe = result["wardrobe"]

    assert "locked_slots" in wardrobe
    assert isinstance(wardrobe["locked_slots"], list)
    assert "outerwear" in wardrobe["locked_slots"]


def test_creator_controls_generation_specifies_flux_primary() -> None:
    result = app._creator_controls("Strict", "Wan2.2 I2V")
    generation = result["generation"]

    assert "black-forest-labs/FLUX.2-klein-9B" in generation["flux_primary"]
    assert "black-forest-labs/FLUX.2-klein-4B" in generation["flux_sidecar"]


def test_creator_controls_generation_persists_seed_style_and_aspect() -> None:
    result = app._creator_controls(
        "Strict",
        "Wan2.2 I2V",
        seed=123,
        style_strength="Cinematic",
        aspect="Square",
    )
    generation = result["generation"]

    assert generation["seed"] == 123
    assert generation["style_strength"] == "Cinematic"
    assert generation["aspect"] == "Square"


# --- _prompt_with_controls tests ---

def test_prompt_with_controls_appends_wardrobe_suffix() -> None:
    controls = app._creator_controls("Strict", "Wan2.2 I2V", footwear="platform boots")
    result = app._prompt_with_controls("gothic couture portrait", controls)

    assert result.startswith("gothic couture portrait")
    assert "Wardrobe controls:" in result
    assert "platform boots" in result
    assert "Style direction:" in result


def test_prompt_with_controls_returns_prompt_unchanged_when_no_wardrobe() -> None:
    # A controls dict with no wardrobe key
    result = app._prompt_with_controls("gothic couture portrait", {})

    assert result == "gothic couture portrait"


def test_prompt_with_controls_includes_all_wardrobe_fields() -> None:
    controls = app._creator_controls(
        "Strict",
        "Wan2.2 I2V",
        silhouette="structured long coat",
        outerwear="black patent leather long coat",
        upper_body="Chantilly lace neckline",
        footwear="platform boots",
        palette="black, crimson, cyan neon",
        hardware="crimson hardware",
    )
    result = app._prompt_with_controls("brief", controls)

    assert "structured long coat" in result
    assert "black patent leather long coat" in result
    assert "Chantilly lace neckline" in result
    assert "platform boots" in result
    assert "crimson hardware" in result


def test_generation_dimensions_support_portrait_and_square() -> None:
    assert app._generation_dimensions("Portrait") == (832, 1216)
    assert app._generation_dimensions("Square") == (1024, 1024)
    assert app._generation_dimensions("unknown") == (832, 1216)


def test_resolve_seed_randomizes_negative_and_uses_explicit_seed() -> None:
    assert app._resolve_seed(42) == 42
    assert app._resolve_seed("42") == 42
    assert 0 <= app._resolve_seed(-1) < 1_000_000_000


def test_button_updates_unlock_checkpoint_then_export() -> None:
    state = {
        "provider_state": "generated",
        "checkpoint": "pending_review",
        "generation": {"status": "success", "output_path": "outputs/runtime/test.png"},
    }
    checkpoint, export, stop = app._button_updates(None, state)
    assert checkpoint["interactive"] is True
    assert export["interactive"] is False
    assert stop["interactive"] is False

    approved_state = {**state, "checkpoint": "approved"}
    checkpoint, export, stop = app._button_updates(None, approved_state)
    assert checkpoint["interactive"] is False
    assert export["interactive"] is True
    assert stop["interactive"] is False


def test_prompt_with_controls_does_not_duplicate_prompt() -> None:
    controls = app._creator_controls("Strict", "Wan2.2 I2V")
    prompt = "gothic couture brief"
    result = app._prompt_with_controls(prompt, controls)

    assert result.count(prompt) == 1


# --- _generated_output_path tests ---

def test_generated_output_path_returns_none_when_no_state() -> None:
    assert app._generated_output_path(None) is None


def test_generated_output_path_returns_none_when_no_generation_key() -> None:
    assert app._generated_output_path({"provider_state": "idle"}) is None


def test_generated_output_path_returns_none_when_no_output_path() -> None:
    state = {"generation": {"status": "disabled"}}
    assert app._generated_output_path(state) is None


def test_generated_output_path_returns_string_when_present() -> None:
    state = {"generation": {"output_path": "/data/artifact.png"}}
    result = app._generated_output_path(state)

    assert result == "/data/artifact.png"


def test_generated_output_path_returns_none_for_empty_path() -> None:
    state = {"generation": {"output_path": ""}}
    assert app._generated_output_path(state) is None


# --- _authoritative_generated_scan tests ---

def test_authoritative_generated_scan_returns_default_for_no_state() -> None:
    result = app._authoritative_generated_scan(None)

    assert isinstance(result, dict)
    assert "status" in result


def test_authoritative_generated_scan_scans_output_path_when_present() -> None:
    from pathlib import Path

    path = app.ROOT / "outputs" / "test-auth-scan.png"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIEND\xaeB`\x82")
    state = {"generation": {"output_path": str(path)}}

    result = app._authoritative_generated_scan(state)

    assert result["status"] in {"pass", "review"}
    assert "export_gate" in result


def test_authoritative_generated_scan_falls_back_to_stored_scan() -> None:
    stored_scan = {"status": "review", "export_gate": "blocked", "findings": ["trailing data"]}
    state = {"generated_scan": stored_scan}

    result = app._authoritative_generated_scan(state)

    assert result == stored_scan


def test_authoritative_generated_scan_uses_output_path_over_stored_scan() -> None:
    from pathlib import Path

    path = app.ROOT / "outputs" / "test-auth-scan-prefer.png"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIEND\xaeB`\x82")
    stale_scan = {"status": "review", "export_gate": "blocked"}
    state = {
        "generation": {"output_path": str(path)},
        "generated_scan": stale_scan,
    }

    result = app._authoritative_generated_scan(state)

    # Should re-scan from file, not use stored scan
    assert result["status"] == "pass"
    assert result["export_gate"] == "clear"


def test_export_packet_allows_blocked_st3gg_with_explicit_override(monkeypatch) -> None:
    monkeypatch.setenv("NEXUS_EXPORT_DIR", "outputs/test-exports")
    run = build_command_center_run("gothic patent leather platform boots")
    blocked_scan = {
        "status": "review",
        "export_gate": "blocked",
        "findings": ["metadata review"],
        "purification_actions": ["strip metadata"],
    }
    state = {
        **app._default_operator_state(),
        "checkpoint": "approved",
        "provider_state": "generated",
        "generated_scan": blocked_scan,
        "generation": {
            "status": "success",
            "provider_state": "generated",
            "output_path": "outputs/runtime/nexus_flux_test.png",
            "lora_status": "skipped_incompatible",
        },
    }

    blocked = app.export_packet(run, False, blocked_scan, "Forge", state, "")
    assert blocked[13]["provider_state"] == "blocked"
    assert "override reason" in blocked[13]["message"].lower()

    exported = app.export_packet(run, False, blocked_scan, "Forge", state, "Operator reviewed ST3GG and wrote audit evidence only.")
    assert exported[13]["provider_state"] == "exported"
    assert exported[13]["export"] == "override"
    assert "export_packet" in exported[13]