tovuk 0.1.103

Deploy Rust workers, static frontends, and full-stack services to Tovuk.
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
use super::{
    args::CliOptions,
    config::{TovukConfig, parse_tovuk_toml, validate_config},
    dev_ports::{DevPortOwner, port_owner},
    errors::{Result, agent_error, print_json},
    frontend_checks::{frontend_package_manager, is_next_frontend},
    project_kind::ProjectKind,
};
use serde::Serialize;
use serde_json::json;
use std::{
    collections::BTreeMap,
    fs,
    io::{self, Write},
    net::{TcpListener, TcpStream},
    path::{Path, PathBuf},
    process::{Child, Command, Stdio},
    thread,
    time::Duration,
};

const DEFAULT_FRONTEND_PORT: u16 = 5173;
const LOCAL_HOST: &str = "127.0.0.1";

#[derive(Clone, Copy, Debug, Default)]
struct DevPorts {
    frontend: Option<u16>,
    worker: Option<u16>,
}

#[derive(Clone, Debug, Serialize)]
struct DevPlan {
    frontend_url: Option<String>,
    kind: &'static str,
    next_actions: Vec<String>,
    port_statuses: Vec<DevPortStatus>,
    processes: Vec<DevProcess>,
    project: String,
    worker_url: Option<String>,
}

#[derive(Clone, Debug, Serialize)]
struct DevProcess {
    command: String,
    cwd: String,
    env: BTreeMap<String, String>,
    name: &'static str,
}

#[derive(Clone, Debug, Serialize)]
struct DevPortStatus {
    agent_instruction: Option<String>,
    available: bool,
    host: &'static str,
    name: &'static str,
    owner: Option<DevPortOwner>,
    port: u16,
    url: String,
}

pub(crate) fn dev(project_dir: &Path, cli: &CliOptions) -> Result<()> {
    let config = read_dev_config(project_dir, cli)?;
    let ports = dev_ports(cli, &config)?;
    let plan = create_dev_plan(project_dir, &config, ports);
    if cli.output.json {
        return print_json(&json!({
            "ok": plan.ports_available(),
            "mode": "plan",
            "dev": plan,
            "agent_instruction": dev_agent_instruction(&plan)
        }));
    }
    print_dev_plan(&plan);
    flush_stdout();
    fail_on_port_conflicts(&plan, cli)?;
    run_dev_processes(&plan, cli)
}

fn read_dev_config(project_dir: &Path, cli: &CliOptions) -> Result<TovukConfig> {
    let source = fs::read_to_string(project_dir.join("tovuk.toml")).map_err(|error| {
        agent_error(
            "missing_project_contract",
            format!("Could not read tovuk.toml: {error}"),
            "Run `tovuk dev` from a Tovuk project root or pass the project path.",
            cli.output.json,
        )
    })?;
    let config = parse_tovuk_toml(&source, project_dir).and_then(|config| {
        validate_config(&config)?;
        Ok(config)
    });
    config.map_err(|message| {
        agent_error(
            "invalid_project_contract",
            format!("Invalid tovuk.toml: {message}"),
            "Fix tovuk.toml, then rerun `tovuk dev`.",
            cli.output.json,
        )
    })
}

fn create_dev_plan(project_dir: &Path, config: &TovukConfig, ports: DevPorts) -> DevPlan {
    let mut processes = Vec::new();
    let mut port_statuses = Vec::new();
    let mut worker_url = None;
    let mut frontend_url = None;

    if matches!(
        config.kind,
        ProjectKind::RustWorker | ProjectKind::Fullstack
    ) {
        let worker_root = config
            .backend
            .root
            .as_deref()
            .map_or_else(|| project_dir.to_path_buf(), |root| project_dir.join(root));
        let worker_port = ports
            .worker
            .unwrap_or_else(|| config.backend.port.unwrap_or(config.run.port));
        let command = local_worker_command(&worker_root, config);
        let url = format!("http://{LOCAL_HOST}:{worker_port}");
        worker_url = Some(url.clone());
        port_statuses.push(dev_port_status("worker", worker_port, &url));
        processes.push(DevProcess {
            command,
            cwd: display_path(&worker_root),
            env: BTreeMap::from([("PORT".to_owned(), worker_port.to_string())]),
            name: "worker",
        });
    }

    if matches!(
        config.kind,
        ProjectKind::StaticFrontend | ProjectKind::Fullstack
    ) {
        let frontend_root = config
            .frontend
            .root
            .as_deref()
            .map_or_else(|| project_dir.to_path_buf(), |root| project_dir.join(root));
        let frontend_port = ports.frontend.unwrap_or(DEFAULT_FRONTEND_PORT);
        let command = local_frontend_command(&frontend_root, frontend_port);
        let mut env = BTreeMap::new();
        if let Some(url) = worker_url.as_deref() {
            let key = if is_next_frontend(&frontend_root) {
                "NEXT_PUBLIC_API_URL"
            } else {
                "VITE_API_URL"
            };
            env.insert(key.to_owned(), format!("{url}/api"));
        }
        let url = format!("http://{LOCAL_HOST}:{frontend_port}");
        frontend_url = Some(url.clone());
        port_statuses.push(dev_port_status("frontend", frontend_port, &url));
        processes.push(DevProcess {
            command,
            cwd: display_path(&frontend_root),
            env,
            name: "frontend",
        });
    }

    DevPlan {
        frontend_url,
        kind: config.kind.as_str(),
        next_actions: vec![
            "Open frontend_url in a browser for local UX testing.".to_owned(),
            "Use Ctrl-C to stop all local dev processes.".to_owned(),
            "Run `tovuk check` before deploying.".to_owned(),
        ],
        port_statuses,
        processes,
        project: display_path(project_dir),
        worker_url,
    }
}

fn dev_ports(cli: &CliOptions, config: &TovukConfig) -> Result<DevPorts> {
    Ok(DevPorts {
        frontend: cli_dev_port(&cli.dev.frontend_port, "--frontend-port", cli)?
            .or(config.dev.frontend_port),
        worker: cli_dev_port(&cli.dev.worker_port, "--worker-port", cli)?
            .or(config.dev.worker_port),
    })
}

fn cli_dev_port(value: &str, flag: &str, cli: &CliOptions) -> Result<Option<u16>> {
    let value = value.trim();
    if value.is_empty() {
        return Ok(None);
    }

    let parsed = value.parse::<u16>().map_err(|_error| {
        agent_error(
            "invalid_argument",
            format!("{flag} must be a TCP port from 1 to 65535."),
            format!("Pass a valid local port, for example `{flag} 5174`."),
            cli.output.json,
        )
    })?;
    if parsed == 0 {
        return Err(agent_error(
            "invalid_argument",
            format!("{flag} must be a TCP port from 1 to 65535."),
            format!("Pass a valid local port, for example `{flag} 5174`."),
            cli.output.json,
        ));
    }

    Ok(Some(parsed))
}

fn dev_port_status(name: &'static str, port: u16, url: &str) -> DevPortStatus {
    let has_listener = TcpStream::connect((LOCAL_HOST, port)).is_ok();
    let available = !has_listener && TcpListener::bind((LOCAL_HOST, port)).is_ok();
    let owner = if available { None } else { port_owner(port) };
    DevPortStatus {
        agent_instruction: if available {
            None
        } else {
            Some(port_conflict_instruction(name, port, url, owner.as_ref()))
        },
        available,
        host: LOCAL_HOST,
        name,
        owner,
        port,
        url: url.to_owned(),
    }
}

fn dev_agent_instruction(plan: &DevPlan) -> &'static str {
    if plan.port_statuses.iter().any(|status| !status.available) {
        "One or more planned dev ports are already in use. Inspect dev.port_statuses, then rerun with --worker-port and/or --frontend-port set to available ports, or update [dev] ports in tovuk.toml before using `tovuk dev --output text`."
    } else {
        "Run `tovuk dev --output text` to start these local processes. JSON mode returns the plan only so child process logs do not corrupt machine-readable output."
    }
}

fn port_conflict_instruction(
    name: &'static str,
    port: u16,
    url: &str,
    owner: Option<&DevPortOwner>,
) -> String {
    let owner_text = owner.map_or_else(
        || "another local process".to_owned(),
        |owner| format!("pid {} ({})", owner.pid, owner.command),
    );
    let override_flag = match name {
        "worker" => "--worker-port",
        "frontend" => "--frontend-port",
        _ => "--worker-port or --frontend-port",
    };
    format!(
        "Port {port} is already in use by {owner_text}. Stop that process, rerun `tovuk dev {override_flag} <free_port>`, or set [dev].{name}_port to a free port before using `tovuk dev --output text`. Do not use {url} for verification until the planned Tovuk dev process owns it."
    )
}

fn fail_on_port_conflicts(plan: &DevPlan, cli: &CliOptions) -> Result<()> {
    if plan.ports_available() {
        return Ok(());
    }

    Err(agent_error(
        "dev_port_in_use",
        "One or more planned Tovuk dev ports are already in use.",
        "Inspect the printed port rows, then stop the owning process or rerun with --worker-port and/or --frontend-port set to free ports. Use `tovuk dev --json` for machine-readable owner details.",
        cli.output.json,
    ))
}

impl DevPlan {
    fn ports_available(&self) -> bool {
        self.port_statuses.iter().all(|status| status.available)
    }
}

fn local_worker_command(worker_root: &Path, config: &TovukConfig) -> String {
    if worker_root.join("Cargo.toml").exists() {
        return "cargo run --release".to_owned();
    }
    config
        .backend
        .command
        .clone()
        .or_else(|| config.run.command.clone())
        .unwrap_or_else(|| "cargo run --release".to_owned())
}

fn local_frontend_command(frontend_root: &Path, frontend_port: u16) -> String {
    if is_next_frontend(frontend_root) {
        return match frontend_package_manager(frontend_root) {
            "bun" => format!("bun run dev --hostname {LOCAL_HOST} --port {frontend_port}"),
            _ => format!("npm run dev -- --hostname {LOCAL_HOST} --port {frontend_port}"),
        };
    }
    match frontend_package_manager(frontend_root) {
        "bun" => format!("bun run dev --host {LOCAL_HOST} --port {frontend_port} --strictPort"),
        _ => format!("npm run dev -- --host {LOCAL_HOST} --port {frontend_port} --strictPort"),
    }
}

fn print_dev_plan(plan: &DevPlan) {
    println!("project {}", plan.project);
    if let Some(url) = plan.worker_url.as_deref() {
        println!("worker {url}");
    }
    if let Some(url) = plan.frontend_url.as_deref() {
        println!("frontend {url}");
    }
    for status in &plan.port_statuses {
        let availability = if status.available {
            "available"
        } else {
            "in-use"
        };
        let owner = status.owner.as_ref().map_or_else(String::new, |owner| {
            format!(" owner_pid={} owner_command={}", owner.pid, owner.command)
        });
        println!(
            "port {} {} {}{}",
            status.name, status.url, availability, owner
        );
    }
    for process in &plan.processes {
        let env = process
            .env
            .iter()
            .map(|(key, value)| format!("{key}={value}"))
            .collect::<Vec<_>>()
            .join(" ");
        let prefix = if env.is_empty() {
            String::new()
        } else {
            format!("{env} ")
        };
        println!(
            "{}: (cd '{}' && {}{})",
            process.name, process.cwd, prefix, process.command
        );
    }
}

fn flush_stdout() {
    let _ignore = io::stdout().flush();
}

fn run_dev_processes(plan: &DevPlan, cli: &CliOptions) -> Result<()> {
    let mut children = Vec::new();
    for process in &plan.processes {
        children.push(start_process(process, cli)?);
    }
    wait_for_any_process(&mut children, cli)
}

fn start_process(process: &DevProcess, cli: &CliOptions) -> Result<RunningProcess> {
    let mut command = shell_command(&process.command);
    command
        .current_dir(PathBuf::from(&process.cwd))
        .stdin(Stdio::null())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit());
    for (key, value) in &process.env {
        command.env(key, value);
    }
    let child = command.spawn().map_err(|error| {
        agent_error(
            "dev_process_failed",
            format!("Could not start {} dev process: {error}", process.name),
            "Install the required local toolchain, check the printed command, then rerun `tovuk dev`.",
            cli.output.json,
        )
    })?;
    Ok(RunningProcess {
        child,
        name: process.name,
    })
}

fn wait_for_any_process(children: &mut [RunningProcess], cli: &CliOptions) -> Result<()> {
    loop {
        for index in 0..children.len() {
            if let Some(status) = children[index].child.try_wait().map_err(|error| {
                agent_error(
                    "dev_process_failed",
                    format!(
                        "Could not inspect {} dev process: {error}",
                        children[index].name
                    ),
                    "Stop local dev processes and rerun `tovuk dev`.",
                    cli.output.json,
                )
            })? {
                stop_other_processes(children, index);
                if status.success() {
                    return Ok(());
                }
                return Err(agent_error(
                    "dev_process_exited",
                    format!("{} dev process exited with {status}.", children[index].name),
                    "Read the local process output above, fix the failing command, then rerun `tovuk dev`.",
                    cli.output.json,
                ));
            }
        }
        thread::sleep(Duration::from_millis(300));
    }
}

fn stop_other_processes(children: &mut [RunningProcess], exiting_index: usize) {
    for (index, process) in children.iter_mut().enumerate() {
        if index != exiting_index {
            let _ignore = process.child.kill();
        }
    }
}

struct RunningProcess {
    child: Child,
    name: &'static str,
}

fn shell_command(source: &str) -> Command {
    if cfg!(windows) {
        let mut command = Command::new("cmd");
        command.args(["/C", source]);
        command
    } else {
        let mut command = Command::new("sh");
        command.args(["-c", source]);
        command
    }
}

fn display_path(path: &Path) -> String {
    path.to_string_lossy().into_owned()
}

#[cfg(test)]
mod tests {
    use super::{
        DevPlan, DevPorts, LOCAL_HOST, cli_dev_port, create_dev_plan, dev_port_status, dev_ports,
        fail_on_port_conflicts, port_conflict_instruction, read_dev_config,
    };
    use crate::cli::args::CliOptions;
    use std::{env, fs, net::TcpListener, path::PathBuf, time::SystemTime};

    #[test]
    fn fullstack_plan_wires_worker_and_frontend() -> Result<(), Box<dyn std::error::Error>> {
        let project_dir = temp_project_dir("fullstack-plan")?;
        fs::create_dir_all(project_dir.join("api"))?;
        fs::create_dir_all(project_dir.join("web"))?;
        fs::write(
            project_dir.join("api/Cargo.toml"),
            "[package]\nname = \"api\"\n",
        )?;
        fs::write(
            project_dir.join("web/package.json"),
            "{\"scripts\":{\"dev\":\"vite\"}}",
        )?;
        fs::write(project_dir.join("web/bun.lock"), "")?;
        fs::write(
            project_dir.join("tovuk.toml"),
            r#"
name = "demo"
kind = "fullstack"

[capabilities]
static_frontend = true
worker = true
sqlite = false
object_storage = false
kv = false
state = false
queue = false
cron = false
service_bindings = false
secrets = false
custom_domains = false
logs = true
builds = true
usage_caps = true
billing = true
support = true
abuse = true

[worker]
root = "api"
command = "./target/release/api"
port = 3000
health = "/api/healthz"

[frontend]
root = "web"
output = "dist"
"#,
        )?;

        let cli = CliOptions::default();
        let config = read_dev_config(&project_dir, &cli)?;
        let plan = create_dev_plan(&project_dir, &config, DevPorts::default());

        if plan.processes.len() != 2 {
            return Err(format!("unexpected process count: {}", plan.processes.len()).into());
        }
        if plan.worker_url.as_deref() != Some("http://127.0.0.1:3000") {
            return Err(format!("unexpected worker url: {:?}", plan.worker_url).into());
        }
        if plan.frontend_url.as_deref() != Some("http://127.0.0.1:5173") {
            return Err(format!("unexpected frontend url: {:?}", plan.frontend_url).into());
        }
        if plan.processes[0].command != "cargo run --release" {
            return Err(format!("unexpected worker command: {}", plan.processes[0].command).into());
        }
        if plan.processes[0].env.get("PORT").map(String::as_str) != Some("3000") {
            return Err(format!("unexpected worker env: {:?}", plan.processes[0].env).into());
        }
        if plan.processes[1]
            .env
            .get("VITE_API_URL")
            .map(String::as_str)
            != Some("http://127.0.0.1:3000/api")
        {
            return Err(format!("unexpected frontend env: {:?}", plan.processes[1].env).into());
        }
        if plan.processes[1].command != "bun run dev --host 127.0.0.1 --port 5173 --strictPort" {
            return Err(
                format!("unexpected frontend command: {}", plan.processes[1].command).into(),
            );
        }

        let _ignore = fs::remove_dir_all(project_dir);
        Ok(())
    }

    #[test]
    fn fullstack_plan_uses_next_dev_flags() -> Result<(), Box<dyn std::error::Error>> {
        let project_dir = temp_project_dir("next-fullstack-plan")?;
        fs::create_dir_all(project_dir.join("api"))?;
        fs::create_dir_all(project_dir.join("web"))?;
        fs::write(
            project_dir.join("api/Cargo.toml"),
            "[package]\nname = \"api\"\n",
        )?;
        fs::write(
            project_dir.join("web/package.json"),
            "{\"dependencies\":{\"next\":\"^16.2.7\"},\"scripts\":{\"dev\":\"next dev\"}}",
        )?;
        fs::write(project_dir.join("web/next.config.mjs"), "")?;
        fs::write(
            project_dir.join("tovuk.toml"),
            r#"
name = "demo"
kind = "fullstack"

[capabilities]
static_frontend = true
worker = true
sqlite = false
object_storage = false
kv = false
state = false
queue = false
cron = false
service_bindings = false
secrets = false
custom_domains = false
logs = true
builds = true
usage_caps = true
billing = true
support = true
abuse = true

[worker]
root = "api"
command = "./target/release/api"
port = 3000
health = "/api/healthz"

[frontend]
root = "web"
output = "out"
"#,
        )?;

        let cli = CliOptions::default();
        let config = read_dev_config(&project_dir, &cli)?;
        let plan = create_dev_plan(&project_dir, &config, DevPorts::default());

        if plan.processes[1]
            .env
            .get("NEXT_PUBLIC_API_URL")
            .map(String::as_str)
            != Some("http://127.0.0.1:3000/api")
        {
            return Err(format!("unexpected frontend env: {:?}", plan.processes[1].env).into());
        }
        if plan.processes[1].command != "npm run dev -- --hostname 127.0.0.1 --port 5173" {
            return Err(
                format!("unexpected frontend command: {}", plan.processes[1].command).into(),
            );
        }

        let _ignore = fs::remove_dir_all(project_dir);
        Ok(())
    }

    #[test]
    fn fullstack_plan_honors_dev_port_overrides() -> Result<(), Box<dyn std::error::Error>> {
        let project_dir = temp_project_dir("fullstack-ports")?;
        fs::create_dir_all(project_dir.join("api"))?;
        fs::create_dir_all(project_dir.join("web"))?;
        fs::write(
            project_dir.join("api/Cargo.toml"),
            "[package]\nname = \"api\"\n",
        )?;
        fs::write(
            project_dir.join("web/package.json"),
            "{\"scripts\":{\"dev\":\"vite\"}}",
        )?;
        fs::write(
            project_dir.join("tovuk.toml"),
            r#"
name = "demo"
kind = "fullstack"

[capabilities]
static_frontend = true
worker = true
sqlite = false
object_storage = false
kv = false
state = false
queue = false
cron = false
service_bindings = false
secrets = false
custom_domains = false
logs = true
builds = true
usage_caps = true
billing = true
support = true
abuse = true

[worker]
root = "api"
command = "./target/release/api"
port = 3000
health = "/api/healthz"

[frontend]
root = "web"
output = "dist"

[dev]
worker_port = 3002
frontend_port = 5175
"#,
        )?;

        let mut cli = CliOptions::default();
        cli.dev.frontend_port = "5174".to_owned();
        cli.dev.worker_port = "3001".to_owned();
        let config = read_dev_config(&project_dir, &cli)?;
        let ports = dev_ports(&cli, &config)?;
        let plan = create_dev_plan(&project_dir, &config, ports);

        if plan.worker_url.as_deref() != Some("http://127.0.0.1:3001") {
            return Err(format!("unexpected worker url: {:?}", plan.worker_url).into());
        }
        if plan.frontend_url.as_deref() != Some("http://127.0.0.1:5174") {
            return Err(format!("unexpected frontend url: {:?}", plan.frontend_url).into());
        }
        if plan.processes[0].env.get("PORT").map(String::as_str) != Some("3001") {
            return Err(format!("unexpected worker env: {:?}", plan.processes[0].env).into());
        }
        if plan.processes[1]
            .env
            .get("VITE_API_URL")
            .map(String::as_str)
            != Some("http://127.0.0.1:3001/api")
        {
            return Err(format!("unexpected frontend env: {:?}", plan.processes[1].env).into());
        }
        if !plan.processes[1].command.contains("--port 5174") {
            return Err(
                format!("unexpected frontend command: {}", plan.processes[1].command).into(),
            );
        }

        let _ignore = fs::remove_dir_all(project_dir);
        Ok(())
    }

    #[test]
    fn fullstack_plan_uses_dev_config_ports() -> Result<(), Box<dyn std::error::Error>> {
        let project_dir = temp_project_dir("fullstack-config-ports")?;
        fs::create_dir_all(project_dir.join("api"))?;
        fs::create_dir_all(project_dir.join("web"))?;
        fs::write(
            project_dir.join("api/Cargo.toml"),
            "[package]\nname = \"api\"\n",
        )?;
        fs::write(
            project_dir.join("web/package.json"),
            "{\"scripts\":{\"dev\":\"vite\"}}",
        )?;
        fs::write(
            project_dir.join("tovuk.toml"),
            r#"
name = "demo"
kind = "fullstack"

[capabilities]
static_frontend = true
worker = true
sqlite = false
object_storage = false
kv = false
state = false
queue = false
cron = false
service_bindings = false
secrets = false
custom_domains = false
logs = true
builds = true
usage_caps = true
billing = true
support = true
abuse = true

[worker]
root = "api"
command = "./target/release/api"
port = 3000
health = "/api/healthz"

[frontend]
root = "web"
output = "dist"

[dev]
worker_port = 3001
frontend_port = 5174
"#,
        )?;

        let cli = CliOptions::default();
        let config = read_dev_config(&project_dir, &cli)?;
        let ports = dev_ports(&cli, &config)?;
        let plan = create_dev_plan(&project_dir, &config, ports);

        if plan.worker_url.as_deref() != Some("http://127.0.0.1:3001") {
            return Err(format!("unexpected worker url: {:?}", plan.worker_url).into());
        }
        if plan.frontend_url.as_deref() != Some("http://127.0.0.1:5174") {
            return Err(format!("unexpected frontend url: {:?}", plan.frontend_url).into());
        }
        if plan.processes[1]
            .env
            .get("VITE_API_URL")
            .map(String::as_str)
            != Some("http://127.0.0.1:3001/api")
        {
            return Err(format!("unexpected frontend env: {:?}", plan.processes[1].env).into());
        }
        if !plan.processes[1].command.contains("--port 5174") {
            return Err(
                format!("unexpected frontend command: {}", plan.processes[1].command).into(),
            );
        }

        let _ignore = fs::remove_dir_all(project_dir);
        Ok(())
    }

    #[test]
    fn dev_port_overrides_reject_invalid_ports() {
        let mut cli = CliOptions::default();
        cli.dev.frontend_port = "0".to_owned();

        let error = cli_dev_port(&cli.dev.frontend_port, "--frontend-port", &cli)
            .err()
            .map(|error| error.payload().code.clone());

        assert_eq!(error.as_deref(), Some("invalid_argument"));
    }

    #[test]
    fn port_status_reports_occupied_port() -> Result<(), Box<dyn std::error::Error>> {
        let listener = TcpListener::bind((LOCAL_HOST, 0))?;
        let port = listener.local_addr()?.port();
        let status = dev_port_status("frontend", port, &format!("http://{LOCAL_HOST}:{port}"));

        if status.available {
            return Err(format!("expected occupied port {port} to be unavailable").into());
        }
        if status.agent_instruction.is_none() {
            return Err("expected occupied port to include an agent instruction".into());
        }
        if !status
            .agent_instruction
            .as_deref()
            .unwrap_or_default()
            .contains("Do not use")
        {
            return Err(format!(
                "expected stale verification warning, got {:?}",
                status.agent_instruction
            )
            .into());
        }

        drop(listener);
        Ok(())
    }

    #[test]
    fn occupied_ports_stop_text_dev_before_process_start() -> Result<(), Box<dyn std::error::Error>>
    {
        let listener = TcpListener::bind((LOCAL_HOST, 0))?;
        let port = listener.local_addr()?.port();
        let status = dev_port_status("worker", port, &format!("http://{LOCAL_HOST}:{port}"));
        let plan = DevPlan {
            frontend_url: None,
            kind: "rust-worker",
            next_actions: Vec::new(),
            port_statuses: vec![status],
            processes: Vec::new(),
            project: "/tmp/demo".to_owned(),
            worker_url: Some(format!("http://{LOCAL_HOST}:{port}")),
        };
        let cli = CliOptions::default();

        let error = fail_on_port_conflicts(&plan, &cli)
            .err()
            .ok_or("expected occupied port to fail")?;
        if error.payload().code != "dev_port_in_use" {
            return Err(format!("unexpected error: {:?}", error.payload()).into());
        }

        drop(listener);
        Ok(())
    }

    #[test]
    fn port_conflict_instruction_names_owner() {
        let owner = super::DevPortOwner {
            command: "api".to_owned(),
            pid: 123,
        };
        let instruction =
            port_conflict_instruction("worker", 3000, "http://127.0.0.1:3000", Some(&owner));

        assert!(instruction.contains("pid 123 (api)"));
        assert!(instruction.contains("tovuk dev --worker-port <free_port>"));
        assert!(instruction.contains("[dev].worker_port"));
        assert!(instruction.contains("Do not use http://127.0.0.1:3000"));
    }

    fn temp_project_dir(name: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
        let nanos = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)?
            .as_nanos();
        let path = env::temp_dir().join(format!("tovuk-{name}-{nanos}"));
        let _ignore = fs::remove_dir_all(&path);
        fs::create_dir_all(&path)?;
        Ok(path)
    }
}