| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { RuleWithSource } from "../.."; |
| 3 | import { convertRuleBlockToSlashCommand } from "./ruleBlockSlashCommand"; |
| 4 | |
| 5 | describe("convertRuleBlockToSlashCommand", () => { |
| 6 | it("should convert a rule with all properties to a slash command", () => { |
| 7 | const rule: RuleWithSource = { |
| 8 | name: "Code Review", |
| 9 | rule: "Review this code for best practices", |
| 10 | description: "Performs a code review", |
| 11 | source: "rules-block", |
| 12 | ruleFile: "/path/to/rules.yaml", |
| 13 | invokable: true, |
| 14 | }; |
| 15 | |
| 16 | const result = convertRuleBlockToSlashCommand(rule); |
| 17 | |
| 18 | expect(result).toEqual({ |
| 19 | name: "Code Review", |
| 20 | description: "Performs a code review", |
| 21 | prompt: "Review this code for best practices", |
| 22 | source: "invokable-rule", |
| 23 | promptFile: "/path/to/rules.yaml", |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | it("should handle a rule without name", () => { |
| 28 | const rule: RuleWithSource = { |
| 29 | rule: "Review this code", |
| 30 | source: "rules-block", |
| 31 | invokable: true, |
| 32 | }; |
| 33 | |
| 34 | const result = convertRuleBlockToSlashCommand(rule); |
| 35 | |
| 36 | expect(result.name).toBe("Review this code"); |
| 37 | expect(result.prompt).toBe("Review this code"); |
| 38 | }); |
| 39 | |
| 40 | it("should truncate long rule text when used as name", () => { |
| 41 | const rule: RuleWithSource = { |
| 42 | rule: "This is a very long rule text that should be truncated", |
| 43 | source: "rules-block", |
| 44 | invokable: true, |
| 45 | }; |
| 46 | |
| 47 | const result = convertRuleBlockToSlashCommand(rule); |
| 48 | |
| 49 | expect(result.name).toBe("This is a very long ..."); |
| 50 | expect(result.prompt).toBe( |
| 51 | "This is a very long rule text that should be truncated", |
| 52 | ); |
| 53 | }); |
| 54 | |
| 55 | it("should handle a rule without description", () => { |
| 56 | const rule: RuleWithSource = { |
| 57 | name: "Test Rule", |
| 58 | rule: "Test rule content", |
| 59 | source: "rules-block", |
| 60 | invokable: true, |
| 61 | }; |
| 62 | |
| 63 | const result = convertRuleBlockToSlashCommand(rule); |
| 64 | |
| 65 | expect(result.description).toBe(""); |
| 66 | }); |
| 67 | |
| 68 | it("should use rule text as the prompt", () => { |
| 69 | const rule: RuleWithSource = { |
| 70 | name: "Explain Code", |
| 71 | rule: "Explain what this code does in simple terms", |
| 72 | source: "rules-block", |
| 73 | invokable: true, |
| 74 | }; |
| 75 | |
| 76 | const result = convertRuleBlockToSlashCommand(rule); |
| 77 | |
| 78 | expect(result.prompt).toBe("Explain what this code does in simple terms"); |
| 79 | }); |
| 80 | |
| 81 | it("should always set source to 'invokable-rule'", () => { |
| 82 | const rule: RuleWithSource = { |
| 83 | name: "Test", |
| 84 | rule: "Test", |
| 85 | source: "default-chat", |
| 86 | invokable: true, |
| 87 | }; |
| 88 | |
| 89 | const result = convertRuleBlockToSlashCommand(rule); |
| 90 | |
| 91 | expect(result.source).toBe("invokable-rule"); |
| 92 | }); |
| 93 | }); |