pixelsrc 0.2.0

Pixelsrc - GenAI-native pixel art format and compiler
Documentation
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
//! Project initialization for pixelsrc
//!
//! Provides scaffolding for new pixelsrc projects with various presets.

use std::fs;
use std::path::Path;
use thiserror::Error;

use crate::templates::justfile::{generate_justfile, JustfileTemplate};

/// Error during project initialization
#[derive(Debug, Error)]
pub enum InitError {
    /// Directory already exists
    #[error("Directory already exists: {0}")]
    DirectoryExists(String),
    /// Failed to create directory
    #[error("Failed to create directory: {0}")]
    CreateDir(std::io::Error),
    /// Failed to write file
    #[error("Failed to write file: {0}")]
    WriteFile(std::io::Error),
    /// Unknown preset
    #[error("Unknown preset '{0}'. Available: minimal, artist, animator, game")]
    UnknownPreset(String),
}

/// Available project presets
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Preset {
    /// Single sprite, basic render
    Minimal,
    /// Static art workflow with palettes, sprites, variants
    Artist,
    /// Animation workflow with sprites, animations, justfile
    Animator,
    /// Full game asset pipeline with atlases, multi-export
    Game,
}

impl Preset {
    /// Parse preset name from string
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "minimal" => Some(Preset::Minimal),
            "artist" => Some(Preset::Artist),
            "animator" => Some(Preset::Animator),
            "game" => Some(Preset::Game),
            _ => None,
        }
    }
}

/// Initialize a new pixelsrc project.
///
/// Creates the project directory structure and starter files based on the
/// selected preset.
///
/// # Arguments
/// - `path` - Directory to create the project in
/// - `name` - Project name (used in pxl.toml)
/// - `preset` - Preset template name ("minimal", "artist", "animator", "game")
///
/// # Returns
/// - `Ok(())` on success
/// - `Err(InitError)` if initialization fails
///
/// # Example
/// ```ignore
/// init_project(Path::new("my-game"), "my-game", "minimal")?;
/// ```
pub fn init_project(path: &Path, name: &str, preset: &str) -> Result<(), InitError> {
    let preset =
        Preset::from_str(preset).ok_or_else(|| InitError::UnknownPreset(preset.to_string()))?;

    // Check if directory already exists and is not empty
    if path.exists() {
        let is_empty = path.read_dir().map(|mut d| d.next().is_none()).unwrap_or(false);
        if !is_empty {
            return Err(InitError::DirectoryExists(path.display().to_string()));
        }
    }

    // Create project based on preset
    match preset {
        Preset::Minimal => init_minimal(path, name),
        Preset::Artist => init_artist(path, name),
        Preset::Animator => init_animator(path, name),
        Preset::Game => init_game(path, name),
    }
}

/// Initialize a minimal project structure.
fn init_minimal(path: &Path, name: &str) -> Result<(), InitError> {
    // Create directory structure
    create_dir(path)?;
    create_dir(&path.join("src/pxl/palettes"))?;
    create_dir(&path.join("src/pxl/sprites"))?;
    create_dir(&path.join("build"))?;

    // Write pxl.toml
    let pxl_toml = generate_minimal_config(name);
    write_file(&path.join("pxl.toml"), &pxl_toml)?;

    // Write .gitignore
    let gitignore = generate_gitignore();
    write_file(&path.join(".gitignore"), &gitignore)?;

    // Write justfile
    let justfile = generate_justfile(JustfileTemplate::Minimal, name);
    write_file(&path.join("justfile"), &justfile)?;

    // Write example palette
    let palette = generate_main_palette();
    write_file(&path.join("src/pxl/palettes/main.pxl"), &palette)?;

    // Write example sprite
    let sprite = generate_example_sprite();
    write_file(&path.join("src/pxl/sprites/example.pxl"), &sprite)?;

    // Write build/.gitkeep
    write_file(&path.join("build/.gitkeep"), "")?;

    Ok(())
}

/// Create a directory and all parent directories.
fn create_dir(path: &Path) -> Result<(), InitError> {
    fs::create_dir_all(path).map_err(InitError::CreateDir)
}

/// Write content to a file.
fn write_file(path: &Path, content: &str) -> Result<(), InitError> {
    fs::write(path, content).map_err(InitError::WriteFile)
}

/// Generate minimal pxl.toml configuration.
fn generate_minimal_config(name: &str) -> String {
    format!(
        r#"[project]
name = "{}"
version = "0.1.0"
"#,
        name
    )
}

/// Generate .gitignore content.
fn generate_gitignore() -> String {
    r#"# Pixelsrc build output
build/

# OS files
.DS_Store
Thumbs.db
"#
    .to_string()
}

/// Generate main palette file content.
fn generate_main_palette() -> String {
    r##"{"type": "palette", "name": "main", "colors": {"{_}": "#00000000", "{black}": "#000000", "{white}": "#FFFFFF", "{primary}": "#4A90D9", "{secondary}": "#D94A4A"}}"##.to_string()
}

/// Generate example sprite file content.
fn generate_example_sprite() -> String {
    r#"{"type": "sprite", "name": "example", "palette": "main", "grid": [
  "{_}{primary}{primary}{_}",
  "{primary}{white}{white}{primary}",
  "{primary}{white}{white}{primary}",
  "{_}{primary}{primary}{_}"
]}"#
    .to_string()
}

// ============================================================================
// Artist Preset
// ============================================================================

/// Initialize an artist project structure.
///
/// Static art workflow with palettes, sprites, and variant support.
fn init_artist(path: &Path, name: &str) -> Result<(), InitError> {
    // Create directory structure
    create_dir(path)?;
    create_dir(&path.join("src/pxl/palettes"))?;
    create_dir(&path.join("src/pxl/sprites"))?;
    create_dir(&path.join("src/pxl/variants"))?;
    create_dir(&path.join("build"))?;

    // Write pxl.toml
    let pxl_toml = generate_artist_config(name);
    write_file(&path.join("pxl.toml"), &pxl_toml)?;

    // Write .gitignore
    let gitignore = generate_gitignore();
    write_file(&path.join(".gitignore"), &gitignore)?;

    // Write justfile
    let justfile = generate_justfile(JustfileTemplate::Artist, name);
    write_file(&path.join("justfile"), &justfile)?;

    // Write palettes
    let main_palette = generate_main_palette();
    write_file(&path.join("src/pxl/palettes/main.pxl"), &main_palette)?;

    let alt_palette = generate_alt_palette();
    write_file(&path.join("src/pxl/palettes/alt.pxl"), &alt_palette)?;

    // Write example sprite
    let sprite = generate_character_sprite();
    write_file(&path.join("src/pxl/sprites/character.pxl"), &sprite)?;

    // Write variant example
    let variant = generate_variant_example();
    write_file(&path.join("src/pxl/variants/character_alt.pxl"), &variant)?;

    // Write build/.gitkeep
    write_file(&path.join("build/.gitkeep"), "")?;

    Ok(())
}

/// Generate artist preset pxl.toml configuration.
fn generate_artist_config(name: &str) -> String {
    format!(
        r#"[project]
name = "{}"
version = "0.1.0"

[defaults]
scale = 1
padding = 0

[validate]
strict = false
unused_palettes = "warn"
"#,
        name
    )
}

/// Generate alternate palette file content.
fn generate_alt_palette() -> String {
    r##"{"type": "palette", "name": "alt", "colors": {"{_}": "#00000000", "{black}": "#1a1a2e", "{white}": "#eaeaea", "{primary}": "#e94560", "{secondary}": "#533483"}}"##.to_string()
}

/// Generate character sprite file content.
fn generate_character_sprite() -> String {
    r#"{"type": "sprite", "name": "character", "palette": "main", "grid": [
  "{_}{_}{primary}{primary}{_}{_}",
  "{_}{primary}{white}{white}{primary}{_}",
  "{primary}{white}{black}{black}{white}{primary}",
  "{primary}{white}{white}{white}{white}{primary}",
  "{_}{primary}{secondary}{secondary}{primary}{_}",
  "{_}{_}{primary}{primary}{_}{_}"
]}"#
    .to_string()
}

/// Generate variant example file content.
fn generate_variant_example() -> String {
    r#"{"type": "sprite", "name": "character_alt", "palette": "alt", "grid": [
  "{_}{_}{primary}{primary}{_}{_}",
  "{_}{primary}{white}{white}{primary}{_}",
  "{primary}{white}{black}{black}{white}{primary}",
  "{primary}{white}{white}{white}{white}{primary}",
  "{_}{primary}{secondary}{secondary}{primary}{_}",
  "{_}{_}{primary}{primary}{_}{_}"
]}"#
    .to_string()
}

// ============================================================================
// Animator Preset
// ============================================================================

/// Initialize an animator project structure.
///
/// Animation workflow with sprites, animations, and justfile with GIF recipes.
fn init_animator(path: &Path, name: &str) -> Result<(), InitError> {
    // Create directory structure
    create_dir(path)?;
    create_dir(&path.join("src/pxl/palettes"))?;
    create_dir(&path.join("src/pxl/sprites"))?;
    create_dir(&path.join("src/pxl/animations"))?;
    create_dir(&path.join("build/preview"))?;

    // Write pxl.toml
    let pxl_toml = generate_animator_config(name);
    write_file(&path.join("pxl.toml"), &pxl_toml)?;

    // Write .gitignore
    let gitignore = generate_gitignore();
    write_file(&path.join(".gitignore"), &gitignore)?;

    // Write justfile
    let justfile = generate_justfile(JustfileTemplate::Animator, name);
    write_file(&path.join("justfile"), &justfile)?;

    // Write palette
    let palette = generate_main_palette();
    write_file(&path.join("src/pxl/palettes/main.pxl"), &palette)?;

    // Write animation frames
    let frames = generate_animation_frames();
    write_file(&path.join("src/pxl/sprites/walk.pxl"), &frames)?;

    // Write animation definition
    let animation = generate_animation_definition();
    write_file(&path.join("src/pxl/animations/walk.pxl"), &animation)?;

    // Write build/.gitkeep
    write_file(&path.join("build/.gitkeep"), "")?;

    Ok(())
}

/// Generate animator preset pxl.toml configuration.
fn generate_animator_config(name: &str) -> String {
    format!(
        r#"[project]
name = "{}"
version = "0.1.0"

[defaults]
scale = 2
padding = 0

[animations]
sources = ["animations/**"]
preview = true
preview_scale = 4

[watch]
debounce_ms = 100
clear_screen = true
"#,
        name
    )
}

/// Generate animation frame sprites.
fn generate_animation_frames() -> String {
    r##"{"type": "palette", "name": "walk", "colors": {"{_}": "#00000000", "{b}": "#2d2d2d", "{s}": "#4a90d9", "{h}": "#ffcc00"}}
{"type": "sprite", "name": "walk_1", "palette": "walk", "grid": ["{_}{h}{h}{_}", "{_}{s}{s}{_}", "{b}{s}{s}{b}", "{_}{b}{b}{_}"]}
{"type": "sprite", "name": "walk_2", "palette": "walk", "grid": ["{_}{h}{h}{_}", "{_}{s}{s}{_}", "{_}{s}{s}{_}", "{b}{_}{_}{b}"]}
{"type": "sprite", "name": "walk_3", "palette": "walk", "grid": ["{_}{h}{h}{_}", "{_}{s}{s}{_}", "{b}{s}{s}{b}", "{_}{b}{b}{_}"]}
{"type": "sprite", "name": "walk_4", "palette": "walk", "grid": ["{_}{h}{h}{_}", "{_}{s}{s}{_}", "{_}{s}{s}{_}", "{b}{_}{_}{b}"]}"##
        .to_string()
}

/// Generate animation definition.
fn generate_animation_definition() -> String {
    r#"{"type": "include", "path": "../sprites/walk.pxl"}
{"type": "animation", "name": "walk_cycle", "frames": ["walk_1", "walk_2", "walk_3", "walk_4"], "duration": 150}"#
        .to_string()
}

// ============================================================================
// Game Preset
// ============================================================================

/// Initialize a game project structure.
///
/// Full game asset pipeline with atlases, multi-export, and justfile.
fn init_game(path: &Path, name: &str) -> Result<(), InitError> {
    // Create directory structure
    create_dir(path)?;
    create_dir(&path.join("src/pxl/palettes"))?;
    create_dir(&path.join("src/pxl/sprites/player"))?;
    create_dir(&path.join("src/pxl/sprites/items"))?;
    create_dir(&path.join("src/pxl/animations"))?;
    create_dir(&path.join("src/pxl/ui"))?;
    create_dir(&path.join("build/atlases"))?;
    create_dir(&path.join("build/preview"))?;

    // Write pxl.toml
    let pxl_toml = generate_game_config(name);
    write_file(&path.join("pxl.toml"), &pxl_toml)?;

    // Write .gitignore
    let gitignore = generate_gitignore();
    write_file(&path.join(".gitignore"), &gitignore)?;

    // Write justfile
    let justfile = generate_justfile(JustfileTemplate::Game, name);
    write_file(&path.join("justfile"), &justfile)?;

    // Write palettes
    let main_palette = generate_main_palette();
    write_file(&path.join("src/pxl/palettes/main.pxl"), &main_palette)?;

    let ui_palette = generate_ui_palette();
    write_file(&path.join("src/pxl/palettes/ui.pxl"), &ui_palette)?;

    // Write player sprites
    let player = generate_player_sprites();
    write_file(&path.join("src/pxl/sprites/player/idle.pxl"), &player)?;

    // Write item sprites
    let items = generate_item_sprites();
    write_file(&path.join("src/pxl/sprites/items/coin.pxl"), &items)?;

    // Write animation
    let animation = generate_player_animation();
    write_file(&path.join("src/pxl/animations/player.pxl"), &animation)?;

    // Write UI elements
    let ui = generate_ui_elements();
    write_file(&path.join("src/pxl/ui/button.pxl"), &ui)?;

    // Write .gitkeep files
    write_file(&path.join("build/.gitkeep"), "")?;

    Ok(())
}

/// Generate game preset pxl.toml configuration.
fn generate_game_config(name: &str) -> String {
    format!(
        r#"[project]
name = "{}"
version = "0.1.0"
src = "src/pxl"
out = "build"

[defaults]
scale = 1
padding = 1

[atlases.sprites]
sources = ["sprites/**"]
max_size = [1024, 1024]
padding = 1
power_of_two = true

[atlases.ui]
sources = ["ui/**"]
max_size = [512, 512]
padding = 0

[animations]
sources = ["animations/**"]
preview = true
preview_scale = 4
sheet_layout = "horizontal"

[export.generic]
enabled = true

[validate]
strict = true
unused_palettes = "warn"
missing_refs = "error"

[watch]
debounce_ms = 100
clear_screen = true
"#,
        name
    )
}

/// Generate UI palette file content.
fn generate_ui_palette() -> String {
    r##"{"type": "palette", "name": "ui", "colors": {"{_}": "#00000000", "{bg}": "#1a1a2e", "{border}": "#4a4a6a", "{text}": "#ffffff", "{accent}": "#e94560"}}"##.to_string()
}

/// Generate player sprites.
fn generate_player_sprites() -> String {
    r#"{"type": "sprite", "name": "player_idle_1", "palette": "main", "grid": [
  "{_}{primary}{primary}{_}",
  "{primary}{white}{white}{primary}",
  "{_}{primary}{primary}{_}",
  "{_}{black}{black}{_}"
]}
{"type": "sprite", "name": "player_idle_2", "palette": "main", "grid": [
  "{_}{primary}{primary}{_}",
  "{primary}{white}{white}{primary}",
  "{_}{primary}{primary}{_}",
  "{black}{_}{_}{black}"
]}"#
    .to_string()
}

/// Generate item sprites.
fn generate_item_sprites() -> String {
    r##"{"type": "palette", "name": "items", "colors": {"{_}": "#00000000", "{gold}": "#FFD700", "{shine}": "#FFFFE0"}}
{"type": "sprite", "name": "coin", "palette": "items", "grid": ["{_}{gold}{gold}{_}", "{gold}{shine}{gold}{gold}", "{gold}{gold}{shine}{gold}", "{_}{gold}{gold}{_}"]}"##
        .to_string()
}

/// Generate player animation.
fn generate_player_animation() -> String {
    r#"{"type": "include", "path": "../sprites/player/idle.pxl"}
{"type": "animation", "name": "player_idle", "frames": ["player_idle_1", "player_idle_2"], "duration": 500}"#
        .to_string()
}

/// Generate UI elements.
fn generate_ui_elements() -> String {
    r#"{"type": "sprite", "name": "button_normal", "palette": "ui", "grid": [
  "{border}{border}{border}{border}{border}{border}",
  "{border}{bg}{bg}{bg}{bg}{border}",
  "{border}{bg}{text}{text}{bg}{border}",
  "{border}{bg}{bg}{bg}{bg}{border}",
  "{border}{border}{border}{border}{border}{border}"
]}
{"type": "sprite", "name": "button_hover", "palette": "ui", "grid": [
  "{accent}{accent}{accent}{accent}{accent}{accent}",
  "{accent}{bg}{bg}{bg}{bg}{accent}",
  "{accent}{bg}{text}{text}{bg}{accent}",
  "{accent}{bg}{bg}{bg}{bg}{accent}",
  "{accent}{accent}{accent}{accent}{accent}{accent}"
]}"#
    .to_string()
}

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

    #[test]
    fn test_preset_from_str() {
        assert_eq!(Preset::from_str("minimal"), Some(Preset::Minimal));
        assert_eq!(Preset::from_str("MINIMAL"), Some(Preset::Minimal));
        assert_eq!(Preset::from_str("artist"), Some(Preset::Artist));
        assert_eq!(Preset::from_str("animator"), Some(Preset::Animator));
        assert_eq!(Preset::from_str("game"), Some(Preset::Game));
        assert_eq!(Preset::from_str("unknown"), None);
    }

    #[test]
    fn test_init_minimal_creates_structure() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("test-project");

        init_project(&project_path, "test-project", "minimal").unwrap();

        // Check directories exist
        assert!(project_path.join("src/pxl/palettes").exists());
        assert!(project_path.join("src/pxl/sprites").exists());
        assert!(project_path.join("build").exists());

        // Check files exist
        assert!(project_path.join("pxl.toml").exists());
        assert!(project_path.join(".gitignore").exists());
        assert!(project_path.join("justfile").exists());
        assert!(project_path.join("src/pxl/palettes/main.pxl").exists());
        assert!(project_path.join("src/pxl/sprites/example.pxl").exists());
        assert!(project_path.join("build/.gitkeep").exists());
    }

    #[test]
    fn test_init_minimal_justfile_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("test");

        init_project(&project_path, "test", "minimal").unwrap();

        let justfile = fs::read_to_string(project_path.join("justfile")).unwrap();
        assert!(justfile.contains("render:"));
        assert!(justfile.contains("validate:"));
        assert!(justfile.contains("clean:"));
    }

    #[test]
    fn test_init_minimal_config_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("my-game");

        init_project(&project_path, "my-game", "minimal").unwrap();

        let config = fs::read_to_string(project_path.join("pxl.toml")).unwrap();
        assert!(config.contains("name = \"my-game\""));
        assert!(config.contains("version = \"0.1.0\""));
    }

    #[test]
    fn test_init_minimal_palette_valid_json() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("test");

        init_project(&project_path, "test", "minimal").unwrap();

        let palette = fs::read_to_string(project_path.join("src/pxl/palettes/main.pxl")).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&palette).unwrap();
        assert_eq!(parsed["type"], "palette");
        assert_eq!(parsed["name"], "main");
    }

    #[test]
    fn test_init_minimal_sprite_valid_json() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("test");

        init_project(&project_path, "test", "minimal").unwrap();

        let sprite = fs::read_to_string(project_path.join("src/pxl/sprites/example.pxl")).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&sprite).unwrap();
        assert_eq!(parsed["type"], "sprite");
        assert_eq!(parsed["name"], "example");
        assert_eq!(parsed["palette"], "main");
    }

    #[test]
    fn test_init_gitignore_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("test");

        init_project(&project_path, "test", "minimal").unwrap();

        let gitignore = fs::read_to_string(project_path.join(".gitignore")).unwrap();
        assert!(gitignore.contains("build/"));
        assert!(gitignore.contains(".DS_Store"));
    }

    #[test]
    fn test_init_existing_non_empty_dir_fails() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("existing");

        // Create a non-empty directory
        fs::create_dir_all(&project_path).unwrap();
        fs::write(project_path.join("some-file.txt"), "content").unwrap();

        let result = init_project(&project_path, "existing", "minimal");
        assert!(matches!(result, Err(InitError::DirectoryExists(_))));
    }

    #[test]
    fn test_init_existing_empty_dir_succeeds() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("empty");

        // Create an empty directory
        fs::create_dir_all(&project_path).unwrap();

        let result = init_project(&project_path, "empty", "minimal");
        assert!(result.is_ok());
    }

    #[test]
    fn test_init_unknown_preset_fails() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("test");

        let result = init_project(&project_path, "test", "nonexistent");
        assert!(matches!(result, Err(InitError::UnknownPreset(_))));
    }

    #[test]
    fn test_init_in_current_dir() {
        let temp = TempDir::new().unwrap();

        // Initialize in the temp directory itself (simulating "pxl init .")
        init_project(temp.path(), "current", "minimal").unwrap();

        assert!(temp.path().join("pxl.toml").exists());
        assert!(temp.path().join("src/pxl").exists());
    }

    // ========================================================================
    // Artist Preset Tests
    // ========================================================================

    #[test]
    fn test_init_artist_creates_structure() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("art-project");

        init_project(&project_path, "art-project", "artist").unwrap();

        // Check directories
        assert!(project_path.join("src/pxl/palettes").exists());
        assert!(project_path.join("src/pxl/sprites").exists());
        assert!(project_path.join("src/pxl/variants").exists());
        assert!(project_path.join("build").exists());

        // Check files
        assert!(project_path.join("pxl.toml").exists());
        assert!(project_path.join(".gitignore").exists());
        assert!(project_path.join("justfile").exists());
        assert!(project_path.join("src/pxl/palettes/main.pxl").exists());
        assert!(project_path.join("src/pxl/palettes/alt.pxl").exists());
        assert!(project_path.join("src/pxl/sprites/character.pxl").exists());
        assert!(project_path.join("src/pxl/variants/character_alt.pxl").exists());
    }

    #[test]
    fn test_init_artist_justfile_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("art");

        init_project(&project_path, "art", "artist").unwrap();

        let justfile = fs::read_to_string(project_path.join("justfile")).unwrap();
        assert!(justfile.contains("render:"));
        assert!(justfile.contains("variants:"));
        assert!(justfile.contains("scales:"));
        assert!(justfile.contains("clean:"));
    }

    #[test]
    fn test_init_artist_config_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("art");

        init_project(&project_path, "art", "artist").unwrap();

        let config = fs::read_to_string(project_path.join("pxl.toml")).unwrap();
        assert!(config.contains("name = \"art\""));
        assert!(config.contains("[defaults]"));
        assert!(config.contains("[validate]"));
    }

    #[test]
    fn test_init_artist_palettes_valid_json() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("art");

        init_project(&project_path, "art", "artist").unwrap();

        // Check main palette
        let main = fs::read_to_string(project_path.join("src/pxl/palettes/main.pxl")).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&main).unwrap();
        assert_eq!(parsed["type"], "palette");
        assert_eq!(parsed["name"], "main");

        // Check alt palette
        let alt = fs::read_to_string(project_path.join("src/pxl/palettes/alt.pxl")).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&alt).unwrap();
        assert_eq!(parsed["type"], "palette");
        assert_eq!(parsed["name"], "alt");
    }

    // ========================================================================
    // Animator Preset Tests
    // ========================================================================

    #[test]
    fn test_init_animator_creates_structure() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("anim-project");

        init_project(&project_path, "anim-project", "animator").unwrap();

        // Check directories
        assert!(project_path.join("src/pxl/palettes").exists());
        assert!(project_path.join("src/pxl/sprites").exists());
        assert!(project_path.join("src/pxl/animations").exists());
        assert!(project_path.join("build/preview").exists());

        // Check files
        assert!(project_path.join("pxl.toml").exists());
        assert!(project_path.join(".gitignore").exists());
        assert!(project_path.join("justfile").exists());
        assert!(project_path.join("src/pxl/palettes/main.pxl").exists());
        assert!(project_path.join("src/pxl/sprites/walk.pxl").exists());
        assert!(project_path.join("src/pxl/animations/walk.pxl").exists());
    }

    #[test]
    fn test_init_animator_config_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("anim");

        init_project(&project_path, "anim", "animator").unwrap();

        let config = fs::read_to_string(project_path.join("pxl.toml")).unwrap();
        assert!(config.contains("name = \"anim\""));
        assert!(config.contains("[animations]"));
        assert!(config.contains("preview = true"));
        assert!(config.contains("[watch]"));
    }

    #[test]
    fn test_init_animator_justfile_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("anim");

        init_project(&project_path, "anim", "animator").unwrap();

        let justfile = fs::read_to_string(project_path.join("justfile")).unwrap();
        assert!(justfile.contains("preview:"));
        assert!(justfile.contains("--gif"));
        assert!(justfile.contains("watch:"));
    }

    #[test]
    fn test_init_animator_animation_valid() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("anim");

        init_project(&project_path, "anim", "animator").unwrap();

        // Check animation file has include and animation definition
        let anim = fs::read_to_string(project_path.join("src/pxl/animations/walk.pxl")).unwrap();
        assert!(anim.contains("\"type\": \"include\""));
        assert!(anim.contains("\"type\": \"animation\""));
        assert!(anim.contains("walk_cycle"));
    }

    // ========================================================================
    // Game Preset Tests
    // ========================================================================

    #[test]
    fn test_init_game_creates_structure() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("game-project");

        init_project(&project_path, "game-project", "game").unwrap();

        // Check directories
        assert!(project_path.join("src/pxl/palettes").exists());
        assert!(project_path.join("src/pxl/sprites/player").exists());
        assert!(project_path.join("src/pxl/sprites/items").exists());
        assert!(project_path.join("src/pxl/animations").exists());
        assert!(project_path.join("src/pxl/ui").exists());
        assert!(project_path.join("build/atlases").exists());
        assert!(project_path.join("build/preview").exists());

        // Check files
        assert!(project_path.join("pxl.toml").exists());
        assert!(project_path.join(".gitignore").exists());
        assert!(project_path.join("justfile").exists());
        assert!(project_path.join("src/pxl/palettes/main.pxl").exists());
        assert!(project_path.join("src/pxl/palettes/ui.pxl").exists());
        assert!(project_path.join("src/pxl/sprites/player/idle.pxl").exists());
        assert!(project_path.join("src/pxl/sprites/items/coin.pxl").exists());
        assert!(project_path.join("src/pxl/animations/player.pxl").exists());
        assert!(project_path.join("src/pxl/ui/button.pxl").exists());
    }

    #[test]
    fn test_init_game_config_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("game");

        init_project(&project_path, "game", "game").unwrap();

        let config = fs::read_to_string(project_path.join("pxl.toml")).unwrap();
        assert!(config.contains("name = \"game\""));
        assert!(config.contains("[atlases.sprites]"));
        assert!(config.contains("[atlases.ui]"));
        assert!(config.contains("[animations]"));
        assert!(config.contains("[export.generic]"));
        assert!(config.contains("[validate]"));
        assert!(config.contains("strict = true"));
    }

    #[test]
    fn test_init_game_justfile_content() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("game");

        init_project(&project_path, "game", "game").unwrap();

        let justfile = fs::read_to_string(project_path.join("justfile")).unwrap();
        assert!(justfile.contains("build: validate"));
        assert!(justfile.contains("atlas-sprites:"));
        assert!(justfile.contains("atlas-ui:"));
        assert!(justfile.contains("ci: fmt-check validate build"));
    }

    #[test]
    fn test_init_game_sprites_valid_json() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("game");

        init_project(&project_path, "game", "game").unwrap();

        // Check player sprite
        let player =
            fs::read_to_string(project_path.join("src/pxl/sprites/player/idle.pxl")).unwrap();
        assert!(player.contains("\"type\": \"sprite\""));
        assert!(player.contains("player_idle_1"));
        assert!(player.contains("player_idle_2"));

        // Check item sprite (has embedded palette)
        let coin = fs::read_to_string(project_path.join("src/pxl/sprites/items/coin.pxl")).unwrap();
        assert!(coin.contains("\"type\": \"palette\""));
        assert!(coin.contains("\"type\": \"sprite\""));
        assert!(coin.contains("coin"));
    }

    #[test]
    fn test_init_game_ui_valid_json() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().join("game");

        init_project(&project_path, "game", "game").unwrap();

        let ui = fs::read_to_string(project_path.join("src/pxl/ui/button.pxl")).unwrap();
        assert!(ui.contains("button_normal"));
        assert!(ui.contains("button_hover"));
    }
}