|
| 1 | +-- Unit tests for the parser. Run with plenary: |
| 2 | +-- :PlenaryBustedFile tests/parse_spec.lua |
| 3 | +-- or from the shell: |
| 4 | +-- nvim --headless -c "PlenaryBustedDirectory tests/" -c "qa!" |
| 5 | + |
| 6 | +local parse = require("effect-error-pretty.parse") |
| 7 | + |
| 8 | +describe("parse.is_ts_source", function() |
| 9 | + it("accepts typescript / ts / vtsls exactly", function() |
| 10 | + assert.is_true(parse.is_ts_source("typescript")) |
| 11 | + assert.is_true(parse.is_ts_source("ts")) |
| 12 | + assert.is_true(parse.is_ts_source("vtsls")) |
| 13 | + end) |
| 14 | + |
| 15 | + it("rejects loose substring matches like 'typos' or 'ts-standard'", function() |
| 16 | + assert.is_false(parse.is_ts_source("typos")) |
| 17 | + assert.is_false(parse.is_ts_source("ts-standard")) |
| 18 | + assert.is_false(parse.is_ts_source("eslint")) |
| 19 | + assert.is_false(parse.is_ts_source(nil)) |
| 20 | + end) |
| 21 | + |
| 22 | + it("honors a custom allowed set", function() |
| 23 | + local allowed = { eslint = true } |
| 24 | + assert.is_true(parse.is_ts_source("eslint", allowed)) |
| 25 | + assert.is_false(parse.is_ts_source("typescript", allowed)) |
| 26 | + end) |
| 27 | +end) |
| 28 | + |
| 29 | +describe("parse.parse — TS core patterns", function() |
| 30 | + it("type_mismatch (TS2322)", function() |
| 31 | + local r = parse.parse("Type 'string' is not assignable to type 'number'.") |
| 32 | + assert.are.equal("type_mismatch", r.kind) |
| 33 | + assert.are.equal("string", r.got) |
| 34 | + assert.are.equal("number", r.expected) |
| 35 | + end) |
| 36 | + |
| 37 | + it("type_mismatch with missing property", function() |
| 38 | + local r = parse.parse( |
| 39 | + "Type '{ x: number; y: number; }' is not assignable to type 'Point'.\n Property 'z' is missing in type '{ x: number; y: number; }' but required in type 'Point'." |
| 40 | + ) |
| 41 | + assert.are.equal("type_mismatch", r.kind) |
| 42 | + assert.are.equal("z", r.missing) |
| 43 | + end) |
| 44 | + |
| 45 | + it("argument type mismatch (TS2345)", function() |
| 46 | + local r = parse.parse("Argument of type 'string' is not assignable to parameter of type 'number'.") |
| 47 | + assert.are.equal("type_mismatch", r.kind) |
| 48 | + assert.are.equal("string", r.got) |
| 49 | + assert.are.equal("number", r.expected) |
| 50 | + end) |
| 51 | + |
| 52 | + it("unknown_property (TS2339)", function() |
| 53 | + local r = parse.parse("Property 'nonexistent' does not exist on type '{ a: number; b: number; }'.") |
| 54 | + assert.are.equal("unknown_property", r.kind) |
| 55 | + assert.are.equal("nonexistent", r.prop) |
| 56 | + end) |
| 57 | + |
| 58 | + it("undefined (TS2304)", function() |
| 59 | + local r = parse.parse("Cannot find name 'someUndeclaredIdentifier'.") |
| 60 | + assert.are.equal("undefined", r.kind) |
| 61 | + assert.are.equal("someUndeclaredIdentifier", r.name) |
| 62 | + end) |
| 63 | + |
| 64 | + it("module_not_found (TS2307)", function() |
| 65 | + local r = parse.parse("Cannot find module './totally/missing/module' or its corresponding type declarations.") |
| 66 | + assert.are.equal("module_not_found", r.kind) |
| 67 | + assert.are.equal("./totally/missing/module", r.path) |
| 68 | + end) |
| 69 | + |
| 70 | + it("export_not_found (TS2305)", function() |
| 71 | + local r = parse.parse("Module '\"fs\"' has no exported member 'nonExistentExport'.") |
| 72 | + assert.are.equal("export_not_found", r.kind) |
| 73 | + assert.are.equal("nonExistentExport", r.member) |
| 74 | + end) |
| 75 | + |
| 76 | + it("implicit_any (TS7006)", function() |
| 77 | + local r = parse.parse("Parameter 'x' implicitly has an 'any' type.") |
| 78 | + assert.are.equal("implicit_any", r.kind) |
| 79 | + assert.are.equal("x", r.name) |
| 80 | + end) |
| 81 | + |
| 82 | + it("nullish", function() |
| 83 | + local r = parse.parse("Object is possibly 'null'.") |
| 84 | + assert.are.equal("nullish", r.kind) |
| 85 | + assert.are.equal("null", r.value) |
| 86 | + end) |
| 87 | + |
| 88 | + it("arg_count (TS2554)", function() |
| 89 | + local r = parse.parse("Expected 2 arguments, but got 1.") |
| 90 | + assert.are.equal("arg_count", r.kind) |
| 91 | + assert.are.equal("1", r.got) |
| 92 | + assert.are.equal("2", r.expected) |
| 93 | + end) |
| 94 | + |
| 95 | + it("const_assign", function() |
| 96 | + local r = parse.parse("Cannot assign to 'PI' because it is a constant.") |
| 97 | + assert.are.equal("const_assign", r.kind) |
| 98 | + assert.are.equal("PI", r.name) |
| 99 | + end) |
| 100 | + |
| 101 | + it("deprecated", function() |
| 102 | + local r = parse.parse("The signature '(): number' of 'oldApi' is deprecated.") |
| 103 | + assert.are.equal("deprecated", r.kind) |
| 104 | + assert.are.equal("oldApi", r.name) |
| 105 | + end) |
| 106 | +end) |
| 107 | + |
| 108 | +describe("parse.parse — Effect patterns", function() |
| 109 | + it("effect_mismatch: missing services (R)", function() |
| 110 | + local r = parse.parse( |
| 111 | + "Type 'Effect<void, DbError, Database | Logger>' is not assignable to type 'Effect<void, DbError, never>'." |
| 112 | + ) |
| 113 | + assert.are.equal("effect_mismatch", r.kind) |
| 114 | + assert.are.equal("effect", r.tag) |
| 115 | + assert.are.equal(1, r.diff_count) |
| 116 | + assert.are.same({ "Database", "Logger" }, r.missing_services) |
| 117 | + assert.are.same({}, r.unhandled_errors) |
| 118 | + assert.is_false(r.success_differs) |
| 119 | + end) |
| 120 | + |
| 121 | + it("effect_mismatch: unhandled errors (E)", function() |
| 122 | + local r = parse.parse( |
| 123 | + "Type 'Effect<User, NetworkError | ParseError, Http>' is not assignable to type 'Effect<User, never, Http>'." |
| 124 | + ) |
| 125 | + assert.are.equal("effect_mismatch", r.kind) |
| 126 | + assert.are.equal(1, r.diff_count) |
| 127 | + assert.are.same({}, r.missing_services) |
| 128 | + assert.are.same({ "NetworkError", "ParseError" }, r.unhandled_errors) |
| 129 | + assert.is_false(r.success_differs) |
| 130 | + end) |
| 131 | + |
| 132 | + it("effect_mismatch: wrong success (A)", function() |
| 133 | + local r = parse.parse( |
| 134 | + "Type 'Effect<User, NetworkError | ParseError, Http>' is not assignable to type 'Effect<string, NetworkError | ParseError, Http>'." |
| 135 | + ) |
| 136 | + assert.are.equal("effect_mismatch", r.kind) |
| 137 | + assert.are.equal(1, r.diff_count) |
| 138 | + assert.is_true(r.success_differs) |
| 139 | + assert.are.equal("User", r.got.A) |
| 140 | + assert.are.equal("string", r.expected.A) |
| 141 | + end) |
| 142 | + |
| 143 | + it("effect_mismatch: scope required", function() |
| 144 | + local r = parse.parse( |
| 145 | + "Type 'Effect<string, never, Scope>' is not assignable to type 'Effect<string, never, never>'." |
| 146 | + ) |
| 147 | + assert.are.equal("effect_mismatch", r.kind) |
| 148 | + assert.is_true(r.scope_required) |
| 149 | + assert.are.same({ "Scope" }, r.missing_services) |
| 150 | + end) |
| 151 | + |
| 152 | + it("effect_mismatch: multi-channel diff", function() |
| 153 | + local r = parse.parse( |
| 154 | + "Type 'Effect<void, DbError | ConfigError, Database | Config>' is not assignable to type 'Effect<void, never, never>'." |
| 155 | + ) |
| 156 | + assert.are.equal("effect_mismatch", r.kind) |
| 157 | + assert.are.equal(2, r.diff_count) |
| 158 | + end) |
| 159 | + |
| 160 | + it("layer_mismatch: ROut widening", function() |
| 161 | + local r = parse.parse( |
| 162 | + "Type 'Layer<Database | Http, never, never>' is not assignable to type 'Layer<Database, never, never>'." |
| 163 | + ) |
| 164 | + assert.are.equal("effect_mismatch", r.kind) |
| 165 | + assert.are.equal("layer", r.tag) |
| 166 | + assert.are.same({ "ROut", "E", "RIn" }, r.labels) |
| 167 | + assert.is_true(r.success_differs) |
| 168 | + end) |
| 169 | + |
| 170 | + it("Effect.Effect namespaced form", function() |
| 171 | + local r = parse.parse( |
| 172 | + "Type 'Effect.Effect<void, never, Database>' is not assignable to type 'Effect.Effect<void, never, never>'." |
| 173 | + ) |
| 174 | + assert.are.equal("effect_mismatch", r.kind) |
| 175 | + assert.are.same({ "Database" }, r.missing_services) |
| 176 | + end) |
| 177 | + |
| 178 | + it("Stream.Stream namespaced form", function() |
| 179 | + local r = parse.parse( |
| 180 | + "Type 'Stream.Stream<number, never, Http>' is not assignable to type 'Stream.Stream<number, never, never>'." |
| 181 | + ) |
| 182 | + assert.are.equal("effect_mismatch", r.kind) |
| 183 | + assert.are.equal("stream", r.tag) |
| 184 | + end) |
| 185 | + |
| 186 | + it("YieldWrap unwrapping", function() |
| 187 | + local r = parse.parse( |
| 188 | + "Type 'YieldWrap<Effect<void, never, Database>>' is not assignable to type 'YieldWrap<Effect<void, never, never>>'." |
| 189 | + ) |
| 190 | + assert.are.equal("effect_mismatch", r.kind) |
| 191 | + assert.are.same({ "Database" }, r.missing_services) |
| 192 | + end) |
| 193 | + |
| 194 | + it("Context.Tag unwrapping in R channel", function() |
| 195 | + local r = parse.parse( |
| 196 | + "Type 'Effect<void, never, Context.Tag<\"Database\", Database>>' is not assignable to type 'Effect<void, never, never>'." |
| 197 | + ) |
| 198 | + assert.are.equal("effect_mismatch", r.kind) |
| 199 | + -- The Tag wrapper is stripped so the diff surfaces the service name only. |
| 200 | + assert.are.same({ "Database" }, r.missing_services) |
| 201 | + end) |
| 202 | + |
| 203 | + it("short signature: Effect<A> defaults E and R to never", function() |
| 204 | + local r = parse.parse("Type 'Effect<number>' is not assignable to type 'Effect<string>'.") |
| 205 | + assert.are.equal("effect_mismatch", r.kind) |
| 206 | + assert.is_true(r.success_differs) |
| 207 | + assert.are.equal("never", r.got.E) |
| 208 | + assert.are.equal("never", r.got.R) |
| 209 | + end) |
| 210 | + |
| 211 | + it("disabled effect: falls back to type_mismatch", function() |
| 212 | + local r = parse.parse( |
| 213 | + "Type 'Effect<void, never, Database>' is not assignable to type 'Effect<void, never, never>'.", |
| 214 | + { effect = false } |
| 215 | + ) |
| 216 | + assert.are.equal("type_mismatch", r.kind) |
| 217 | + end) |
| 218 | +end) |
| 219 | + |
| 220 | +describe("parse.parse — extensibility", function() |
| 221 | + it("extra_patterns runs after builtins and wins on new shapes", function() |
| 222 | + local r = parse.parse("Custom lint: bad-foo at column 3", { |
| 223 | + extra_patterns = { |
| 224 | + function(msg) |
| 225 | + local bad = msg:match("Custom lint: ([%w-]+)") |
| 226 | + if bad then |
| 227 | + return { kind = "custom_lint", rule = bad } |
| 228 | + end |
| 229 | + end, |
| 230 | + }, |
| 231 | + }) |
| 232 | + assert.are.equal("custom_lint", r.kind) |
| 233 | + assert.are.equal("bad-foo", r.rule) |
| 234 | + end) |
| 235 | +end) |
| 236 | + |
| 237 | +describe("parse.union_diff", function() |
| 238 | + it("returns members of a not in b", function() |
| 239 | + assert.are.same({ "A", "C" }, parse.union_diff("A | B | C", "B")) |
| 240 | + end) |
| 241 | + |
| 242 | + it("treats never as empty", function() |
| 243 | + assert.are.same({}, parse.union_diff("never", "A")) |
| 244 | + assert.are.same({ "A" }, parse.union_diff("A", "never")) |
| 245 | + end) |
| 246 | + |
| 247 | + it("preserves generic-bracket depth when splitting", function() |
| 248 | + local diff = parse.union_diff("User<Id | Slug> | Admin", "Admin") |
| 249 | + assert.are.same({ "User<Id | Slug>" }, diff) |
| 250 | + end) |
| 251 | +end) |
| 252 | + |
| 253 | +describe("parse.has_scope", function() |
| 254 | + it("detects Scope in a union", function() |
| 255 | + assert.is_true(parse.has_scope("Scope")) |
| 256 | + assert.is_true(parse.has_scope("Database | Scope")) |
| 257 | + assert.is_true(parse.has_scope("Scope.Scope | Http")) |
| 258 | + assert.is_false(parse.has_scope("Database | Http")) |
| 259 | + assert.is_false(parse.has_scope("never")) |
| 260 | + end) |
| 261 | +end) |
0 commit comments