Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript
3.7 KB
Raw Blame
import { describe, expect, it } from "vitest"
import {
  correctWikiPageRouting,
  parseWikiSchemaRouting,
  validateWikiPageRouting,
} from "./wiki-schema"

const SCHEMA = `# Wiki Schema

## Page Types

| Type | Directory | Purpose |
| ---- | --------- | ------- |
| source | wiki/sources/ | Source summaries |
| concept | wiki/concepts/ | Ideas |
| method | wiki/methods/ | Methods |
| overview | wiki/ | Top-level overview |
`

describe("parseWikiSchemaRouting", () => {
  it("extracts type directories from the Page Types table", () => {
    const routing = parseWikiSchemaRouting(SCHEMA)

    expect(routing.typeDirs).toEqual({
      source: "wiki/sources",
      concept: "wiki/concepts",
      method: "wiki/methods",
      overview: "wiki",
    })
  })

  it("ignores unrelated markdown tables outside the Page Types section", () => {
    const routing = parseWikiSchemaRouting([
      "# Wiki Schema",
      "",
      "| Name | Directory |",
      "| ---- | --------- |",
      "| draft | wiki/drafts/ |",
      "",
      "## Page Types",
      "",
      "| Type | Directory | Purpose |",
      "| ---- | --------- | ------- |",
      "| concept | wiki/concepts/ | Ideas |",
      "",
      "## Examples",
      "",
      "| Type | Directory |",
      "| ---- | --------- |",
      "| person | wiki/people/ |",
    ].join("\n"))

    expect(routing.typeDirs).toEqual({
      concept: "wiki/concepts",
    })
  })

  it("rejects schema directories that escape or are invalid on Windows", () => {
    const routing = parseWikiSchemaRouting([
      "## Page Types",
      "| Type | Directory |",
      "| --- | --- |",
      "| escape | wiki/../outside/ |",
      "| windows | wiki/bad:name/ |",
      "| safe | wiki/安全/ |",
    ].join("\n"))

    expect(routing.typeDirs).toEqual({ safe: "wiki/安全" })
  })
})

describe("correctWikiPageRouting", () => {
  const routing = parseWikiSchemaRouting(SCHEMA)

  it("moves a typed page to its schema directory while preserving its filename", () => {
    expect(correctWikiPageRouting(
      "wiki/concepts/retrieval.md",
      "---\ntype: method\ntitle: Retrieval\n---\n# Retrieval",
      routing,
    )).toEqual({
      path: "wiki/methods/retrieval.md",
      message: 'Auto-routed page type "method" from "wiki/concepts/" to "wiki/methods/".',
    })
  })

  it("leaves unknown and already-correct routes unchanged", () => {
    expect(correctWikiPageRouting(
      "wiki/methods/retrieval.md",
      "---\ntype: method\n---",
      routing,
    ).message).toBeNull()
    expect(correctWikiPageRouting(
      "wiki/custom/item.md",
      "---\ntype: custom\n---",
      routing,
    ).path).toBe("wiki/custom/item.md")
  })
})

describe("validateWikiPageRouting", () => {
  const routing = parseWikiSchemaRouting(SCHEMA)

  it("reports a mismatch between frontmatter type and schema directory", () => {
    const issue = validateWikiPageRouting(
      "wiki/concepts/flash-attention.md",
      [
        "---",
        "type: source",
        "title: Flash Attention",
        "---",
        "",
        "# Flash Attention",
      ].join("\n"),
      routing,
    )

    expect(issue?.message).toContain('type "source" must be under "wiki/sources/"')
  })

  it("allows custom schema types routed by the table", () => {
    expect(
      validateWikiPageRouting(
        "wiki/methods/retrieval.md",
        [
          "---",
          "type: method",
          "title: Retrieval",
          "---",
          "",
          "# Retrieval",
        ].join("\n"),
        routing,
      ),
    ).toBeNull()
  })

  it("does not enforce pages without a parseable type", () => {
    expect(validateWikiPageRouting("wiki/concepts/no-type.md", "# No Type", routing)).toBeNull()
  })
})