| 1 | import { PackageIdentifier } from "../interfaces/slugs.js"; |
| 2 | import { parseMarkdownRuleOrAssistantUnrolled } from "./unroll.js"; |
| 3 | |
| 4 | describe("parseMarkdownRuleOrAssistantUnrolled tests", () => { |
| 5 | it("parses valid YAML content as AssistantUnrolled", () => { |
| 6 | const mockId: PackageIdentifier = { |
| 7 | uriType: "file", |
| 8 | fileUri: "./foo/bar.yaml", |
| 9 | }; |
| 10 | const yamlContent = ` |
| 11 | name: Test Agent |
| 12 | version: 1.0.0 |
| 13 | |
| 14 | models: |
| 15 | - name: model |
| 16 | model: modelname |
| 17 | provider: ollama |
| 18 | `; |
| 19 | const result = parseMarkdownRuleOrAssistantUnrolled(yamlContent, mockId); |
| 20 | expect(result).toHaveProperty("name", "Test Agent"); |
| 21 | expect(result).toHaveProperty("version", "1.0.0"); |
| 22 | expect(result.models?.length).toBe(1); |
| 23 | expect(result.models?.[0]).toHaveProperty("name", "model"); |
| 24 | }); |
| 25 | |
| 26 | it("parses markdown rule content as AssistantUnrolled", () => { |
| 27 | const mockId: PackageIdentifier = { |
| 28 | uriType: "file", |
| 29 | fileUri: "./foo/bar.md", |
| 30 | }; |
| 31 | const markdownContent = ` |
| 32 | --- |
| 33 | name: rulename |
| 34 | description: my rule description |
| 35 | pattern: "**" |
| 36 | --- |
| 37 | This is the rule |
| 38 | `; |
| 39 | const result = parseMarkdownRuleOrAssistantUnrolled( |
| 40 | markdownContent, |
| 41 | mockId, |
| 42 | ); |
| 43 | expect(result).toHaveProperty("name"); |
| 44 | expect(result).toHaveProperty("version"); |
| 45 | expect(Array.isArray(result.rules)).toBe(true); |
| 46 | expect(result.rules).toBeDefined(); |
| 47 | expect(result.rules!.length).toBe(1); |
| 48 | const rule = result.rules![0]; |
| 49 | expect(rule).toHaveProperty("name", "rulename"); |
| 50 | expect(rule).toHaveProperty("description", "my rule description"); |
| 51 | expect(rule).toHaveProperty("globs", "./foo/**/*"); |
| 52 | expect(rule).toHaveProperty("rule", "This is the rule"); |
| 53 | }); |
| 54 | |
| 55 | it("throws error for invalid content in yaml file", () => { |
| 56 | const mockId: PackageIdentifier = { |
| 57 | uriType: "file", |
| 58 | fileUri: "./foo/bar.yaml", |
| 59 | }; |
| 60 | const invalidContent = ` |
| 61 | name: Test Agent |
| 62 | version: 1.0.0 |
| 63 | |
| 64 | model: # should be models |
| 65 | - name: model |
| 66 | model: modelname |
| 67 | provider: ollama |
| 68 | `; |
| 69 | expect(() => |
| 70 | parseMarkdownRuleOrAssistantUnrolled(invalidContent, mockId), |
| 71 | ).toThrow(); |
| 72 | }); |
| 73 | |
| 74 | it("Every non-YAML file is a rule", () => { |
| 75 | const mockId: PackageIdentifier = { |
| 76 | uriType: "file", |
| 77 | fileUri: "./foo/bar", |
| 78 | }; |
| 79 | const dubiousContent = `This is not \nproper #YAML#`; |
| 80 | const result = parseMarkdownRuleOrAssistantUnrolled(dubiousContent, mockId); |
| 81 | expect(result).toHaveProperty("name"); |
| 82 | expect(result).toHaveProperty("version"); |
| 83 | expect(Array.isArray(result.rules)).toBe(true); |
| 84 | expect(result.rules).toBeDefined(); |
| 85 | expect(result.rules!.length).toBe(1); |
| 86 | const rule = result.rules![0]; |
| 87 | expect(rule).toHaveProperty("name", "foo/bar"); |
| 88 | expect(rule).toHaveProperty("globs", "./foo/**/*"); |
| 89 | expect(rule).toHaveProperty("rule", dubiousContent); |
| 90 | }); |
| 91 | }); |