| 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: ["Rule 1", { name: "Rule 2" }, "Rule 3"], |
| 70 | } as any; |
| 71 | |
| 72 | const { lastFrame } = render( |
| 73 | <IntroMessage |
| 74 | config={config} |
| 75 | model={mockModel} |
| 76 | mcpService={mockMcpService} |
| 77 | />, |
| 78 | ); |
| 79 | |
| 80 | const output = lastFrame(); |
| 81 | |
| 82 | expect(output).toContain("Rules:"); |
| 83 | expect(output).toContain("Rule 1"); |
| 84 | expect(output).toContain("Rule 2"); |
| 85 | expect(output).toContain("Rule 3"); |
| 86 | }); |
| 87 | |
| 88 | it("should not show Rules section when no rules exist", () => { |
| 89 | const config: AssistantUnrolled = { |
| 90 | name: "Test Assistant", |
| 91 | } as any; |
| 92 | |
| 93 | const { lastFrame } = render( |
| 94 | <IntroMessage |
| 95 | config={config} |
| 96 | model={mockModel} |
| 97 | mcpService={mockMcpService} |
| 98 | />, |
| 99 | ); |
| 100 | |
| 101 | const output = lastFrame(); |
| 102 | |
| 103 | expect(output).not.toContain("Rules:"); |
| 104 | }); |
| 105 | |
| 106 | it("should handle rule objects with missing name property", () => { |
| 107 | const config: AssistantUnrolled = { |
| 108 | name: "Test Assistant", |
| 109 | rules: [ |
| 110 | "String rule", |
| 111 | { someOtherProp: "value" }, // Missing 'name' property |
| 112 | ], |
| 113 | } as any; |
| 114 | |
| 115 | const { lastFrame } = render( |
| 116 | <IntroMessage |
| 117 | config={config} |
| 118 | model={mockModel} |
| 119 | mcpService={mockMcpService} |
| 120 | />, |
| 121 | ); |
| 122 | |
| 123 | const output = lastFrame(); |
| 124 | |
| 125 | expect(output).toContain("String rule"); |
| 126 | expect(output).toContain("Unknown"); // Should show "Unknown" for objects without name |
| 127 | }); |
| 128 | |
| 129 | it("should display hub rule slugs, not their content", () => { |
| 130 | const config: AssistantUnrolled = { |
| 131 | name: "Test Assistant", |
| 132 | rules: [ |
| 133 | { |
| 134 | name: "org/complex-rule", |
| 135 | rule: `--- |
| 136 | alwaysApply: true |
| 137 | --- |
| 138 | |
| 139 | This is a very long rule content that should not be displayed. |
| 140 | It contains multiple lines and complex formatting. |
| 141 | The UI should only show the slug 'org/complex-rule'.`, |
| 142 | }, |
| 143 | { name: "another/rule", rule: "Short content" }, |
| 144 | ], |
| 145 | } as any; |
| 146 | |
| 147 | const { lastFrame } = render( |
| 148 | <IntroMessage |
| 149 | config={config} |
| 150 | model={mockModel} |
| 151 | mcpService={mockMcpService} |
| 152 | />, |
| 153 | ); |
| 154 | |
| 155 | const output = lastFrame(); |
| 156 | |
| 157 | // Should show slugs only |
| 158 | expect(output).toContain("org/complex-rule"); |
| 159 | expect(output).toContain("another/rule"); |
| 160 | |
| 161 | // Should NOT show the rule content |
| 162 | expect(output).not.toContain("This is a very long rule"); |
| 163 | expect(output).not.toContain("alwaysApply"); |
| 164 | expect(output).not.toContain("Short content"); |
| 165 | }); |
| 166 | |
| 167 | it("should handle partial loading gracefully", () => { |
| 168 | // Test with no config, no model, no mcpService |
| 169 | const { lastFrame: frame1 } = render(<IntroMessage />); |
| 170 | let output = frame1(); |
| 171 | expect(output).toContain("Loading..."); |
| 172 | |
| 173 | // Test with only model missing |
| 174 | const config: AssistantUnrolled = { |
| 175 | name: "Test Assistant", |
| 176 | rules: ["Rule 1"], |
| 177 | } as any; |
| 178 | |
| 179 | const { lastFrame: frame2 } = render( |
| 180 | <IntroMessage config={config} mcpService={mockMcpService} />, |
| 181 | ); |
| 182 | output = frame2(); |
| 183 | expect(output).toContain("Test Assistant"); |
| 184 | expect(output).toContain("Loading..."); |
| 185 | expect(output).toContain("Rule 1"); |
| 186 | |
| 187 | // Test with only config missing |
| 188 | const { lastFrame: frame3 } = render( |
| 189 | <IntroMessage model={mockModel} mcpService={mockMcpService} />, |
| 190 | ); |
| 191 | output = frame3(); |
| 192 | expect(output).toContain("claude-3-sonnet"); |
| 193 | expect(output).not.toContain("Rules:"); |
| 194 | }); |
| 195 | }); |