okf 0.2.6

The Open Knowledge Format (OKF) v0.2 in pure Rust: the `okf` CLI (validate, lint, inspect, index, graph, format, diff) plus the full okf-core and okf-validator library APIs re-exported.
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
//! Integration tests for OKF CLI refactoring commands: `mv`, `rm`, `split`, and `merge`.

mod common;

use common::TempDir;
use std::process::Command;

fn okf() -> Command {
    Command::new(env!("CARGO_BIN_EXE_okf"))
}

#[test]
fn cli_mv_renames_and_rewrites_links() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "auth/token.md",
        "---\ntype: Concept\ntitle: Auth Token\n---\n\n# Auth Token\n\nSee [Profile](../users/profile.md).\n",
    );
    tmp.write(
        "users/profile.md",
        "---\ntype: Concept\ntitle: User Profile\n---\n\n# User Profile\n\nUses [Auth Token](../auth/token.md).\n",
    );

    let output = okf()
        .args([
            "mv",
            "auth/token",
            "security/jwt",
            "--bundle",
            tmp.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("renamed concept auth/token -> security/jwt"));
    assert!(stdout.contains("rewrote 1 incoming link(s)"));
    assert!(stdout.contains("rebased 1 outgoing link(s)"));

    // Check files on disk
    assert!(!tmp.path().join("auth/token.md").exists());
    assert!(tmp.path().join("security/jwt.md").exists());

    let moved_content = tmp.read("security/jwt.md");
    assert!(moved_content.contains("[Profile](../users/profile.md)"));

    let users_content = tmp.read("users/profile.md");
    assert!(users_content.contains("[Auth Token](../security/jwt.md)"));

    let log_content = tmp.read("log.md");
    assert!(log_content.contains("Renamed concept `auth/token` to `security/jwt`"));
}

#[test]
fn cli_rename_alias_and_json_output() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "old_name.md",
        "---\ntype: Concept\ntitle: Old Name\n---\n\n# Old Name\n",
    );

    let output = okf()
        .args([
            "rename",
            "old_name",
            "new_name",
            "--bundle",
            tmp.path().to_str().unwrap(),
            "--json",
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let val: serde_json::Value = serde_json::from_str(&stdout).unwrap();

    assert_eq!(val["status"], "ok");
    assert_eq!(val["source"], "old_name");
    assert_eq!(val["target"], "new_name");
    assert_eq!(val["dry_run"], false);
    assert!(!tmp.path().join("old_name.md").exists());
    assert!(tmp.path().join("new_name.md").exists());
}

#[test]
fn cli_rm_protection_and_redirect() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "deprecated.md",
        "---\ntype: Concept\ntitle: Deprecated\n---\n\n# Deprecated\n",
    );
    tmp.write(
        "replacement.md",
        "---\ntype: Concept\ntitle: Replacement\n---\n\n# Replacement\n",
    );
    tmp.write(
        "guide.md",
        "---\ntype: Concept\ntitle: Guide\n---\n\n# Guide\n\nRead [Deprecated](deprecated.md).\n",
    );

    // 1. Without flags -> should fail
    let output_err = okf()
        .args(["rm", "deprecated", "--bundle", tmp.path().to_str().unwrap()])
        .output()
        .unwrap();

    assert_eq!(output_err.status.code(), Some(65));
    let stderr = String::from_utf8(output_err.stderr).unwrap();
    assert!(
        stderr.contains("cannot remove concept 'deprecated' because 1 other concept(s) link to it")
    );

    // 2. With --redirect-to
    let output_redirect = okf()
        .args([
            "rm",
            "deprecated",
            "--redirect-to",
            "replacement",
            "--bundle",
            tmp.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

    assert!(output_redirect.status.success());
    assert!(!tmp.path().join("deprecated.md").exists());

    let guide_content = tmp.read("guide.md");
    assert!(guide_content.contains("[Deprecated](replacement.md)"));
}

#[test]
fn cli_rm_unlink_flag() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "obsolete.md",
        "---\ntype: Concept\ntitle: Obsolete\n---\n\n# Obsolete\n",
    );
    tmp.write(
        "guide.md",
        "---\ntype: Concept\ntitle: Guide\n---\n\n# Guide\n\nSee [Obsolete Guide](obsolete.md) for background.\n",
    );

    let output = okf()
        .args([
            "delete",
            "obsolete",
            "--unlink",
            "--bundle",
            tmp.path().to_str().unwrap(),
            "--json",
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let val: serde_json::Value = serde_json::from_str(&stdout).unwrap();

    assert_eq!(val["unlinked_count"], 1);
    assert!(!tmp.path().join("obsolete.md").exists());

    let guide_content = tmp.read("guide.md");
    assert!(guide_content.contains("See Obsolete Guide for background."));
}

#[test]
fn cli_split_extracts_section() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "billing/pricing.md",
        "---\ntype: Concept\ntitle: Pricing\n---\n\n# Pricing\n\nGeneral overview.\n\n## Enterprise Tier\n\nEnterprise pricing includes dedicated support.\nSLA is 99.99% uptime.\n\n## Community Tier\n\nFree tier.\n",
    );

    let output = okf()
        .args([
            "split",
            "billing/pricing",
            "billing/enterprise",
            "--section",
            "Enterprise Tier",
            "--title",
            "Enterprise Pricing",
            "--bundle",
            tmp.path().to_str().unwrap(),
            "--json",
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let val: serde_json::Value = serde_json::from_str(&stdout).unwrap();

    assert_eq!(val["source"], "billing/pricing");
    assert_eq!(val["target"], "billing/enterprise");
    assert_eq!(val["target_title"], "Enterprise Pricing");

    assert!(tmp.path().join("billing/enterprise.md").exists());
    let ent_content = tmp.read("billing/enterprise.md");
    assert!(ent_content.contains("# Enterprise Pricing"));
    assert!(ent_content.contains("Enterprise pricing includes dedicated support."));

    let pricing_content = tmp.read("billing/pricing.md");
    assert!(
        pricing_content
            .contains("## Enterprise Tier\n\nSee [Enterprise Pricing](enterprise.md).\n")
    );
    assert!(pricing_content.contains("## Community Tier"));
}

#[test]
fn cli_merge_consolidates_concepts() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "billing/discounts.md",
        "---\ntype: Concept\ntitle: Discounts\n---\n\n# Discounts\n\nCoupon codes.\n",
    );
    tmp.write(
        "billing/pricing.md",
        "---\ntype: Concept\ntitle: Pricing\n---\n\n# Pricing\n\nPricing details.\n",
    );
    tmp.write(
        "overview.md",
        "---\ntype: Concept\ntitle: Overview\n---\n\n# Overview\n\nCheck [Discounts](billing/discounts.md).\n",
    );

    let output = okf()
        .args([
            "merge-concepts",
            "billing/discounts",
            "billing/pricing",
            "--bundle",
            tmp.path().to_str().unwrap(),
            "--json",
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let val: serde_json::Value = serde_json::from_str(&stdout).unwrap();

    assert_eq!(val["source"], "billing/discounts");
    assert_eq!(val["target"], "billing/pricing");
    assert_eq!(val["rewritten_links_count"], 1);

    assert!(!tmp.path().join("billing/discounts.md").exists());

    let pricing_content = tmp.read("billing/pricing.md");
    assert!(pricing_content.contains("## Discounts"));
    assert!(pricing_content.contains("Coupon codes."));

    let overview_content = tmp.read("overview.md");
    assert!(overview_content.contains("[Discounts](billing/pricing.md)"));
}

#[test]
fn cli_mv_renames_section_and_rewrites_anchors() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "billing/pricing.md",
        "---\ntype: Concept\ntitle: Pricing\n---\n\n# Pricing\n\nSee [Internal Tiers](#pricing-tiers).\n\n## Pricing Tiers\n\nTier details.\n",
    );
    tmp.write(
        "overview.md",
        "---\ntype: Concept\ntitle: Overview\n---\n\n# Overview\n\nCheck [Pricing Plans](billing/pricing.md#pricing-tiers).\n",
    );

    let output = okf()
        .args([
            "mv",
            "billing/pricing#pricing-tiers",
            "billing/pricing#subscription-plans",
            "--bundle",
            tmp.path().to_str().unwrap(),
            "--json",
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let val: serde_json::Value = serde_json::from_str(&stdout).unwrap();

    assert_eq!(val["status"], "ok");
    assert_eq!(val["kind"], "rename_section");
    assert_eq!(val["internal_links_updated"], 1);
    assert_eq!(val["external_links_updated"], 1);

    let pricing_content = tmp.read("billing/pricing.md");
    assert!(
        pricing_content.contains("## subscription-plans")
            || pricing_content.contains("## Subscription Plans")
            || pricing_content.contains("subscription-plans")
    );
    assert!(pricing_content.contains("[Internal Tiers](#subscription-plans)"));

    let overview_content = tmp.read("overview.md");
    assert!(overview_content.contains("[Pricing Plans](billing/pricing.md#subscription-plans)"));
}

#[test]
fn cli_refactor_exit_codes_missing_bundle_and_data_error() {
    // 1. Missing bundle path -> EX_NOINPUT (66)
    let output_no_input = okf()
        .args([
            "mv",
            "auth",
            "security",
            "--bundle",
            "/non/existent/bundle/dir",
        ])
        .output()
        .unwrap();
    assert_eq!(output_no_input.status.code(), Some(66));

    // 2. Concept not found -> EX_DATAERR (65)
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");

    let output_data_err = okf()
        .args([
            "mv",
            "ghost_concept",
            "target",
            "--bundle",
            tmp.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert_eq!(output_data_err.status.code(), Some(65));

    // 3. Section not found on split -> EX_DATAERR (65)
    tmp.write("concept.md", "---\ntype: Concept\n---\n\n# Concept\n");
    let output_sec_err = okf()
        .args([
            "split",
            "concept",
            "new_concept",
            "--section",
            "NonExistentSection",
            "--bundle",
            tmp.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert_eq!(output_sec_err.status.code(), Some(65));
}

#[test]
fn cli_refactor_author_attribution_and_path_resolution() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "module/service.md",
        "---\ntype: Concept\ntitle: Service\n---\n\n# Service\n\n## Implementation\n\n```python\n# ## Implementation\npass\n```\n\nDetails.\n",
    );

    // 1. Rename with ./ relative paths and --author
    let output = okf()
        .args([
            "mv",
            "./module/service.md",
            "./module/daemon.md",
            "--bundle",
            tmp.path().to_str().unwrap(),
            "--author",
            "human:alice",
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    assert!(!tmp.path().join("module/service.md").exists());
    assert!(tmp.path().join("module/daemon.md").exists());

    let log_content = tmp.read("log.md");
    assert!(log_content.contains("(by human:alice)"));

    // 2. Split section with code block immunity and --author
    let split_out = okf()
        .args([
            "split",
            "module/daemon",
            "module/impl",
            "--section",
            "Implementation",
            "--bundle",
            tmp.path().to_str().unwrap(),
            "--author",
            "human:bob",
        ])
        .output()
        .unwrap();

    assert!(split_out.status.success());
    assert!(tmp.path().join("module/impl.md").exists());

    let log_after_split = tmp.read("log.md");
    assert!(log_after_split.contains("(by human:bob)"));

    let daemon_content = tmp.read("module/daemon.md");
    assert!(daemon_content.contains("[Implementation](impl.md)"));
}

#[test]
fn cli_merge_force_flag_accepted() {
    let tmp = TempDir::new();
    tmp.write("index.md", "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n");
    tmp.write(
        "a.md",
        "---\ntype: Concept\ntitle: A\n---\n\n# A\n\nContent A.\n",
    );
    tmp.write(
        "b.md",
        "---\ntype: Concept\ntitle: B\n---\n\n# B\n\nContent B.\n",
    );

    let output = okf()
        .args([
            "merge",
            "a",
            "b",
            "--force",
            "--bundle",
            tmp.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    assert!(!tmp.path().join("a.md").exists());
    assert!(tmp.path().join("b.md").exists());
}

#[test]
fn cli_refactor_infers_bundle_from_source_file_path() {
    let tmp = TempDir::new();
    let bundle_dir = tmp.path().join("my_bundle");
    std::fs::create_dir_all(&bundle_dir).unwrap();

    std::fs::write(
        bundle_dir.join("index.md"),
        "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n",
    )
    .unwrap();
    std::fs::write(
        bundle_dir.join("source.md"),
        "---\ntype: Concept\ntitle: Source\n---\n\n# Source\n",
    )
    .unwrap();

    let outside_dir = tmp.path().join("outside");
    std::fs::create_dir_all(&outside_dir).unwrap();

    let output = Command::new(env!("CARGO_BIN_EXE_okf"))
        .args([
            "mv",
            bundle_dir.join("source.md").to_str().unwrap(),
            "target",
        ])
        .current_dir(&outside_dir)
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(!bundle_dir.join("source.md").exists());
    assert!(bundle_dir.join("target.md").exists());
}