| 1 | import { AssistantUnrolled } from "@continuedev/config-yaml"; |
| 2 | import { render } from "ink-testing-library"; |
| 3 | import React from "react"; |
| 4 | import { describe, expect, it, vi } from "vitest"; |
| 5 | |
| 6 | import { IntroMessage } from "./IntroMessage.js"; |
| 7 | |
| 8 | // Mock the MCPService |
| 9 | vi.mock("../services/MCPService.js", () => ({ |
| 10 | MCPService: vi.fn().mockImplementation(() => ({ |
| 11 | getState: () => ({ prompts: [] }), |
| 12 | })), |
| 13 | })); |
| 14 | |
| 15 | // Mock the model capability check |
| 16 | vi.mock("../utils/modelCapability.js", () => ({ |
| 17 | isModelCapable: vi.fn().mockReturnValue(true), |
| 18 | })); |
| 19 | |
| 20 | describe("IntroMessage", () => { |
| 21 | const mockMcpService = { |
| 22 | getState: () => ({ prompts: [] }), |
| 23 | } as any; |
| 24 | |
| 25 | const mockModel = { |
| 26 | provider: "anthropic", |
| 27 | name: "claude-3-sonnet", |
| 28 | model: "claude-3-sonnet", |
| 29 | } as any; |
| 30 | |
| 31 | it("should not duplicate rules from command line", () => { |
| 32 | // Config with rules that were already injected from command line |
| 33 | // Hub rules are stored as RuleObject with name and rule fields |
| 34 | const config: AssistantUnrolled = { |
| 35 | name: "Test Assistant", |
| 36 | rules: [ |
| 37 | { name: "nate/spanish", rule: "Always respond in Spanish." }, |
| 38 | "Always be helpful", |
| 39 | ], |
| 40 | } as any; |
| 41 | |
| 42 | const { lastFrame } = render( |
| 43 | <IntroMessage |
| 44 | config={config} |
| 45 | model={mockModel} |
| 46 | mcpService={mockMcpService} |
| 47 | />, |
| 48 | ); |
| 49 | |
| 50 | const output = lastFrame(); |
| 51 | |
| 52 | // Count occurrences of "nate/spanish" in the output |
| 53 | const matches = output?.match(/nate\/spanish/g) || []; |
| 54 | |
| 55 | // Should appear exactly once, not twice |
| 56 | expect(matches.length).toBe(1); |
| 57 | |
| 58 | // Verify the slug is shown, not the content |
| 59 | expect(output).toContain("nate/spanish"); |
| 60 | expect(output).not.toContain("Always respond in Spanish"); |
| 61 | |
| 62 | // Verify plain string rule is shown as-is |
| 63 | expect(output).toContain("Always be helpful"); |
| 64 | }); |
| 65 | |
| 66 | it("should display rules from config correctly", () => { |
| 67 | const config: AssistantUnrolled = { |
| 68 | name: "Test Assistant", |
| 69 | rules: [ |
| 70 | "Rule 1", |
| 71 | { name: "Rule 2" }, |
| 72 | "Rule 3", |
| 73 | ], |
| 74 | } as any; |
| 75 | |
| 76 | const { lastFrame } = render( |
| 77 | <IntroMessage |
| 78 | config={config} |
| 79 | model={mockModel} |
| 80 | mcpService={mockMcpService} |
| 81 | />, |
| 82 | ); |
| 83 | |
| 84 | const output = lastFrame(); |
| 85 | |
| 86 | expect(output).toContain("Rules:"); |
| 87 | expect(output).toContain("Rule 1"); |
| 88 | expect(output).toContain("Rule 2"); |
| 89 | expect(output).toContain("Rule 3"); |
| 90 | }); |
| 91 | |
| 92 | it("should not show Rules section when no rules exist", () => { |
| 93 | const config: AssistantUnrolled = { |
| 94 | name: "Test Assistant", |
| 95 | } as any; |
| 96 | |
| 97 | const { lastFrame } = render( |
| 98 | <IntroMessage |
| 99 | config={config} |
| 100 | model={mockModel} |
| 101 | mcpService={mockMcpService} |
| 102 | />, |
| 103 | ); |
| 104 | |
| 105 | const output = lastFrame(); |
| 106 | |
| 107 | expect(output).not.toContain("Rules:"); |
| 108 | }); |
| 109 | |
| 110 | it("should handle rule objects with missing name property", () => { |
| 111 | const config: AssistantUnrolled = { |
| 112 | name: "Test Assistant", |
| 113 | rules: [ |
| 114 | "String rule", |
| 115 | { someOtherProp: "value" }, // Missing 'name' property |
| 116 | ], |
| 117 | } as any; |
| 118 | |
| 119 | const { lastFrame } = render( |
| 120 | <IntroMessage |
| 121 | config={config} |
| 122 | model={mockModel} |
| 123 | mcpService={mockMcpService} |
| 124 | />, |
| 125 | ); |
| 126 | |
| 127 | const output = lastFrame(); |
| 128 | |
| 129 | expect(output).toContain("String rule"); |
| 130 | expect(output).toContain("Unknown"); // Should show "Unknown" for objects without name |
| 131 | }); |
| 132 | |
| 133 | it("should display hub rule slugs, not their content", () => { |
| 134 | const config: AssistantUnrolled = { |
| 135 | name: "Test Assistant", |
| 136 | rules: [ |
| 137 | { |
| 138 | name: "org/complex-rule", |
| 139 | rule: `--- |
| 140 | alwaysApply: true |
| 141 | --- |
| 142 | |
| 143 | This is a very long rule content that should not be displayed. |
| 144 | It contains multiple lines and complex formatting. |
| 145 | The UI should only show the slug 'org/complex-rule'.` |
| 146 | }, |
| 147 | { name: "another/rule", rule: "Short content" }, |
| 148 | ], |
| 149 | } as any; |
| 150 | |
| 151 | const { lastFrame } = render( |
| 152 | <IntroMessage |
| 153 | config={config} |
| 154 | model={mockModel} |
| 155 | mcpService={mockMcpService} |
| 156 | />, |
| 157 | ); |
| 158 | |
| 159 | const output = lastFrame(); |
| 160 | |
| 161 | // Should show slugs only |
| 162 | expect(output).toContain("org/complex-rule"); |
| 163 | expect(output).toContain("another/rule"); |
| 164 | |
| 165 | // Should NOT show the rule content |
| 166 | expect(output).not.toContain("This is a very long rule"); |
| 167 | expect(output).not.toContain("alwaysApply"); |
| 168 | expect(output).not.toContain("Short content"); |
| 169 | }); |
| 170 | }); |