gobby-wiki 0.7.0

Gobby wiki CLI shell
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
use std::fs;

use crate::graph::analytics::GraphAnalyticsError;
use crate::graph::{
    GraphExportOptions, WikiGraphCodeEdge, WikiGraphDocument, WikiGraphFacts, WikiGraphLink,
    WikiGraphLinkTarget, WikiGraphSource,
};
use crate::search::SearchScope;

use super::graph::graph_export_error;
use super::write::{StagedExport, commit_staged_exports, rollback_staged_exports};
use super::*;

#[test]
fn exports_do_not_mutate_wiki_state() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let wiki_page = root.join("knowledge/topics/ownership.md");
    fs::create_dir_all(wiki_page.parent().expect("wiki parent")).expect("wiki dir");
    fs::write(&wiki_page, "# Ownership\n\nCanonical page.\n").expect("wiki page");
    let before = fs::read_to_string(&wiki_page).expect("read before");

    let artifact = write_export(
        root,
        ExportRequest {
            filename: "ownership-bundle.md".to_string(),
            kind: ExportKind::Bundle,
            contents: "# Ownership Bundle\n\nGenerated export.\n".to_string(),
        },
    )
    .expect("export is written");

    assert_eq!(artifact.path, root.join("outputs/ownership-bundle.md"));
    assert_eq!(artifact.bytes_written, 38);
    assert_eq!(fs::read_to_string(&wiki_page).expect("read after"), before);
    assert_eq!(
        fs::read_to_string(root.join("outputs/ownership-bundle.md")).expect("export"),
        "# Ownership Bundle\n\nGenerated export.\n"
    );
    assert_eq!(
        bundled_workflow_assets()
            .iter()
            .map(|asset| asset.name)
            .collect::<Vec<_>>(),
        vec!["compile", "query", "audit"]
    );

    let bundle_artifact =
        export_workflow_assets(root, "workflow-assets.md").expect("workflow assets export");
    assert_eq!(
        bundle_artifact.path,
        root.join("outputs/workflow-assets.md")
    );
    let bundle = fs::read_to_string(root.join("outputs/workflow-assets.md")).expect("bundle");
    assert!(bundle.contains("# GWiki Workflow Assets"));
    assert!(bundle.contains("## compile"));

    let report = root.join("meta/health/latest.md");
    fs::create_dir_all(report.parent().expect("report parent")).expect("report dir");
    fs::write(&report, "# Health\n\nGenerated report.\n").expect("report");
    let report_artifact =
        export_report_file(root, "reports/health.md", &report).expect("report export");
    assert_eq!(report_artifact.path, root.join("outputs/reports/health.md"));
    assert_eq!(
        fs::read_to_string(root.join("outputs/reports/health.md")).expect("report export"),
        "# Health\n\nGenerated report.\n"
    );
    assert_eq!(fs::read_to_string(&wiki_page).expect("read final"), before);
}

#[test]
fn graph_analytics_export_artifacts_include_degradation_and_mermaid() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let scope = SearchScope::project("project-123");
    let facts = WikiGraphFacts {
        documents: vec![
            WikiGraphDocument {
                scope: scope.clone(),
                path: "knowledge/topics/overview.md".into(),
                title: Some("Overview".to_string()),
            },
            WikiGraphDocument {
                scope: scope.clone(),
                path: "code/src/lib.rs".into(),
                title: Some("lib.rs".to_string()),
            },
            WikiGraphDocument {
                scope: scope.clone(),
                path: "documents/design.md".into(),
                title: Some("Design Notes".to_string()),
            },
        ],
        links: vec![WikiGraphLink {
            scope: scope.clone(),
            source_path: "knowledge/topics/overview.md".into(),
            raw_target: "code/src/lib.rs".to_string(),
            target: WikiGraphLinkTarget::Resolved("code/src/lib.rs".into()),
        }],
        sources: vec![WikiGraphSource {
            scope,
            source_path: "raw/sources/example.md".into(),
            document_path: "knowledge/topics/overview.md".into(),
        }],
        code_edges: Vec::new(),
    };

    let artifacts = export_graph_artifacts(
        root,
        &facts,
        GraphExportOptions::degraded(vec![
            "falkordb_unavailable".to_string(),
            "semantic_relations_unavailable".to_string(),
        ]),
    )
    .expect("graph artifacts exported");

    assert_eq!(artifacts.len(), 2);
    assert_eq!(artifacts[0].path, root.join("outputs/graph.json"));
    assert_eq!(artifacts[1].path, root.join("outputs/GRAPH_REPORT.md"));

    let graph_json: serde_json::Value = serde_json::from_str(
        &fs::read_to_string(root.join("outputs/graph.json")).expect("graph json"),
    )
    .expect("valid graph json");
    assert_eq!(graph_json["degraded"], true);
    assert_eq!(
        graph_json["degraded_sources"],
        serde_json::json!(["falkordb_unavailable", "semantic_relations_unavailable"])
    );
    assert_eq!(graph_json["nodes"].as_array().expect("nodes").len(), 5);
    assert_eq!(
        graph_json["edges"]["links"]
            .as_array()
            .expect("links")
            .len(),
        1
    );
    assert_eq!(
        graph_json["edges"]["imports"].as_array().expect("imports"),
        &Vec::<serde_json::Value>::new()
    );
    assert_eq!(
        graph_json["edges"]["calls"].as_array().expect("calls"),
        &Vec::<serde_json::Value>::new()
    );
    assert_eq!(
        graph_json["edges"]["trust"]
            .as_array()
            .expect("trust")
            .len(),
        1
    );
    assert_eq!(
        graph_json["edges"]["audit"]
            .as_array()
            .expect("audit")
            .len(),
        1
    );
    let central_node_id = graph_json["analytics"]["centrality"][0]["node"]["id"]
        .as_str()
        .expect("central node id");
    assert!(central_node_id.starts_with("document-knowledge-topics-overview-md-"));
    assert_eq!(
        graph_json["analytics"]["centrality"][0]["degree"],
        serde_json::json!(2)
    );
    // Weighted Leiden clusters the citation→source→overview→lib.rs chain
    // into two 2-node communities (split at the middle "supports" edge),
    // plus the isolated design.md singleton — three communities total. The
    // first (sorted by smallest member id) is the {citation, source} pair.
    assert_eq!(
        graph_json["analytics"]["communities"][0]["nodes"]
            .as_array()
            .expect("community nodes")
            .len(),
        2
    );

    let report = fs::read_to_string(root.join("outputs/GRAPH_REPORT.md")).expect("graph report");
    assert!(report.contains("# GWiki Graph Report"));
    assert!(report.contains("## Analytics"));
    assert!(report.contains(&format!("- Top central node: {central_node_id} (degree 2)")));
    assert!(report.contains("- Communities: 3"));
    assert!(report.contains("## Degraded sources"));
    assert!(report.contains("- falkordb_unavailable"));
    assert!(report.contains("```mermaid"));
    assert!(report.contains(" --> "));
}

#[test]
fn graph_export_removes_json_when_report_write_fails() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let outputs = root.join("outputs");
    fs::create_dir_all(&outputs).expect("outputs dir");
    fs::write(outputs.join("graph.json"), "existing graph").expect("existing graph");
    fs::create_dir(outputs.join("GRAPH_REPORT.md")).expect("blocking dir");

    let error = export_graph_artifacts(
        root,
        &WikiGraphFacts::default(),
        GraphExportOptions::available(),
    )
    .expect_err("report path directory should fail");

    assert!(error.to_string().contains("GRAPH_REPORT.md"));
    assert_eq!(
        fs::read_to_string(outputs.join("graph.json")).expect("preserved graph"),
        "existing graph"
    );
}

#[test]
fn graph_export_errors_are_invalid_input() {
    let error = graph_export_error(GraphAnalyticsError::DuplicateNode {
        id: "node-1".to_string(),
        existing_kind: "topic".to_string(),
        duplicate_kind: "source".to_string(),
        existing_weight: 1.0,
        duplicate_weight: 0.5,
    });

    assert!(matches!(
        error,
        WikiError::InvalidInput {
            field: "graph",
            message
        } if message.contains("duplicate graph node `node-1`")
    ));
}

fn agent_export_facts(scope: SearchScope) -> WikiGraphFacts {
    WikiGraphFacts {
        documents: vec![
            WikiGraphDocument {
                scope: scope.clone(),
                path: "knowledge/topics/overview.md".into(),
                title: Some("Overview".to_string()),
            },
            WikiGraphDocument {
                scope: scope.clone(),
                path: "code/src/lib.rs".into(),
                title: Some("lib.rs".to_string()),
            },
            WikiGraphDocument {
                scope: scope.clone(),
                path: "documents/design.md".into(),
                title: Some("Design Notes".to_string()),
            },
        ],
        links: vec![WikiGraphLink {
            scope: scope.clone(),
            source_path: "knowledge/topics/overview.md".into(),
            raw_target: "[[Design Notes]]".to_string(),
            target: WikiGraphLinkTarget::Resolved("documents/design.md".into()),
        }],
        sources: vec![WikiGraphSource {
            scope: scope.clone(),
            source_path: "raw/sources/example.md".into(),
            document_path: "knowledge/topics/overview.md".into(),
        }],
        code_edges: vec![WikiGraphCodeEdge {
            scope,
            document_path: "code/src/lib.rs".into(),
            source: "alpha".to_string(),
            target: "beta".to_string(),
            kind: "imports".to_string(),
            direction: "out".to_string(),
            line: Some(3),
            provenance: "code-edge-must-be-excluded".to_string(),
        }],
    }
}

#[test]
fn agent_exports_emit_index_jsonld_and_content() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let overview = root.join("knowledge/topics/overview.md");
    fs::create_dir_all(overview.parent().expect("overview parent")).expect("overview dir");
    fs::write(&overview, "# Overview\n\nVault entrypoint.\n").expect("overview page");

    let artifacts = export_agent_artifacts(
        root,
        &agent_export_facts(SearchScope::project("project-123")),
        GraphExportOptions::available(),
    )
    .expect("agent artifacts exported");

    assert_eq!(
        artifacts
            .iter()
            .map(|artifact| artifact.path.clone())
            .collect::<Vec<_>>(),
        vec![
            root.join("outputs/graph.jsonld"),
            root.join("outputs/llms.txt"),
            root.join("outputs/llms-full.txt"),
        ]
    );

    // graph.jsonld: schema.org JSON-LD of the vault document graph.
    let jsonld_text = fs::read_to_string(root.join("outputs/graph.jsonld")).expect("graph jsonld");
    let jsonld: serde_json::Value = serde_json::from_str(&jsonld_text).expect("valid graph jsonld");
    assert_eq!(jsonld["@context"], "https://schema.org");
    let graph = jsonld["@graph"].as_array().expect("@graph array");
    // Vault nodes only: 3 documents + 1 source + 1 citation. No code endpoints.
    assert_eq!(graph.len(), 5);

    let overview_entity = graph
        .iter()
        .find(|entity| entity["url"] == serde_json::json!("knowledge/topics/overview.md"))
        .expect("overview entity");
    assert_eq!(overview_entity["@type"], "Article");
    assert_eq!(overview_entity["genre"], "wiki_page");
    assert_eq!(overview_entity["name"], "Overview");
    // Wikilink → cites the resolved target page.
    let design_id = graph
        .iter()
        .find(|entity| entity["url"] == serde_json::json!("documents/design.md"))
        .expect("design entity")["@id"]
        .clone();
    assert_eq!(
        overview_entity["citation"],
        serde_json::json!([{ "@id": design_id }])
    );
    // Source provenance → isBasedOn the supporting source.
    let source_id = graph
        .iter()
        .find(|entity| entity["genre"] == serde_json::json!("source"))
        .expect("source entity")["@id"]
        .clone();
    assert_eq!(
        overview_entity["isBasedOn"],
        serde_json::json!([{ "@id": source_id }])
    );

    let lib_entity = graph
        .iter()
        .find(|entity| entity["url"] == serde_json::json!("code/src/lib.rs"))
        .expect("lib entity");
    assert_eq!(lib_entity["@type"], "SoftwareSourceCode");

    // The code graph is excluded: no code-edge endpoints or edge classes.
    assert!(!jsonld_text.contains("code-edge-must-be-excluded"));
    assert!(!jsonld_text.contains("\"imports\""));
    assert!(!jsonld_text.contains("\"calls\""));

    // llms.txt: portable index with link sections.
    let index = fs::read_to_string(root.join("outputs/llms.txt")).expect("llms index");
    assert!(index.starts_with("# GWiki Vault Index"));
    assert!(
        index.contains("> Static agent index for project project-123. 3 documents, 1 sources.")
    );
    assert!(index.contains("## Documents"));
    assert!(index.contains("- [Overview](knowledge/topics/overview.md)"));
    assert!(index.contains("- [Design Notes](documents/design.md)"));
    assert!(index.contains("## Sources"));
    assert!(index.contains("- [raw/sources/example.md](raw/sources/example.md)"));

    // llms-full.txt: real content for present docs, placeholder for missing ones.
    let full = fs::read_to_string(root.join("outputs/llms-full.txt")).expect("llms full");
    assert!(full.starts_with("# GWiki Vault Content"));
    assert!(full.contains("## Overview"));
    assert!(full.contains("`knowledge/topics/overview.md`"));
    assert!(full.contains("Vault entrypoint."));
    // design.md was never written to disk, so it degrades gracefully.
    assert!(full.contains("## Design Notes"));
    assert!(full.contains("_(content unavailable)_"));
}

#[test]
fn agent_exports_do_not_mutate_vault() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let overview = root.join("knowledge/topics/overview.md");
    fs::create_dir_all(overview.parent().expect("overview parent")).expect("overview dir");
    fs::write(&overview, "# Overview\n\nVault entrypoint.\n").expect("overview page");
    let before = fs::read_to_string(&overview).expect("read before");

    export_agent_artifacts(
        root,
        &agent_export_facts(SearchScope::project("project-123")),
        GraphExportOptions::available(),
    )
    .expect("agent artifacts exported");

    assert_eq!(fs::read_to_string(&overview).expect("read after"), before);
}

#[test]
fn agent_exports_clean_up_on_write_failure() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let outputs = root.join("outputs");
    fs::create_dir_all(&outputs).expect("outputs dir");
    fs::write(outputs.join("graph.jsonld"), "existing graph").expect("existing graph");
    // A directory at the llms.txt path forces the second write to fail.
    fs::create_dir(outputs.join("llms.txt")).expect("blocking dir");

    let error = export_agent_artifacts(
        root,
        &agent_export_facts(SearchScope::project("project-123")),
        GraphExportOptions::available(),
    )
    .expect_err("blocked llms.txt path should fail");

    assert!(error.to_string().contains("llms.txt"));
    assert_eq!(
        fs::read_to_string(outputs.join("graph.jsonld")).expect("preserved graph"),
        "existing graph"
    );
}

#[test]
fn rollback_removes_only_committed_new_exports() {
    let temp = tempfile::tempdir().expect("tempdir");
    let first_temp = temp.path().join("first.tmp");
    let first_target = temp.path().join("first.md");
    let second_target = temp.path().join("second.md");
    fs::write(&first_temp, "first").expect("first temp");

    let mut staged = vec![
        StagedExport {
            artifact: ExportArtifact {
                path: first_target.clone(),
                kind: ExportKind::Report,
                bytes_written: 5,
            },
            temp_path: first_temp,
            backup_path: None,
            committed: false,
        },
        StagedExport {
            artifact: ExportArtifact {
                path: second_target.clone(),
                kind: ExportKind::Report,
                bytes_written: 6,
            },
            temp_path: temp.path().join("missing.tmp"),
            backup_path: None,
            committed: false,
        },
    ];

    commit_staged_exports(&mut staged).expect_err("missing temp fails second commit");
    assert!(staged[0].committed);
    assert!(!staged[1].committed);
    assert_eq!(
        fs::read_to_string(&first_target).expect("first target"),
        "first"
    );

    fs::write(&second_target, "unrelated").expect("unrelated file");
    rollback_staged_exports(&staged);

    assert!(!first_target.exists());
    assert_eq!(
        fs::read_to_string(&second_target).expect("unrelated preserved"),
        "unrelated"
    );
}

#[cfg(unix)]
#[test]
fn agent_exports_do_not_follow_document_symlink_outside_vault() {
    use std::os::unix::fs::symlink;

    let temp = tempfile::tempdir().expect("tempdir");
    let outside = tempfile::tempdir().expect("outside tempdir");
    let root = temp.path();
    let secret = outside.path().join("secret.md");
    fs::write(&secret, "classified outside vault").expect("secret file");

    let design = root.join("documents/design.md");
    fs::create_dir_all(design.parent().expect("design parent")).expect("design dir");
    symlink(&secret, &design).expect("design symlink");

    export_agent_artifacts(
        root,
        &agent_export_facts(SearchScope::project("project-123")),
        GraphExportOptions::available(),
    )
    .expect("agent artifacts exported");

    let full = fs::read_to_string(root.join("outputs/llms-full.txt")).expect("llms full");
    assert!(full.contains("## Design Notes"));
    assert!(!full.contains("classified outside vault"));
    assert!(full.contains("_(content unavailable)_"));
}