|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { findOcPaths, getOcPathDocumentKind, parseOcPath, resolveOcPath } from "./oc-path.js"; |
| 3 | + |
| 4 | +describe("workspace oc-path", () => { |
| 5 | + it("parses supported workspace file paths and decoded segments", () => { |
| 6 | + expect(parseOcPath("oc://policy.jsonc/tools/%5Bid=send-email%5D/sensitivity")).toEqual({ |
| 7 | + filePath: "policy.jsonc", |
| 8 | + segments: ["tools", "[id=send-email]", "sensitivity"], |
| 9 | + }); |
| 10 | + expect(parseOcPath("oc://notes/AGENTS.md/Tools")).toEqual({ |
| 11 | + filePath: "notes/AGENTS.md", |
| 12 | + segments: ["Tools"], |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + it("rejects non oc paths and unsupported file kinds", () => { |
| 17 | + expect(() => parseOcPath("policy.jsonc/tools")).toThrow("must start with oc://"); |
| 18 | + expect(() => parseOcPath("oc://policy.txt/tools")).toThrow( |
| 19 | + "must include a supported workspace file", |
| 20 | + ); |
| 21 | + expect(() => getOcPathDocumentKind("policy.txt")).toThrow("Unsupported OpenClaw path"); |
| 22 | + }); |
| 23 | + |
| 24 | + it("resolves JSONC object, array, negative-index, and predicate paths", () => { |
| 25 | + const content = `{ |
| 26 | + // comments are accepted by the JSONC parser |
| 27 | + tools: [ |
| 28 | + { id: "send-email", sensitivity: "restricted" }, |
| 29 | + { id: "calendar", sensitivity: "normal" }, |
| 30 | + ], |
| 31 | + }`; |
| 32 | + |
| 33 | + expect( |
| 34 | + resolveOcPath({ |
| 35 | + ocPath: "oc://policy.jsonc/tools/0/id", |
| 36 | + content, |
| 37 | + }), |
| 38 | + ).toMatchObject({ kind: "value", value: "send-email" }); |
| 39 | + expect( |
| 40 | + resolveOcPath({ |
| 41 | + ocPath: "oc://policy.jsonc/tools/-1/id", |
| 42 | + content, |
| 43 | + }), |
| 44 | + ).toMatchObject({ kind: "value", value: "calendar" }); |
| 45 | + expect( |
| 46 | + resolveOcPath({ |
| 47 | + ocPath: "oc://policy.jsonc/tools/[id=send-email]/sensitivity", |
| 48 | + content, |
| 49 | + }), |
| 50 | + ).toMatchObject({ kind: "value", value: "restricted" }); |
| 51 | + expect( |
| 52 | + resolveOcPath({ |
| 53 | + ocPath: "oc://policy.jsonc/tools/[id=missing]/sensitivity", |
| 54 | + content, |
| 55 | + }), |
| 56 | + ).toBeUndefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("finds JSONC wildcard matches", () => { |
| 60 | + const matches = findOcPaths({ |
| 61 | + ocPath: "oc://policy.jsonc/tools/*/id", |
| 62 | + content: `{ tools: [{ id: "send-email" }, { id: "calendar" }] }`, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(matches).toEqual([ |
| 66 | + { kind: "value", path: "oc://policy.jsonc/tools/0/id", value: "send-email" }, |
| 67 | + { kind: "value", path: "oc://policy.jsonc/tools/1/id", value: "calendar" }, |
| 68 | + ]); |
| 69 | + }); |
| 70 | + |
| 71 | + it("resolves markdown heading paths without rewriting the document", () => { |
| 72 | + const content = `# Agent |
| 73 | +
|
| 74 | +Intro. |
| 75 | +
|
| 76 | +## Tools |
| 77 | +
|
| 78 | +### send-email |
| 79 | +
|
| 80 | +R5, sensitivity: restricted |
| 81 | +
|
| 82 | +### calendar |
| 83 | +
|
| 84 | +R2, sensitivity: normal |
| 85 | +
|
| 86 | +## Notes |
| 87 | +
|
| 88 | +Other content. |
| 89 | +`; |
| 90 | + |
| 91 | + expect( |
| 92 | + resolveOcPath({ |
| 93 | + ocPath: "oc://AGENTS.md/Tools/send-email", |
| 94 | + content, |
| 95 | + }), |
| 96 | + ).toMatchObject({ |
| 97 | + kind: "markdown-section", |
| 98 | + heading: "send-email", |
| 99 | + line: 7, |
| 100 | + valueText: "R5, sensitivity: restricted", |
| 101 | + }); |
| 102 | + expect( |
| 103 | + resolveOcPath({ |
| 104 | + ocPath: "oc://AGENTS.md/Tools/missing", |
| 105 | + content, |
| 106 | + }), |
| 107 | + ).toBeUndefined(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("finds markdown wildcard heading matches", () => { |
| 111 | + const matches = findOcPaths({ |
| 112 | + ocPath: "oc://AGENTS.md/Tools/*", |
| 113 | + content: `# Agent |
| 114 | +
|
| 115 | +## Tools |
| 116 | +
|
| 117 | +### send-email |
| 118 | +Email body. |
| 119 | +
|
| 120 | +### calendar |
| 121 | +Calendar body. |
| 122 | +`, |
| 123 | + }); |
| 124 | + |
| 125 | + expect(matches.map((match) => match.path)).toEqual([ |
| 126 | + "oc://AGENTS.md/Tools/send-email", |
| 127 | + "oc://AGENTS.md/Tools/calendar", |
| 128 | + ]); |
| 129 | + expect(matches.map((match) => ("heading" in match ? match.heading : ""))).toEqual([ |
| 130 | + "send-email", |
| 131 | + "calendar", |
| 132 | + ]); |
| 133 | + }); |
| 134 | +}); |
0 commit comments