|
| 1 | +/** |
| 2 | + * Auth Response Contract Tests |
| 3 | + * |
| 4 | + * Ensures auth responses match the contract expected by UI components. |
| 5 | + * These tests verify the AuthRequiredResponse interface is correctly |
| 6 | + * implemented to prevent the "Checking connection..." hang bug. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, expect, test } from "bun:test"; |
| 10 | + |
| 11 | +/** |
| 12 | + * AuthRequiredResponse interface - must match ui-apps/hooks/utils.ts |
| 13 | + * This is the contract between the tool and the UI component. |
| 14 | + * |
| 15 | + * IMPORTANT: If you change this interface, you must also update: |
| 16 | + * - src/tools/auth-status.ts (the tool that returns this) |
| 17 | + * - ui-apps/hooks/utils.ts (the isAuthRequired function) |
| 18 | + */ |
| 19 | +interface AuthRequiredResponse { |
| 20 | + authRequired: true; |
| 21 | + service: string; |
| 22 | + authUrl: string; |
| 23 | + state: string; |
| 24 | + message: string; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Check if a response matches the AuthRequiredResponse interface |
| 29 | + * This mirrors the isAuthRequired function in ui-apps/hooks/utils.ts |
| 30 | + */ |
| 31 | +function isValidAuthRequiredResponse( |
| 32 | + data: unknown |
| 33 | +): data is AuthRequiredResponse { |
| 34 | + if (typeof data !== "object" || data === null) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + const response = data as Record<string, unknown>; |
| 39 | + |
| 40 | + return ( |
| 41 | + response.authRequired === true && |
| 42 | + typeof response.service === "string" && |
| 43 | + typeof response.authUrl === "string" && |
| 44 | + typeof response.state === "string" && |
| 45 | + typeof response.message === "string" |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Required fields for AuthRequiredResponse |
| 51 | + * Used to generate test cases |
| 52 | + */ |
| 53 | +const _REQUIRED_FIELDS = [ |
| 54 | + "authRequired", |
| 55 | + "service", |
| 56 | + "authUrl", |
| 57 | + "state", |
| 58 | + "message", |
| 59 | +] as const; |
| 60 | + |
| 61 | +describe("AuthRequiredResponse contract", () => { |
| 62 | + // Valid response that should pass all checks |
| 63 | + const validResponse: AuthRequiredResponse = { |
| 64 | + authRequired: true, |
| 65 | + service: "X", |
| 66 | + authUrl: "https://twitter.com/oauth/authorize?token=abc123", |
| 67 | + state: "auth-state-xyz", |
| 68 | + message: "Connect your X account to continue.", |
| 69 | + }; |
| 70 | + |
| 71 | + describe("valid responses", () => { |
| 72 | + test("accepts a complete valid response", () => { |
| 73 | + expect(isValidAuthRequiredResponse(validResponse)).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + test("accepts response with extra fields", () => { |
| 77 | + const withExtra = { |
| 78 | + ...validResponse, |
| 79 | + extraField: "should be ignored", |
| 80 | + anotherExtra: 123, |
| 81 | + }; |
| 82 | + expect(isValidAuthRequiredResponse(withExtra)).toBe(true); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe("authRequired field", () => { |
| 87 | + test("rejects when authRequired is missing", () => { |
| 88 | + const { authRequired, ...missing } = validResponse; |
| 89 | + expect(isValidAuthRequiredResponse(missing)).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + test("rejects when authRequired is false", () => { |
| 93 | + const invalid = { ...validResponse, authRequired: false }; |
| 94 | + expect(isValidAuthRequiredResponse(invalid)).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + test("rejects when authRequired is string 'true'", () => { |
| 98 | + const invalid = { ...validResponse, authRequired: "true" }; |
| 99 | + expect(isValidAuthRequiredResponse(invalid)).toBe(false); |
| 100 | + }); |
| 101 | + |
| 102 | + test("rejects when authRequired is 1", () => { |
| 103 | + const invalid = { ...validResponse, authRequired: 1 }; |
| 104 | + expect(isValidAuthRequiredResponse(invalid)).toBe(false); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("service field", () => { |
| 109 | + test("rejects when service is missing", () => { |
| 110 | + const { service, ...missing } = validResponse; |
| 111 | + expect(isValidAuthRequiredResponse(missing)).toBe(false); |
| 112 | + }); |
| 113 | + |
| 114 | + test("rejects when service is not a string", () => { |
| 115 | + const invalid = { ...validResponse, service: 123 }; |
| 116 | + expect(isValidAuthRequiredResponse(invalid)).toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + test("accepts empty string for service", () => { |
| 120 | + const withEmpty = { ...validResponse, service: "" }; |
| 121 | + expect(isValidAuthRequiredResponse(withEmpty)).toBe(true); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe("authUrl field", () => { |
| 126 | + test("rejects when authUrl is missing", () => { |
| 127 | + const { authUrl, ...missing } = validResponse; |
| 128 | + expect(isValidAuthRequiredResponse(missing)).toBe(false); |
| 129 | + }); |
| 130 | + |
| 131 | + test("rejects when authUrl is not a string", () => { |
| 132 | + const invalid = { ...validResponse, authUrl: null }; |
| 133 | + expect(isValidAuthRequiredResponse(invalid)).toBe(false); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe("state field", () => { |
| 138 | + test("rejects when state is missing", () => { |
| 139 | + const { state, ...missing } = validResponse; |
| 140 | + expect(isValidAuthRequiredResponse(missing)).toBe(false); |
| 141 | + }); |
| 142 | + |
| 143 | + test("rejects when state is not a string", () => { |
| 144 | + const invalid = { ...validResponse, state: { id: "test" } }; |
| 145 | + expect(isValidAuthRequiredResponse(invalid)).toBe(false); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe("message field", () => { |
| 150 | + test("rejects when message is missing", () => { |
| 151 | + const { message, ...missing } = validResponse; |
| 152 | + expect(isValidAuthRequiredResponse(missing)).toBe(false); |
| 153 | + }); |
| 154 | + |
| 155 | + test("rejects when message is not a string", () => { |
| 156 | + const invalid = { ...validResponse, message: undefined }; |
| 157 | + expect(isValidAuthRequiredResponse(invalid)).toBe(false); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe("invalid input types", () => { |
| 162 | + test("rejects null", () => { |
| 163 | + expect(isValidAuthRequiredResponse(null)).toBe(false); |
| 164 | + }); |
| 165 | + |
| 166 | + test("rejects undefined", () => { |
| 167 | + expect(isValidAuthRequiredResponse(undefined)).toBe(false); |
| 168 | + }); |
| 169 | + |
| 170 | + test("rejects string", () => { |
| 171 | + expect(isValidAuthRequiredResponse("not an object")).toBe(false); |
| 172 | + }); |
| 173 | + |
| 174 | + test("rejects number", () => { |
| 175 | + expect(isValidAuthRequiredResponse(42)).toBe(false); |
| 176 | + }); |
| 177 | + |
| 178 | + test("rejects array", () => { |
| 179 | + expect(isValidAuthRequiredResponse([validResponse])).toBe(false); |
| 180 | + }); |
| 181 | + |
| 182 | + test("rejects empty object", () => { |
| 183 | + expect(isValidAuthRequiredResponse({})).toBe(false); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe("regression: bug that caused 'Checking connection...' hang", () => { |
| 188 | + test("OLD FORMAT (bug): missing authRequired field is rejected", () => { |
| 189 | + // This was the old format that caused the hang |
| 190 | + const oldBuggyFormat = { |
| 191 | + service: "X", |
| 192 | + authUrl: "https://twitter.com/oauth/authorize?token=abc123", |
| 193 | + state: "auth-state-xyz", |
| 194 | + // Missing: authRequired: true |
| 195 | + // Missing: message |
| 196 | + }; |
| 197 | + |
| 198 | + expect(isValidAuthRequiredResponse(oldBuggyFormat)).toBe(false); |
| 199 | + }); |
| 200 | + |
| 201 | + test("NEW FORMAT (fixed): includes authRequired and message", () => { |
| 202 | + // This is the correct format |
| 203 | + const fixedFormat = { |
| 204 | + authRequired: true, |
| 205 | + service: "X", |
| 206 | + authUrl: "https://twitter.com/oauth/authorize?token=abc123", |
| 207 | + state: "auth-state-xyz", |
| 208 | + message: "Connect your X account to continue.", |
| 209 | + }; |
| 210 | + |
| 211 | + expect(isValidAuthRequiredResponse(fixedFormat)).toBe(true); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
| 215 | + |
| 216 | +describe("auth-status tool response format", () => { |
| 217 | + /** |
| 218 | + * This test documents what the auth-status tool SHOULD return. |
| 219 | + * If this test fails, update src/tools/auth-status.ts to match. |
| 220 | + */ |
| 221 | + test("documents expected auth-required response format", () => { |
| 222 | + // When not authenticated, auth-status should return JSON like this: |
| 223 | + const expectedFormat = { |
| 224 | + authRequired: true, |
| 225 | + service: "X", |
| 226 | + authUrl: "https://twitter.com/i/oauth2/authorize?...", |
| 227 | + state: "ar_xxxxxxxxxxxxx", |
| 228 | + message: "Connect your X account to continue.", |
| 229 | + }; |
| 230 | + |
| 231 | + // Verify the expected format is valid |
| 232 | + expect(isValidAuthRequiredResponse(expectedFormat)).toBe(true); |
| 233 | + |
| 234 | + // Document required fields |
| 235 | + expect(expectedFormat).toHaveProperty("authRequired", true); |
| 236 | + expect(expectedFormat).toHaveProperty("service"); |
| 237 | + expect(expectedFormat).toHaveProperty("authUrl"); |
| 238 | + expect(expectedFormat).toHaveProperty("state"); |
| 239 | + expect(expectedFormat).toHaveProperty("message"); |
| 240 | + }); |
| 241 | + |
| 242 | + test("documents expected success response format", () => { |
| 243 | + // When authenticated, auth-status should return a text message like: |
| 244 | + const expectedSuccessMessage = |
| 245 | + "✓ X connected as @username. You can now post, reply, and view your conversations."; |
| 246 | + |
| 247 | + // The message should contain the checkmark |
| 248 | + expect(expectedSuccessMessage).toContain("✓"); |
| 249 | + |
| 250 | + // The message should mention X |
| 251 | + expect(expectedSuccessMessage).toContain("X connected"); |
| 252 | + }); |
| 253 | +}); |
0 commit comments