|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { buildCliErrorReport } from "../../src/discord/utils.js"; |
| 3 | + |
| 4 | +describe("buildCliErrorReport", () => { |
| 5 | + it("should include the error message prominently in the report", () => { |
| 6 | + const errorMsg = 'column "content_hash" of relation "contents" does not exist'; |
| 7 | + const report = buildCliErrorReport({ |
| 8 | + command: "add --format ndjson https://example.com/article", |
| 9 | + args: [], |
| 10 | + exitCode: 1, |
| 11 | + error: errorMsg, |
| 12 | + stderr: "Some stderr content", |
| 13 | + note: "Test error report", |
| 14 | + }); |
| 15 | + |
| 16 | + // Should include the error message section |
| 17 | + expect(report).toContain("--- Error Message ---"); |
| 18 | + expect(report).toContain(errorMsg); |
| 19 | + |
| 20 | + // Error message should appear before stderr section |
| 21 | + const errorIndex = report.indexOf("--- Error Message ---"); |
| 22 | + const stderrIndex = report.indexOf("--- stderr ---"); |
| 23 | + expect(errorIndex).toBeLessThan(stderrIndex); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should include both error and stderr when both are provided", () => { |
| 27 | + const errorMsg = "Processing failed"; |
| 28 | + const stderr = "Some stderr output"; |
| 29 | + const report = buildCliErrorReport({ |
| 30 | + command: "add --format ndjson https://example.com/article", |
| 31 | + args: [], |
| 32 | + exitCode: 1, |
| 33 | + error: errorMsg, |
| 34 | + stderr: stderr, |
| 35 | + }); |
| 36 | + |
| 37 | + expect(report).toContain("--- Error Message ---"); |
| 38 | + expect(report).toContain(errorMsg); |
| 39 | + expect(report).toContain("--- stderr ---"); |
| 40 | + expect(report).toContain(stderr); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should not include error section when error is not provided", () => { |
| 44 | + const report = buildCliErrorReport({ |
| 45 | + command: "add --format ndjson https://example.com/article", |
| 46 | + args: [], |
| 47 | + exitCode: 1, |
| 48 | + stderr: "Some stderr output", |
| 49 | + }); |
| 50 | + |
| 51 | + expect(report).not.toContain("--- Error Message ---"); |
| 52 | + expect(report).toContain("--- stderr ---"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should include RCA for database schema errors based on error message", () => { |
| 56 | + const errorMsg = 'column "content_hash" of relation "contents" does not exist'; |
| 57 | + const report = buildCliErrorReport({ |
| 58 | + command: "add --format ndjson https://example.com/article", |
| 59 | + args: [], |
| 60 | + exitCode: 1, |
| 61 | + error: errorMsg, |
| 62 | + }); |
| 63 | + |
| 64 | + // Previously there was an RCA section here; that has been removed. |
| 65 | + // The report should still contain the error message itself. |
| 66 | + expect(report).toContain(errorMsg); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should include RCA for unique constraint errors", () => { |
| 70 | + const errorMsg = "duplicate key value violates unique constraint"; |
| 71 | + const report = buildCliErrorReport({ |
| 72 | + command: "add --format ndjson https://example.com/article", |
| 73 | + args: [], |
| 74 | + exitCode: 1, |
| 75 | + error: errorMsg, |
| 76 | + }); |
| 77 | + |
| 78 | + // RCA removed; ensure the error text is still present in the report. |
| 79 | + expect(report).toContain(errorMsg); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should include RCA for foreign key errors", () => { |
| 83 | + const errorMsg = "violates foreign key constraint"; |
| 84 | + const report = buildCliErrorReport({ |
| 85 | + command: "add --format ndjson https://example.com/article", |
| 86 | + args: [], |
| 87 | + exitCode: 1, |
| 88 | + error: errorMsg, |
| 89 | + }); |
| 90 | + |
| 91 | + // RCA removed; ensure the error text is still present in the report. |
| 92 | + expect(report).toContain(errorMsg); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should include RCA for processing errors", () => { |
| 96 | + const errorMsg = "failed extracting content"; |
| 97 | + const report = buildCliErrorReport({ |
| 98 | + command: "add --format ndjson https://example.com/article", |
| 99 | + args: [], |
| 100 | + exitCode: 1, |
| 101 | + error: errorMsg, |
| 102 | + }); |
| 103 | + |
| 104 | + // RCA removed; ensure the error text is still present in the report. |
| 105 | + expect(report).toContain(errorMsg); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should handle empty error and stderr gracefully", () => { |
| 109 | + const report = buildCliErrorReport({ |
| 110 | + command: "add --format ndjson https://example.com/article", |
| 111 | + args: [], |
| 112 | + exitCode: 1, |
| 113 | + }); |
| 114 | + |
| 115 | + // Should not have error or stderr sections |
| 116 | + expect(report).not.toContain("--- Error Message ---"); |
| 117 | + expect(report).not.toContain("--- stderr ---"); |
| 118 | + // But should still have basic report structure |
| 119 | + expect(report).toContain("⚠️ CLI Error Report"); |
| 120 | + expect(report).toContain("Exit code: 1"); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should include command and exit code", () => { |
| 124 | + const report = buildCliErrorReport({ |
| 125 | + command: "stats", |
| 126 | + args: [], |
| 127 | + exitCode: 127, |
| 128 | + }); |
| 129 | + |
| 130 | + expect(report).toContain("Command: `stats `"); |
| 131 | + expect(report).toContain("Exit code: 127"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should include spawn error when provided", () => { |
| 135 | + const report = buildCliErrorReport({ |
| 136 | + command: "add --format ndjson https://example.com/article", |
| 137 | + args: [], |
| 138 | + spawnError: "ENOENT: no such file or directory", |
| 139 | + }); |
| 140 | + |
| 141 | + expect(report).toContain("Spawn error: ENOENT: no such file or directory"); |
| 142 | + expect(report).toContain("The CLI failed to start (spawn error)"); |
| 143 | + }); |
| 144 | + |
| 145 | + it("should include note when provided", () => { |
| 146 | + const report = buildCliErrorReport({ |
| 147 | + command: "add --format ndjson https://example.com/article", |
| 148 | + args: [], |
| 149 | + exitCode: 1, |
| 150 | + note: "Observed during user request", |
| 151 | + }); |
| 152 | + |
| 153 | + expect(report).toContain("Note: Observed during user request"); |
| 154 | + }); |
| 155 | +}); |
0 commit comments