okf-studio 0.2.6

okf studio: an interactive terminal IDE and mission control center for Open Knowledge Format (OKF) bundles, built on the okf-core engine.
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
//! Integration tests: snapshot pipeline, reducer behavior, worker
//! round-trips against tempdir bundles, and `TestBackend` rendering.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use okf_core::{ConceptId, Date};
use okf_studio::app::{App, Command, Msg, Overlay, PreviewReport, RefactorOp, TreeSel};
use okf_studio::snapshot::Snapshot;
use okf_studio::{StudioOptions, Tab};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc::channel;
use std::time::Duration;

/// A scratch bundle on disk, cleaned up on drop.
struct TestBundle {
    root: PathBuf,
}

impl Drop for TestBundle {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.root);
    }
}

fn write(root: &Path, rel: &str, content: &str) {
    let path = root.join(rel);
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    std::fs::write(path, content).unwrap();
}

/// Builds the fixture bundle the tests share: a mix of tiers, staleness,
/// a computation contract, a broken link, and log history.
fn fixture() -> TestBundle {
    let root = std::env::temp_dir().join(format!(
        "okf-studio-test-{}-{:?}",
        std::process::id(),
        std::thread::current().id()
    ));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();

    write(
        &root,
        "index.md",
        "---\nokf_version: \"0.2\"\n---\n\n# Concept\n\n* [Travel](policies/travel_expenses.md) - travel policy\n",
    );
    write(
        &root,
        "log.md",
        "# Update Log\n\n## 2026-08-20\n* **Update**: Adjusted `policies/travel_expenses`.\n\n## 2026-08-01\n* **Creation**: Established the bundle.\n",
    );
    write(
        &root,
        "policies/travel_expenses.md",
        "---\ntype: Policy\ntitle: Travel and expense policy\ndescription: Reimbursement for travel.\nstatus: stable\ngenerated: { by: reference_agent/gemini-3.7-flash, at: 2026-06-20T22:53:05Z }\nverified: { by: human:sarah_hr, at: 2026-06-25T09:00:00Z }\nstale_after: 2026-12-31T00:00:00Z\ntags: [hr, travel]\nsources:\n  - id: mileage-guide\n    resource: https://example.com/mileage\n    title: Standard mileage guidelines\n---\n\n# Travel and expense policy\n\nEmployees are reimbursed at approved rates.[^mileage-guide]\n\nTotal reimbursement uses the [Mileage calculator](../computations/mileage_calc.md)\nand relates to [Paid time off](paid_time_off.md).\n\n## Reimbursement rates\n\nStandard rates apply.\n\n[^mileage-guide]: Standard mileage reimbursement guidelines\n",
    );
    write(
        &root,
        "policies/paid_time_off.md",
        "---\ntype: Policy\ntitle: Paid time off\nverified:\n  - { by: process:hr-nightly, at: 2026-07-01T02:00:00Z }\n---\n\n# Paid time off\n\nSee [Travel](travel_expenses.md).\n",
    );
    write(
        &root,
        "policies/remote_work.md",
        "---\ntype: Policy\ntitle: Remote work\nstatus: draft\nstale_after: 2026-08-13T00:00:00Z\n---\n\n# Remote work\n\nLinks to a [missing page](../legacy/old_faq.md).\n",
    );
    write(
        &root,
        "computations/mileage_calc.md",
        "---\ntype: Attested Computation\ntitle: Mileage calculator\nruntime: python\nparameters:\n  - { name: miles, type: number, required: true }\n  - { name: rate_per_mile, type: number }\nexecutor:\n  resource: references/skills/submit_expense.md\n  receipt: [report_id, calculated_amount, status]\nattester:\n  resource: references/attesters/verify_rate.py\n---\n\n# Computation\n\n```python\ndef calculate(miles: float, rate: float = 0.67) -> float:\n    return round(miles * rate, 2)\n```\n",
    );
    write(
        &root,
        "references/skills/submit_expense.md",
        "---\ntype: Skill\n---\n\n# Submit\n",
    );
    write(
        &root,
        "references/attesters/verify_rate.py",
        "def verify(receipt):\n    return True\n",
    );
    TestBundle { root }
}

const TODAY: Date = Date {
    year: 2026,
    month: 8,
    day: 25,
};

fn snapshot(bundle: &TestBundle) -> Arc<Snapshot> {
    Arc::new(Snapshot::build(&bundle.root, Some(TODAY), 1).unwrap())
}

fn app_with(bundle: &TestBundle) -> App {
    let mut app = App::new(&StudioOptions {
        root: bundle.root.clone(),
        today: Some(TODAY),
        no_watch: true,
        initial_tab: None,
        author: Some("human:tester".to_string()),
    });
    app.pending_commands.clear();
    app.update(Msg::SnapshotReady(snapshot(bundle)));
    app
}

fn key(app: &mut App, code: KeyCode) {
    app.update(Msg::Key(KeyEvent::new(code, KeyModifiers::NONE)));
}

fn ch(app: &mut App, c: char) {
    key(app, KeyCode::Char(c));
}

// ---------------------------------------------------------------------------
// Snapshot pipeline.
// ---------------------------------------------------------------------------

#[test]
fn snapshot_derives_the_world_model() {
    let bundle = fixture();
    let snap = snapshot(&bundle);

    assert_eq!(snap.stats.concepts, 5);
    assert_eq!(snap.stats.stale, 1); // remote_work passed its stale_after
    assert_eq!(snap.stats.broken_links, 1);
    assert_eq!(snap.contracts.len(), 1);
    assert!(
        snap.contracts[0].healthy(),
        "{:?}",
        snap.contracts[0].issues
    );

    let travel = ConceptId::parse("policies/travel_expenses").unwrap();
    let meta = snap.meta(&travel).unwrap();
    assert_eq!(meta.tier, okf_core::TrustTier::HumanReviewed);
    assert_eq!(meta.out_degree, 2);
    assert!(!meta.stale);
    assert!(
        meta.headings
            .iter()
            .any(|(_, t)| t == "Reimbursement rates")
    );

    // The attention queue leads with the stale draft.
    assert!(!snap.attention.is_empty());
    assert_eq!(snap.attention[0].id.to_string(), "policies/remote_work");

    // Search: segment initials find the concept; filters compose.
    let hits = snap.search.search("pte", 10);
    assert_eq!(hits[0].id, travel);
    let stale_hits = snap.search.search("is:stale", 10);
    assert_eq!(stale_hits.len(), 1);
    assert_eq!(stale_hits[0].id.to_string(), "policies/remote_work");

    // Log views merged.
    assert_eq!(snap.log_timeline.iter().map(|(_, c)| c).sum::<usize>(), 2);
    assert_eq!(snap.log_days.first().unwrap().0, "2026-08-20");

    // Graph model: 5 concepts + 1 phantom (broken target) + 1 source node.
    assert_eq!(snap.graph.nodes.len(), 7);
}

// ---------------------------------------------------------------------------
// Markdown renderer.
// ---------------------------------------------------------------------------

#[test]
fn renderer_maps_links_headings_and_footnotes() {
    let theme = okf_studio::theme::Theme::with_color(false);
    let body = "# Title\n\nSee [other](./other.md) and a note.[^src]\n\n```python\nx = 1\n```\n\n| a | b |\n|---|---|\n| 1 | 2 |\n\n[^src]: The source\n";
    let doc = okf_studio::markdown::render_document(body, 60, &theme, None);
    assert_eq!(doc.headings.len(), 1);
    assert_eq!(doc.links.len(), 2); // one link + one footnote ref
    assert!(doc.footnote_defs.contains_key("src"));
    let text: String = doc
        .lines
        .iter()
        .map(|l| {
            l.spans
                .iter()
                .map(|s| s.content.as_ref())
                .collect::<String>()
                + "\n"
        })
        .collect();
    assert!(text.contains("→other"));
    assert!(text.contains("syntax ✔"), "code fence verdict shown");
    assert!(text.contains("│ 1 │ 2 │"), "table box-drawn: {text}");
}

#[test]
fn renderer_survives_adversarial_input() {
    let theme = okf_studio::theme::Theme::with_color(false);
    for body in [
        "```\nunclosed fence",
        "[unclosed link](nowhere",
        "**unclosed bold and `code",
        "| lonely table cell",
        "日本語のテキストが折り返される長い行です。日本語のテキストが折り返される長い行です。",
        "",
    ] {
        let doc = okf_studio::markdown::render_document(body, 20, &theme, None);
        let _ = doc.lines.len(); // must not panic
    }
}

// ---------------------------------------------------------------------------
// Reducer.
// ---------------------------------------------------------------------------

#[test]
fn keys_drive_tabs_palette_and_quit() {
    let bundle = fixture();
    let mut app = app_with(&bundle);
    assert_eq!(app.tab, Tab::Explorer);

    ch(&mut app, '2');
    assert_eq!(app.tab, Tab::Graph);
    ch(&mut app, '3');
    assert_eq!(app.tab, Tab::Trust);
    ch(&mut app, '1');
    assert_eq!(app.tab, Tab::Explorer);

    // Omnisearch: type a query, Enter jumps to the top hit.
    ch(&mut app, '/');
    assert!(matches!(app.overlays.last(), Some(Overlay::Palette(_))));
    for c in "pto".chars() {
        ch(&mut app, c);
    }
    key(&mut app, KeyCode::Enter);
    assert!(app.overlays.is_empty());
    assert_eq!(
        app.explorer.selected,
        Some(TreeSel::Concept(
            ConceptId::parse("policies/paid_time_off").unwrap()
        ))
    );

    // Esc closes overlays; q quits at the root.
    ch(&mut app, '?');
    assert!(matches!(app.overlays.last(), Some(Overlay::Help(_))));
    key(&mut app, KeyCode::Esc);
    assert!(app.overlays.is_empty());
    ch(&mut app, 'q');
    assert!(app.should_quit);
}

#[test]
fn selection_survives_snapshot_swaps() {
    let bundle = fixture();
    let mut app = app_with(&bundle);
    let pto = ConceptId::parse("policies/paid_time_off").unwrap();
    app.open_concept(&pto);
    assert_eq!(app.explorer.selected, Some(TreeSel::Concept(pto.clone())));

    // Same concept still present: selection unchanged.
    app.update(Msg::SnapshotReady(snapshot(&bundle)));
    assert_eq!(app.explorer.selected, Some(TreeSel::Concept(pto.clone())));

    // Concept removed underneath us: selection falls back to the first.
    std::fs::remove_file(bundle.root.join("policies/paid_time_off.md")).unwrap();
    app.update(Msg::SnapshotReady(snapshot(&bundle)));
    assert_ne!(app.explorer.selected, Some(TreeSel::Concept(pto)));
    assert!(app.explorer.selected.is_some());
}

#[test]
fn remove_verb_debounces_into_a_preview_command() {
    let bundle = fixture();
    let mut app = app_with(&bundle);
    let pto = ConceptId::parse("policies/paid_time_off").unwrap();
    app.open_concept(&pto);
    app.pending_commands.clear();

    key(&mut app, KeyCode::Delete);
    assert!(matches!(app.overlays.last(), Some(Overlay::Refactor(_))));

    // The dry-run request is debounced onto the next tick.
    app.update(Msg::Tick);
    let preview = app
        .pending_commands
        .iter()
        .find(|c| matches!(c, Command::Preview { .. }));
    assert!(preview.is_some(), "{:?}", app.pending_commands);
}

#[test]
fn verify_confirm_emits_stamp_command() {
    let bundle = fixture();
    let mut app = app_with(&bundle);
    let travel = ConceptId::parse("policies/travel_expenses").unwrap();
    app.open_concept(&travel);
    app.pending_commands.clear();

    ch(&mut app, 'v');
    assert!(matches!(app.overlays.last(), Some(Overlay::Confirm(_))));
    key(&mut app, KeyCode::Enter);
    assert!(matches!(
        app.pending_commands.first(),
        Some(Command::StampVerification(id)) if *id == travel
    ));
}

// ---------------------------------------------------------------------------
// Worker round-trips.
// ---------------------------------------------------------------------------

fn recv_until<F: Fn(&Msg) -> bool>(rx: &std::sync::mpsc::Receiver<Msg>, accept: F) -> Msg {
    loop {
        let msg = rx
            .recv_timeout(Duration::from_secs(10))
            .expect("worker reply");
        if accept(&msg) {
            return msg;
        }
    }
}

#[test]
fn worker_reloads_stamps_and_refactors() {
    let bundle = fixture();
    let (tx, rx) = channel();
    let worker = okf_studio::worker::spawn(
        okf_studio::worker::WorkerConfig {
            root: bundle.root.clone(),
            today: Some(TODAY),
            author: "human:tester".to_string(),
        },
        tx,
    );

    worker.send(Command::Reload).unwrap();
    let Msg::SnapshotReady(snap) = recv_until(&rx, |m| matches!(m, Msg::SnapshotReady(_))) else {
        unreachable!()
    };
    assert_eq!(snap.generation, 1);

    // Verification stamp: unverified concept becomes human-reviewed on disk,
    // and the log records it.
    let remote = ConceptId::parse("policies/remote_work").unwrap();
    worker
        .send(Command::StampVerification(remote.clone()))
        .unwrap();
    let Msg::Applied(result) = recv_until(&rx, |m| matches!(m, Msg::Applied(_))) else {
        unreachable!()
    };
    assert!(result.is_ok(), "{result:?}");
    let Msg::SnapshotReady(snap) = recv_until(&rx, |m| matches!(m, Msg::SnapshotReady(_))) else {
        unreachable!()
    };
    assert_eq!(
        snap.meta(&remote).unwrap().tier,
        okf_core::TrustTier::HumanReviewed
    );
    let log_text = std::fs::read_to_string(bundle.root.join("log.md")).unwrap();
    assert!(log_text.contains("Verified concept `policies/remote_work`"));

    // Move preview then apply: dry-run touches nothing, apply rewrites links.
    let source = ConceptId::parse("policies/travel_expenses").unwrap();
    let target = ConceptId::parse("policies/travel").unwrap();
    let op = RefactorOp::Move {
        source: source.clone(),
        target: target.clone(),
        force: false,
    };
    worker
        .send(Command::Preview {
            request: 7,
            op: op.clone(),
        })
        .unwrap();
    let Msg::PreviewReady(7, Ok(PreviewReport::Move(report))) =
        recv_until(&rx, |m| matches!(m, Msg::PreviewReady(..)))
    else {
        panic!("expected a move preview")
    };
    assert!(report.dry_run);
    assert!(report.rewritten_incoming_links >= 1);
    assert!(bundle.root.join("policies/travel_expenses.md").exists());

    worker.send(Command::Apply(op)).unwrap();
    let Msg::Applied(result) = recv_until(&rx, |m| matches!(m, Msg::Applied(_))) else {
        unreachable!()
    };
    assert!(result.is_ok(), "{result:?}");
    let Msg::SnapshotReady(snap) = recv_until(&rx, |m| matches!(m, Msg::SnapshotReady(_))) else {
        unreachable!()
    };
    assert!(!bundle.root.join("policies/travel_expenses.md").exists());
    assert!(snap.bundle.contains(&target));
    let pto_text = std::fs::read_to_string(bundle.root.join("policies/paid_time_off.md")).unwrap();
    assert!(pto_text.contains("travel.md"), "{pto_text}");

    worker.send(Command::Shutdown).unwrap();
}

#[test]
fn worker_extends_stale_after() {
    let bundle = fixture();
    let (tx, rx) = channel();
    let worker = okf_studio::worker::spawn(
        okf_studio::worker::WorkerConfig {
            root: bundle.root.clone(),
            today: Some(TODAY),
            author: "human:tester".to_string(),
        },
        tx,
    );
    worker.send(Command::Reload).unwrap();
    let _ = recv_until(&rx, |m| matches!(m, Msg::SnapshotReady(_)));

    let remote = ConceptId::parse("policies/remote_work").unwrap();
    let new_date = Date {
        year: 2026,
        month: 12,
        day: 31,
    };
    worker
        .send(Command::SetStaleAfter(remote.clone(), new_date))
        .unwrap();
    let _ = recv_until(&rx, |m| matches!(m, Msg::Applied(_)));
    let Msg::SnapshotReady(snap) = recv_until(&rx, |m| matches!(m, Msg::SnapshotReady(_))) else {
        unreachable!()
    };
    let meta = snap.meta(&remote).unwrap();
    assert!(!meta.stale);
    assert_eq!(
        meta.stale_after.as_ref().unwrap().raw,
        "2026-12-31T00:00:00Z"
    );
    worker.send(Command::Shutdown).unwrap();
}

// ---------------------------------------------------------------------------
// TestBackend rendering.
// ---------------------------------------------------------------------------

fn buffer_text(terminal: &Terminal<TestBackend>) -> String {
    let buffer = terminal.backend().buffer();
    let area = *buffer.area();
    let mut out = String::new();
    for y in area.top()..area.bottom() {
        for x in area.left()..area.right() {
            out.push_str(buffer[(x, y)].symbol());
        }
        out.push('\n');
    }
    out
}

fn draw(app: &App, width: u16, height: u16) -> String {
    let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();
    terminal
        .draw(|frame| okf_studio::ui::shell::draw(frame, app))
        .unwrap();
    buffer_text(&terminal)
}

#[test]
fn every_workspace_renders_at_both_sizes() {
    let bundle = fixture();
    let mut app = app_with(&bundle);

    for (w, h) in [(80, 24), (120, 40)] {
        app.tab = Tab::Explorer;
        let text = draw(&app, w, h);
        assert!(text.contains("okf studio"));
        assert!(text.contains("Explorer"));
        assert!(text.contains("travel_exp"), "{text}"); // may clip at 80 cols
        assert!(text.contains("conformant"));
        assert!(text.contains("5 concepts"));

        app.tab = Tab::Graph;
        let text = draw(&app, w, h);
        assert!(text.contains("Graph"));
        assert!(text.contains("layout: force"));
        assert!(text.contains("color: trust"));

        app.tab = Tab::Trust;
        let text = draw(&app, w, h);
        assert!(text.contains("Mission Control"));
        assert!(text.contains("ATTENTION QUEUE"));
        assert!(text.contains("remote_work"));
        assert!(text.contains("ACTORS"));

        app.tab = Tab::Computations;
        let text = draw(&app, w, h);
        assert!(text.contains("Computations"));
        assert!(text.contains("mileage_calc"));
        assert!(text.contains("PLAYGROUND"));
        assert!(text.contains("call sketch"));
    }
}

#[test]
fn overlays_render() {
    let bundle = fixture();
    let mut app = app_with(&bundle);

    ch(&mut app, '/');
    for c in "trav".chars() {
        ch(&mut app, c);
    }
    let text = draw(&app, 100, 30);
    assert!(text.contains("trav"));
    assert!(text.contains("travel"));

    key(&mut app, KeyCode::Esc);
    ch(&mut app, '!');
    let text = draw(&app, 100, 30);
    assert!(text.contains("Diagnostics"));

    key(&mut app, KeyCode::Esc);
    let travel = ConceptId::parse("policies/travel_expenses").unwrap();
    app.open_concept(&travel);
    key(&mut app, KeyCode::F(2));
    let text = draw(&app, 100, 30);
    assert!(text.contains("Move policies/travel_expenses"));
    assert!(text.contains("target id"));
}