scsh 1.11.0

Scoped Skills Helper — preflight a git repo and run its scoped skills in ephemeral containers.
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
use super::cast::cast_player_page;
use super::client_js::live_client_js;
use super::escape::esc;
use super::proc::{empty_output_html, empty_output_label};
use super::session::session_page;
use crate::daemon::model::{DaemonMode, ProcKind, ProcRecord, ProcStatus, Session, Store};

/// A one-proc store for the cast player page tests: the proc has a registered cast and
/// the given status.
fn store_with_cast_proc(status: ProcStatus) -> Store {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "castab".into(),
    Session {
      id: "castab".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 1,
      client_connected: true,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 0,
        kind: ProcKind::Skill,
        label: "claude: add".into(),
        status,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        cast_path: Some("/tmp/x.cast".into()),
        harness: Some("claude".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: None,
        lines: vec![],
      }],
    },
  );
  store
}

fn session_procs_html(html: &str) -> &str {
  let needle = r#"<div class="procs" id="session-procs">"#;
  let start = html.find(needle).expect("session-procs") + needle.len();
  let tail = &html[start..];
  let end = tail.find(r#"<p class="permalink">"#).expect("permalink");
  &tail[..end]
}

#[test]
fn esc_handles_basic_html() {
  assert_eq!(esc("<a>"), "&lt;a&gt;");
}

#[test]
fn browser_player_is_first_party_and_carries_no_third_party_license() {
  // The whole point of scsh-cast-player: the session browser ships NO third-party code.
  // (Exported .html pages still embed asciinema-player via beecast-page — that attribution
  // is pinned separately in tests/cli.rs.)
  let js = super::PLAYER_JS;
  let css = super::PLAYER_CSS;
  assert!(js.contains("ScshCastPlayer"), "the first-party player global must be defined");
  assert!(js.contains("ScshVT"), "the DOM-free core must be bundled first");
  assert!(js.contains("Clean-room implementation"), "the clean-room statement rides in the asset");
  for banned in ["asciinema-player", "AsciinemaPlayer", "@license", "Apache"] {
    assert!(!js.contains(banned), "browser player JS must not carry '{banned}'");
    assert!(!css.contains(banned), "browser player CSS must not carry '{banned}'");
  }
}

/// Run the DOM-free VT core's behavior tests under Node (parsing all three asciicast
/// versions plus the terminal state machine). Skips silently when `node` is not on PATH —
/// the Rust-side structural tests above still gate the asset itself.
#[test]
fn vt_core_node_selftest() {
  if crate::runtime::which("node").is_none() {
    return;
  }
  let dir = std::env::temp_dir().join(format!("scsh-vt-selftest-{}", std::process::id()));
  std::fs::create_dir_all(&dir).unwrap();
  let bundle = dir.join("player.js");
  std::fs::write(&bundle, super::PLAYER_JS).unwrap();
  let script = format!(
    r#"
const assert = require('assert');
require({bundle:?});
const VT = globalThis.ScshVT;

// v3: intervals sum; term size from header; resize + marker events survive; # comments skip.
let c = VT.parseCast('{{"version":3,"term":{{"cols":10,"rows":3}}}}\n# note\n[0.5,"o","hi"]\n[0.5,"m","chapter"]\n[1.0,"r","20x5"]\n');
assert.strictEqual(c.cols, 10); assert.strictEqual(c.rows, 3);
assert.strictEqual(c.events.length, 3);
assert.strictEqual(c.duration, 2);
assert.strictEqual(c.events[2].t, 2);

// v2: absolute times.
c = VT.parseCast('{{"version":2,"width":80,"height":24}}\n[0.5,"o","a"]\n[2.0,"o","b"]\n');
assert.strictEqual(c.duration, 2); assert.strictEqual(c.events[1].t, 2);

// v1: one JSON doc, stdout deltas.
c = VT.parseCast('{{"version":1,"width":5,"height":2,"stdout":[[0.1,"x"],[0.2,"y"]]}}');
assert.strictEqual(c.cols, 5); assert.strictEqual(c.events.length, 2);
assert(Math.abs(c.duration - 0.3) < 1e-9);

// Plain text + CR/LF.
let t = new VT.Term(10, 3);
t.write('hello\r\nworld');
assert.deepStrictEqual(t.textLines(), ['hello', 'world', '']);

// CUP + overwrite mid-screen.
t.write('\x1b[1;3Hga');
assert.strictEqual(t.textLines()[0], 'hegao');

// ED 2 clears everything.
t.write('\x1b[2J');
assert.deepStrictEqual(t.textLines(), ['', '', '']);

// SGR runs merge; colors land on cells.
t = new VT.Term(10, 1);
t.write('\x1b[31mred\x1b[0m ok');
const runs = t.snapshot().rows[0];
assert.strictEqual(runs[0].text, 'red'); assert.strictEqual(runs[0].fg, 1);
assert.strictEqual(runs[1].fg, null);

// 256-color + truecolor.
t = new VT.Term(4, 1);
t.write('\x1b[38;5;196mX\x1b[38;2;1;2;3mY');
const r2 = t.snapshot().rows[0];
assert.strictEqual(r2[0].fg, 196);
assert.strictEqual(r2[1].fg, '#010203');
assert.strictEqual(VT.color256(196), '#ff0000');
assert.strictEqual(VT.color256(232), '#080808');

// Deferred wrap: printing in the last column does not wrap until the next char.
t = new VT.Term(3, 2);
t.write('abc');
assert.strictEqual(t.snapshot().cursor.y, 0);
t.write('d');
assert.deepStrictEqual(t.textLines(), ['abc', 'd']);

// Scroll region: LF at the region bottom scrolls only the region.
t = new VT.Term(5, 4);
t.write('aa\r\nbb\r\ncc\r\ndd');
t.write('\x1b[2;3r\x1b[3;1H\n');
const lines = t.textLines();
assert.strictEqual(lines[0], 'aa');
assert.strictEqual(lines[1], 'cc');
assert.strictEqual(lines[3], 'dd');

// Alternate screen: primary content comes back on exit.
t = new VT.Term(5, 2);
t.write('main');
t.write('\x1b[?1049h\x1b[Halt');
assert.strictEqual(t.textLines()[0], 'alt');
t.write('\x1b[?1049l');
assert.strictEqual(t.textLines()[0], 'main');

// DEC special graphics: tmux border characters.
t = new VT.Term(4, 1);
t.write('\x1b(0qqx\x1b(B');
assert.strictEqual(t.textLines()[0], '──│');

// Cursor hide/show.
t = new VT.Term(2, 1);
t.write('\x1b[?25l');
assert.strictEqual(t.snapshot().cursor.visible, false);
t.write('\x1b[?25h');
assert.strictEqual(t.snapshot().cursor.visible, true);

// OSC titles are consumed, never printed.
t = new VT.Term(8, 1);
t.write('\x1b]0;title\x07ok');
assert.strictEqual(t.textLines()[0], 'ok');

console.log('vt selftest OK');
"#,
    bundle = bundle
  );
  let out = std::process::Command::new("node")
    .arg("-")
    .arg("--input-type=commonjs")
    .stdin(std::process::Stdio::piped())
    .stdout(std::process::Stdio::piped())
    .stderr(std::process::Stdio::piped())
    .spawn()
    .and_then(|mut child| {
      use std::io::Write;
      child.stdin.take().unwrap().write_all(script.as_bytes())?;
      child.wait_with_output()
    })
    .expect("node runs");
  let _ = std::fs::remove_dir_all(&dir);
  assert!(
    out.status.success() && String::from_utf8_lossy(&out.stdout).contains("vt selftest OK"),
    "vt selftest failed:\nstdout: {}\nstderr: {}",
    String::from_utf8_lossy(&out.stdout),
    String::from_utf8_lossy(&out.stderr)
  );
}

#[test]
fn skipped_workflow_step_renders_as_a_dim_slashed_row() {
  let mut store = store_with_cast_proc(ProcStatus::Skipped);
  {
    let p = &mut store.sessions.get_mut("castab").unwrap().procs[0];
    p.cast_path = None; // a skipped step never ran, so it has no recording
    p.detail = Some("skipped — its when: gate is false".into());
    p.note = Some("step 2/2 · needs probe_credentials".into());
  }
  let html = session_page(&store, "castab").expect("session renders");
  let procs = session_procs_html(&html);
  assert!(procs.contains(r#"class="proc skipped""#), "got: {procs}");
  assert!(procs.contains(""), "skipped glyph: {procs}");
  assert!(procs.contains("step 2/2 · needs probe_credentials"), "step note: {procs}");
  assert!(procs.contains("skipped — its when: gate is false"), "skip reason: {procs}");
  assert!(!procs.contains("data-proc-stop"), "a skipped step offers no kill button: {procs}");
  // The client knows the glyph too (live updates keep ⊘ when a tick arrives).
  let js = live_client_js();
  assert!(js.contains("skipped:'⊘'"));
}

#[test]
fn running_cast_preview_starts_near_the_end() {
  let js = live_client_js();
  // A still-running proc's player opens ~3s before the current tail (autoplaying), not at 0.
  assert!(js.contains("const LIVE_PREVIEW_TAIL_SECS = 3"), "tail preview window constant");
  assert!(js.contains("createCastPlayer(box, 'near-end', true)"), "running casts open near the end");
  assert!(js.contains("stats.duration - LIVE_PREVIEW_TAIL_SECS"), "near-end resolves against the loaded duration");
}

#[test]
fn index_page_shows_colored_harness_chips_per_proc() {
  let mut store = store_with_cast_proc(ProcStatus::Running);
  // A second, finished proc on another harness: its chip renders dimmed.
  {
    let session = store.sessions.get_mut("castab").unwrap();
    let mut done = session.procs[0].clone();
    done.index = 1;
    done.status = ProcStatus::Ok;
    done.harness = Some("grok".into());
    done.label = "grok: add".into();
    session.procs.push(done);
    // Build procs never get a chip — only skill runs count.
    let mut build = session.procs[0].clone();
    build.index = 2;
    build.kind = ProcKind::Build;
    build.harness = Some("codex".into());
    session.procs.push(build);
  }
  let html = super::index_page(&store);
  assert!(html.contains(r#"<span class="hchip hchip--claude" title="claude: add (running)">C</span>"#), "got: {html}");
  assert!(html.contains(r#"<span class="hchip hchip--grok hchip--done" title="grok: add (ok)">G</span>"#), "got: {html}");
  assert!(!html.contains(r#"class="hchip hchip--codex"#), "build procs must not render a chip");
  // The stylesheet distinguishes the same letter by harness color, and the client JS
  // mirrors the markup for live re-renders.
  assert!(html.contains(".hchip--claude"));
  assert!(html.contains(".hchip--codex"));
  assert!(html.contains("function harnessChipsHtml"));
}

#[test]
fn index_page_carries_the_images_panel_and_its_client_wiring() {
  let store = Store::new(DaemonMode::Persistent, 7274, 1);
  let html = super::index_page(&store);
  // The panel skeleton: status table body plus every control the client script binds to.
  for id in ["images-body", "images-build-selected", "images-build-all", "images-rebuild-base", "images-force"] {
    assert!(html.contains(&format!("id=\"{id}\"")), "index page should contain #{id}");
  }
  // First paint already lists every known image (§13: no empty limbo while inspect runs).
  assert!(html.contains("checking…"), "skeleton rows start in checking…");
  assert!(html.contains("scsh-base:latest"), "base image row on first paint");
  for tag in ["scsh-opencode:latest", "scsh-claude:latest", "scsh-codex:latest", "scsh-grok:latest", "scsh-cursor:latest"] {
    assert!(html.contains(tag), "harness image {tag} on first paint");
  }
  assert!(html.contains("checking container runtime…"), "note explains the pending inspect");
  // The embedded client script populates the panel from the images API without blanking rows.
  let js = live_client_js();
  assert!(js.contains("/api/v1/images"), "client js should fetch the images API");
  assert!(js.contains("/api/v1/images/build"), "client js should post builds");
  assert!(js.contains("function markImagesChecking"), "refresh keeps rows visible while checking");
  assert!(!js.contains("loading…"), "must not replace the table with a blank loading row");
  // Each image row carries its own [re]build button (base row rebuilds base + everything).
  assert!(js.contains("data-image-build"), "per-row build buttons are rendered");
  assert!(js.contains("function startImageBuildOne"), "per-row build buttons are wired");
  assert!(html.contains("image-action-cell"), "skeleton rows reserve the per-row action cell");
}

#[test]
fn index_page_carries_the_repositories_panel_and_its_client_wiring() {
  let store = Store::new(DaemonMode::Persistent, 7274, 1);
  let html = super::index_page(&store);
  for id in ["repo-path", "repo-pick", "repo-open", "repo-blockers", "defs-panel", "defs-list", "def-form", "repos-body"] {
    assert!(html.contains(&format!("id=\"{id}\"")), "index page should contain #{id}");
  }
  // The four tabs, and their panels.
  for (tab, panel) in [("jobs", "tab-jobs"), ("dirs", "tab-dirs"), ("start", "tab-start"), ("images", "tab-images")] {
    assert!(html.contains(&format!("data-tab=\"{tab}\"")), "index page should have the {tab} tab");
    assert!(html.contains(&format!("id=\"{panel}\"")), "index page should have panel #{panel}");
  }
  let js = live_client_js();
  assert!(js.contains("/api/v1/repos/open"), "client js opens a repo");
  assert!(js.contains("/api/v1/repos/pick"), "client js pops the folder picker");
  assert!(js.contains("/api/v1/jobs/start"), "client js starts a job");
  assert!(js.contains("function renderRepoJobs"), "client js renders jobs by repository");
  assert!(js.contains("OPEN_REPO_RUNNABLE"), "client js gates Start on the repo being runnable");
  assert!(js.contains("function initTabs"), "client js wires the tabs");
}

#[test]
fn empty_output_label_depends_on_proc_status() {
  assert_eq!(empty_output_label(ProcStatus::Running), "No output yet.");
  assert_eq!(empty_output_label(ProcStatus::Waiting), "No output yet.");
  assert_eq!(empty_output_label(ProcStatus::Ok), "No output.");
  assert_eq!(empty_output_label(ProcStatus::Fail), "No output.");
}

#[test]
fn session_proc_html_has_no_stray_backslashes() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "test".into(),
    Session {
      id: "test".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 1,
      client_connected: false,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 0,
        kind: ProcKind::Skill,
        label: "opencode: add".into(),
        status: ProcStatus::Running,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        cast_path: None,
        harness: Some("opencode".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: None,
        lines: vec![],
      }],
    },
  );
  let html = session_page(&store, "test").expect("session page");
  let procs = session_procs_html(&html);
  assert!(!html.contains("\\\n"), "raw-string line continuations must not leak backslashes");
  assert!(!procs.contains("\\\n"), "autoscroll markup must not leak backslashes");
  assert!(procs.contains(r#"<label class="autoscroll-ctl">"#));
  assert!(procs.contains("Auto-scroll to bottom"));
  assert!(html.contains(r#"<div class="output"><div class="dim">No output yet.</div>"#));
  assert!(html.contains(r#"id="session-stop""#), "running session should offer Force stop");
  assert!(html.contains("Force stop"));
}

#[test]
fn ended_session_hides_force_stop_button() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "done01".into(),
    Session {
      id: "done01".into(),
      started_at: 1,
      ended_at: Some(10),
      profile: Some("default".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 10,
      client_connected: false,
      run_pid: None,
      skills: vec![],
      procs: vec![],
    },
  );
  let html = session_page(&store, "done01").expect("session page");
  assert!(!html.contains(r#"id="session-stop""#), "ended session must not offer Force stop");
}

#[test]
fn client_js_wires_force_stop() {
  let js = live_client_js();
  assert!(js.contains("/api/v1/session/stop"), "client js posts session stop");
  assert!(js.contains("function forceStopSession"), "client js defines forceStopSession");
}

#[test]
fn recorded_proc_embeds_cast_player_instead_of_text_output() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "castab".into(),
    Session {
      id: "castab".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 1,
      client_connected: true,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 2,
        kind: ProcKind::Skill,
        label: "claude: add".into(),
        status: ProcStatus::Ok,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        cast_path: Some("/tmp/x.cast".into()),
        harness: Some("claude".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: Some(3.0),
        lines: vec![],
      }],
    },
  );
  let html = session_page(&store, "castab").expect("session page");
  // The page loads the player assets and embeds a player box wired to the cast endpoint.
  assert!(html.contains(r#"<link rel="stylesheet" href="/assets/scsh-cast-player.css">"#), "player css");
  assert!(html.contains(r#"<script src="/assets/scsh-cast-player.js"></script>"#), "player js");
  let procs = session_procs_html(&html);
  assert!(procs.contains(r#"<div class="cast" data-cast-url="/cast/castab/2""#), "cast embed");
  assert!(procs.contains("data-cast-fs"), "fullscreen button");
  assert!(procs.contains("data-cast-link"), "timestamp deep-link button");
  assert!(procs.contains(r#"<a href="/cast/castab/2?dl=1" download>"#), "download link");
  // A recorded proc shows the player, NOT the text output / autoscroll control.
  assert!(!procs.contains(r#"<div class="output">"#), "no text output for recorded proc");
  assert!(!procs.contains("autoscroll-ctl"), "no autoscroll control for recorded proc");
}

#[test]
fn empty_output_html_has_no_backslash_artifacts() {
  let html = empty_output_html(ProcStatus::Ok);
  assert_eq!(html, "<div class=\"dim\">No output.</div>\n");
  assert!(!html.contains("\\"));
  let running = empty_output_html(ProcStatus::Running);
  assert_eq!(running, "<div class=\"dim\">No output yet.</div>\n");
  assert!(!running.contains("\\"));
}

#[test]
fn session_proc_html_shows_autoscroll_while_running() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "test".into(),
    Session {
      id: "test".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 1,
      client_connected: true,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 0,
        kind: ProcKind::Skill,
        label: "opencode: add".into(),
        status: ProcStatus::Running,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        cast_path: None,
        harness: Some("opencode".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: None,
        lines: vec![],
      }],
    },
  );
  let html = session_page(&store, "test").expect("session page");
  let procs = session_procs_html(&html);
  assert!(procs.contains(r#"<label class="autoscroll-ctl">"#));
}

#[test]
fn empty_cast_shows_placeholder_instead_of_player_error() {
  // Both the session-page embed and the standalone player page fetch the cast text first
  // and render a calm placeholder when it has no complete event lines yet, instead of
  // handing the player an empty/404 cast (which errors).
  let js = live_client_js();
  assert!(js.contains("Recording in progress — no frames yet."));
  assert!(js.contains("No recorded frames."));
  assert!(js.contains("cast-placeholder"));
  assert!(js.contains("{ data: text }"), "player mounts over the already-fetched text");
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Running), "castab", 0).expect("player page");
  assert!(page.contains("Recording in progress — no frames yet."));
  assert!(page.contains("cast-placeholder"));
  assert!(page.contains("const LIVE = true;"));
  let done = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(done.contains("const LIVE = false;"));
}

#[test]
fn cast_growth_notifications_drive_the_reload_banner() {
  // The session page routes WS messages by type: cast_growth feeds the banner, everything
  // else stays on the tick path.
  let js = live_client_js();
  assert!(js.contains("if (msg.type === 'cast_growth') { onCastGrowth(msg); return; }"));
  assert!(js.contains("onWsMessage(JSON.parse(ev.data))"));
  assert!(js.contains("Recording grew: +"));
  assert!(js.contains("data-cast-grew"));
  // The standalone player page listens on its own WS connection — but only while the proc
  // runs, and it degrades to the manual reload button when the WS is unavailable.
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Running), "castab", 0).expect("player page");
  assert!(page.contains("'cast_growth'"));
  assert!(page.contains("const SESSION = 'castab';"));
  assert!(page.contains("const PROC = 0;"));
  assert!(page.contains(r#"<button id="grew" hidden></button>"#));
  assert!(page.contains("Recording grew: +"));
  assert!(page.contains("if (!castRunning) return;"), "no WS connect once the proc finished");
}

#[test]
fn live_toggle_renders_only_while_the_proc_runs() {
  // Session-page embed: the Live toggle is in the toolbar, hidden unless the proc runs.
  let running = super::proc::cast_embed_html("castab", &store_with_cast_proc(ProcStatus::Running).sessions["castab"].procs[0]);
  assert!(running.contains(r#"<button type="button" data-cast-live>● Live</button>"#));
  let done = super::proc::cast_embed_html("castab", &store_with_cast_proc(ProcStatus::Ok).sessions["castab"].procs[0]);
  assert!(done.contains(r#"<button type="button" data-cast-live hidden>● Live</button>"#));
  // The client JS follows the tail via cast_growth reloads (see the mechanism comment).
  let js = live_client_js();
  assert!(js.contains("function setCastLive(box, on)"));
  assert!(js.contains("if (box._live) { createCastPlayer(box, box._loadedDuration ?? 'end', true); return; }"));
  // Standalone page: toggle present while running, hidden for finished procs, and the
  // finish notice disables it after the final reload.
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Running), "castab", 0).expect("player page");
  assert!(page.contains(r#"<button id="live-toggle">● Live</button>"#));
  assert!(page.contains("toggle.disabled = true;"));
  let finished = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(finished.contains(r#"<button id="live-toggle" hidden>● Live</button>"#));
}

#[test]
fn export_html_download_renders_on_both_pages_and_hides_without_frames() {
  // Standalone player page: the download link points at the export endpoint, starts
  // hidden, and rides the same no-frames state as the placeholder.
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(page.contains(r#"<a id="dl-html" href="/cast/castab/0/export.html" download hidden>⬇ download .html</a>"#));
  assert!(page.contains("document.getElementById('dl-html').hidden = !stats.events;"));
  // Session-page embed: same link, same hide-until-frames wiring — both in the
  // server-rendered snippet and in the client JS that regenerates it.
  let session = session_page(&store_with_cast_proc(ProcStatus::Ok), "castab").expect("session page");
  let procs = session_procs_html(&session);
  assert!(procs.contains(r#"<a href="/cast/castab/0/export.html" data-cast-export download hidden>⬇ .html</a>"#));
  let js = live_client_js();
  assert!(js.contains("/export.html\" data-cast-export download hidden>⬇ .html</a>"));
  assert!(js.contains("exportLink.hidden = !stats.events;"));
}

#[test]
fn session_page_header_offers_the_session_export_download() {
  // A session with a recorded proc gets the whole-session download button in the header
  // (decided server-side: any proc with a registered cast; the endpoint 404s edge cases).
  let html = session_page(&store_with_cast_proc(ProcStatus::Ok), "castab").expect("session page");
  assert!(
    html.contains(r#"href="/session/castab/export.html" download"#) && html.contains("session-export"),
    "session export button"
  );
  // No recorded proc anywhere → no button (nothing to export; the 404 would only confuse).
  let mut store = store_with_cast_proc(ProcStatus::Ok);
  store.sessions.get_mut("castab").unwrap().procs[0].cast_path = None;
  let bare = session_page(&store, "castab").expect("session page");
  // (The `.session-export` CSS rule is in the shared shell, so match the anchor itself.)
  assert!(!bare.contains("<a class=\"session-export\""), "no export button without any registered cast");
}

#[test]
fn live_client_js_counts_alive_clients_and_shutdown() {
  let js = live_client_js();
  assert!(js.contains("alive_clients"));
  assert!(js.contains("shutting down in"));
}

#[test]
fn live_client_js_skips_index_render_without_sessions() {
  let js = live_client_js();
  assert!(js.contains("if (!body || sessions == null) return"));
  // renderIndex (and the jobs-per-repo view) run only when a snapshot is present.
  assert!(js.contains("if (snapshot) {"));
  assert!(js.contains("renderIndex(snapshot, nowUnix)"));
}

#[test]
fn live_client_js_shows_connecting_on_ws_close() {
  let js = live_client_js();
  assert!(js.contains("setDaemonStatus('connecting', 'connecting…', null)"));
  assert!(!js.contains("daemon unreachable"));
}

#[test]
fn wrap_page_connecting_status_uses_blue() {
  use super::layout::wrap_page;
  let html = wrap_page("scsh sessions", 7274, None, "<p>body</p>");
  assert!(html.contains("class=\"daemon-status connecting\""));
  assert!(html.contains(".daemon-status.connecting .dot { background: var(--cyan);"));
}

#[test]
fn every_daemon_page_carries_the_inline_favicon() {
  use super::layout::wrap_page;
  // A data: URI, so the dashboard and the standalone player page stay request-free.
  let html = wrap_page("scsh sessions", 7274, None, "<p>body</p>");
  assert!(html.contains("<link rel=\"icon\" href=\"data:image/svg+xml,"), "dashboard favicon");
  let player = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(player.contains("<link rel=\"icon\" href=\"data:image/svg+xml,"), "player-page favicon");
}

#[test]
fn wrap_page_serves_valid_css_braces() {
  use super::layout::wrap_page;
  let html = wrap_page("scsh sessions", 7274, None, "<p>body</p>");
  assert!(html.contains(":root {"));
  assert!(!html.contains(":root {{"));
  assert!(html.contains(".daemon-status {"));
}