Elffuss BraidHub

kikocis/continue public

2d87ab11542d · 2.1 KiBRawPermalink
1import * as YAML from "yaml";
2import { createRuleBlockImpl } from "./createRuleBlock";
3
4// Mock the extras parameter with necessary functions
5const mockIde = {
6 getWorkspaceDirs: jest.fn().mockResolvedValue(["/"]),
7 writeFile: jest.fn().mockResolvedValue(undefined),
8 openFile: jest.fn().mockResolvedValue(undefined),
9};
10
11const mockExtras = {
12 ide: mockIde,
13};
14
15describe("createRuleBlockImpl", () => {
16 beforeEach(() => {
17 jest.clearAllMocks();
18 });
19
20 it("should create a basic rule with name and rule content", async () => {
21 const args = {
22 name: "Test Rule",
23 rule: "Always write tests",
24 };
25
26 const result = await createRuleBlockImpl(args, mockExtras as any);
27
28 // Verify that writeFile was called with the correct YAML
29 expect(mockIde.writeFile).toHaveBeenCalled();
30 const yamlContent = mockIde.writeFile.mock.calls[0][1];
31 const parsedYaml = YAML.parse(yamlContent);
32
33 // Verify the structure of the YAML
34 expect(parsedYaml).toEqual({
35 name: "Test Rule",
36 version: "0.0.1",
37 schema: "v1",
38 rules: [
39 {
40 name: "Test Rule",
41 rule: "Always write tests",
42 },
43 ],
44 });
45 });
46
47 it("should create a rule with glob pattern", async () => {
48 const args = {
49 name: "TypeScript Rule",
50 rule: "Use interfaces for object shapes",
51 globs: "**/*.{ts,tsx}",
52 };
53
54 await createRuleBlockImpl(args, mockExtras as any);
55
56 // Verify the YAML structure with globs
57 const yamlContent = mockIde.writeFile.mock.calls[0][1];
58 const parsedYaml = YAML.parse(yamlContent);
59 expect(parsedYaml.rules[0]).toEqual({
60 name: "TypeScript Rule",
61 rule: "Use interfaces for object shapes",
62 globs: "**/*.{ts,tsx}",
63 });
64 });
65
66 it("should create a filename based on sanitized rule name", async () => {
67 const args = {
68 name: "Special Ch@racters & Spaces",
69 rule: "Handle special characters",
70 };
71
72 await createRuleBlockImpl(args, mockExtras as any);
73
74 // Check that the filename is sanitized
75 const fileUri = mockIde.writeFile.mock.calls[0][0];
76 expect(fileUri).toContain("special-chracters-spaces.yaml");
77 });
78});