|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { readdirSync, readFileSync, statSync } from "fs"; |
| 3 | +import { join, relative } from "path"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Structural convention tests for API route error handling (Issue #1118). |
| 7 | + * |
| 8 | + * Validates that API routes performing Supabase mutations use the standard |
| 9 | + * error classification utilities (captureSupabaseError, captureApiError, |
| 10 | + * isForeignKeyViolationError) instead of bare console.error or unclassified |
| 11 | + * catch blocks. This prevents the "forgot to add the check in the new |
| 12 | + * endpoint" pattern that caused repeated Sentry noise bugs. |
| 13 | + */ |
| 14 | + |
| 15 | +const API_DIR = join(__dirname, "..", "..", "app", "api"); |
| 16 | +const PROJECT_ROOT = join(__dirname, "..", "..", ".."); |
| 17 | + |
| 18 | +/** |
| 19 | + * Routes that intentionally skip certain checks. |
| 20 | + * Each entry must have a comment explaining why. |
| 21 | + */ |
| 22 | +const ALLOWLIST: Record<string, string> = { |
| 23 | + // Health endpoint uses raw fetch (no Supabase client), no mutations, |
| 24 | + // and is monitored externally — intentionally silent on errors. |
| 25 | + "src/app/api/health/route.ts": "raw fetch health check, no Supabase client", |
| 26 | +}; |
| 27 | + |
| 28 | +/** Mutation method patterns — calls that write data. */ |
| 29 | +const MUTATION_PATTERN = /\.(?:insert|update|delete|upsert)\s*\(/; |
| 30 | + |
| 31 | +/** Supabase .from() call pattern — indicates Supabase table access. */ |
| 32 | +const FROM_CALL_PATTERN = /\.from\s*\(/; |
| 33 | + |
| 34 | +/** Supabase .rpc() call pattern — indicates Supabase RPC access. */ |
| 35 | +const RPC_CALL_PATTERN = /\.rpc\s*\(/; |
| 36 | + |
| 37 | +/** Sentry capture function patterns. */ |
| 38 | +const CAPTURE_SUPABASE_ERROR = "captureSupabaseError"; |
| 39 | +const CAPTURE_API_ERROR = "captureApiError"; |
| 40 | +const IS_FK_VIOLATION = "isForeignKeyViolationError"; |
| 41 | + |
| 42 | +/** Bare console.error pattern. */ |
| 43 | +const CONSOLE_ERROR_PATTERN = /\bconsole\.error\b/; |
| 44 | + |
| 45 | +/** Catch block pattern — matches `catch (identifier)` or `catch (identifier: type)`. */ |
| 46 | +const CATCH_BLOCK_PATTERN = /\bcatch\s*\(\s*\w+/g; |
| 47 | + |
| 48 | +function collectRouteFiles(dir: string): string[] { |
| 49 | + const files: string[] = []; |
| 50 | + |
| 51 | + function walk(d: string) { |
| 52 | + for (const entry of readdirSync(d)) { |
| 53 | + const full = join(d, entry); |
| 54 | + const stat = statSync(full); |
| 55 | + if (stat.isDirectory()) { |
| 56 | + walk(full); |
| 57 | + } else if (entry === "route.ts") { |
| 58 | + files.push(full); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + walk(dir); |
| 64 | + return files; |
| 65 | +} |
| 66 | + |
| 67 | +function toRelative(absPath: string): string { |
| 68 | + return relative(PROJECT_ROOT, absPath); |
| 69 | +} |
| 70 | + |
| 71 | +describe("API route error-handling consistency", () => { |
| 72 | + const routeFiles = collectRouteFiles(API_DIR); |
| 73 | + |
| 74 | + it("finds at least one API route file", () => { |
| 75 | + expect(routeFiles.length).toBeGreaterThan(0); |
| 76 | + }); |
| 77 | + |
| 78 | + it("routes with mutations import captureSupabaseError or captureApiError", () => { |
| 79 | + const violations: string[] = []; |
| 80 | + |
| 81 | + for (const file of routeFiles) { |
| 82 | + const rel = toRelative(file); |
| 83 | + if (ALLOWLIST[rel]) continue; |
| 84 | + |
| 85 | + const content = readFileSync(file, "utf-8"); |
| 86 | + |
| 87 | + // Skip routes that don't use Supabase at all |
| 88 | + const usesSupabase = |
| 89 | + FROM_CALL_PATTERN.test(content) || RPC_CALL_PATTERN.test(content); |
| 90 | + if (!usesSupabase) continue; |
| 91 | + |
| 92 | + const hasMutations = |
| 93 | + MUTATION_PATTERN.test(content) || RPC_CALL_PATTERN.test(content); |
| 94 | + if (!hasMutations) continue; |
| 95 | + |
| 96 | + const hasCaptureSupabase = content.includes(CAPTURE_SUPABASE_ERROR); |
| 97 | + const hasCaptureApi = content.includes(CAPTURE_API_ERROR); |
| 98 | + |
| 99 | + if (!hasCaptureSupabase && !hasCaptureApi) { |
| 100 | + violations.push( |
| 101 | + `${rel}: performs mutations but does not import ${CAPTURE_SUPABASE_ERROR} or ${CAPTURE_API_ERROR}`, |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + expect( |
| 107 | + violations, |
| 108 | + `API routes with Supabase mutations must import error capture utilities:\n${violations.join("\n")}`, |
| 109 | + ).toEqual([]); |
| 110 | + }); |
| 111 | + |
| 112 | + it("routes with mutations that use .insert/.update/.delete import isForeignKeyViolationError or captureSupabaseError", () => { |
| 113 | + const violations: string[] = []; |
| 114 | + |
| 115 | + for (const file of routeFiles) { |
| 116 | + const rel = toRelative(file); |
| 117 | + if (ALLOWLIST[rel]) continue; |
| 118 | + |
| 119 | + const content = readFileSync(file, "utf-8"); |
| 120 | + |
| 121 | + // Only check routes that perform table mutations (not RPC-only routes) |
| 122 | + const hasTableMutations = MUTATION_PATTERN.test(content); |
| 123 | + if (!hasTableMutations) continue; |
| 124 | + |
| 125 | + // captureSupabaseError already classifies FK violations internally |
| 126 | + // (downgrades to warning level), so either explicit isForeignKeyViolationError |
| 127 | + // checks or captureSupabaseError usage satisfies this requirement. |
| 128 | + const hasFkCheck = content.includes(IS_FK_VIOLATION); |
| 129 | + const hasCaptureSupabase = content.includes(CAPTURE_SUPABASE_ERROR); |
| 130 | + |
| 131 | + if (!hasFkCheck && !hasCaptureSupabase) { |
| 132 | + violations.push( |
| 133 | + `${rel}: performs table mutations but does not import ${IS_FK_VIOLATION} or ${CAPTURE_SUPABASE_ERROR}`, |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + expect( |
| 139 | + violations, |
| 140 | + `API routes with table mutations must handle FK violations (via ${IS_FK_VIOLATION} or ${CAPTURE_SUPABASE_ERROR}):\n${violations.join("\n")}`, |
| 141 | + ).toEqual([]); |
| 142 | + }); |
| 143 | + |
| 144 | + it("catch blocks use captureApiError or captureSupabaseError, not bare console.error", () => { |
| 145 | + const violations: string[] = []; |
| 146 | + |
| 147 | + for (const file of routeFiles) { |
| 148 | + const rel = toRelative(file); |
| 149 | + if (ALLOWLIST[rel]) continue; |
| 150 | + |
| 151 | + const content = readFileSync(file, "utf-8"); |
| 152 | + const lines = content.split("\n"); |
| 153 | + |
| 154 | + // Skip routes without catch blocks |
| 155 | + CATCH_BLOCK_PATTERN.lastIndex = 0; |
| 156 | + if (!CATCH_BLOCK_PATTERN.test(content)) continue; |
| 157 | + |
| 158 | + const hasCaptureSupabase = content.includes(CAPTURE_SUPABASE_ERROR); |
| 159 | + const hasCaptureApi = content.includes(CAPTURE_API_ERROR); |
| 160 | + |
| 161 | + // If the route has catch blocks but no Sentry capture at all, flag it |
| 162 | + if (!hasCaptureSupabase && !hasCaptureApi) { |
| 163 | + violations.push( |
| 164 | + `${rel}: has catch blocks but does not use ${CAPTURE_API_ERROR} or ${CAPTURE_SUPABASE_ERROR}`, |
| 165 | + ); |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + // Check for bare console.error without accompanying Sentry capture |
| 170 | + for (let i = 0; i < lines.length; i++) { |
| 171 | + if (CONSOLE_ERROR_PATTERN.test(lines[i])) { |
| 172 | + violations.push( |
| 173 | + `${rel}:${i + 1}: uses console.error — use ${CAPTURE_API_ERROR} or ${CAPTURE_SUPABASE_ERROR} instead`, |
| 174 | + ); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + expect( |
| 180 | + violations, |
| 181 | + `API route catch blocks must use Sentry capture utilities, not bare console.error:\n${violations.join("\n")}`, |
| 182 | + ).toEqual([]); |
| 183 | + }); |
| 184 | + |
| 185 | + it("routes with Supabase access have at least one catch block with error capture", () => { |
| 186 | + const violations: string[] = []; |
| 187 | + |
| 188 | + for (const file of routeFiles) { |
| 189 | + const rel = toRelative(file); |
| 190 | + if (ALLOWLIST[rel]) continue; |
| 191 | + |
| 192 | + const content = readFileSync(file, "utf-8"); |
| 193 | + |
| 194 | + const usesSupabase = |
| 195 | + FROM_CALL_PATTERN.test(content) || RPC_CALL_PATTERN.test(content); |
| 196 | + if (!usesSupabase) continue; |
| 197 | + |
| 198 | + // Route uses Supabase — it should have error handling |
| 199 | + CATCH_BLOCK_PATTERN.lastIndex = 0; |
| 200 | + const hasCatchBlock = CATCH_BLOCK_PATTERN.test(content); |
| 201 | + const hasCaptureSupabase = content.includes(CAPTURE_SUPABASE_ERROR); |
| 202 | + const hasCaptureApi = content.includes(CAPTURE_API_ERROR); |
| 203 | + |
| 204 | + if (!hasCatchBlock || (!hasCaptureSupabase && !hasCaptureApi)) { |
| 205 | + violations.push( |
| 206 | + `${rel}: uses Supabase but lacks a catch block with ${CAPTURE_API_ERROR}/${CAPTURE_SUPABASE_ERROR}`, |
| 207 | + ); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + expect( |
| 212 | + violations, |
| 213 | + `API routes using Supabase must have catch blocks with Sentry error capture:\n${violations.join("\n")}`, |
| 214 | + ).toEqual([]); |
| 215 | + }); |
| 216 | + |
| 217 | + it("allowlist entries reference existing route files", () => { |
| 218 | + const staleEntries: string[] = []; |
| 219 | + |
| 220 | + for (const rel of Object.keys(ALLOWLIST)) { |
| 221 | + const absPath = join(PROJECT_ROOT, rel); |
| 222 | + try { |
| 223 | + statSync(absPath); |
| 224 | + } catch { |
| 225 | + staleEntries.push(`${rel}: ${ALLOWLIST[rel]}`); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + expect( |
| 230 | + staleEntries, |
| 231 | + `Allowlist contains entries for files that no longer exist — remove them:\n${staleEntries.join("\n")}`, |
| 232 | + ).toEqual([]); |
| 233 | + }); |
| 234 | +}); |
0 commit comments