| 1 | import * as Protocol from "@effect/doctest/Protocol" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | |
| 4 | describe("Protocol", () => { |
| 5 | it("round-trips collector requests", () => { |
| 6 | const id = Protocol.collectorId("/src/a file.ts", "version-1") |
| 7 | |
| 8 | assert.deepStrictEqual(Protocol.request(Protocol.collectorPrefix, id), { |
| 9 | file: "/src/a file.ts", |
| 10 | index: undefined, |
| 11 | version: "version-1" |
| 12 | }) |
| 13 | }) |
| 14 | |
| 15 | it("round-trips snippet requests", () => { |
| 16 | const id = Protocol.snippetId("/src/example.ts", 3, "version-1") |
| 17 | |
| 18 | assert.deepStrictEqual(Protocol.request(Protocol.snippetPrefix, id), { |
| 19 | file: "/src/example.ts", |
| 20 | index: 3, |
| 21 | version: "version-1" |
| 22 | }) |
| 23 | }) |
| 24 | |
| 25 | it("rejects malformed virtual requests", () => { |
| 26 | for ( |
| 27 | const id of [ |
| 28 | "other:file", |
| 29 | Protocol.snippetPrefix, |
| 30 | `${Protocol.snippetPrefix}file=example.ts&index=-1`, |
| 31 | `${Protocol.snippetPrefix}file=example.ts&index=1.5` |
| 32 | ] |
| 33 | ) { |
| 34 | assert.isUndefined(Protocol.request(Protocol.snippetPrefix, id)) |
| 35 | } |
| 36 | }) |
| 37 | |
| 38 | it("round-trips resolved collector identifiers", () => { |
| 39 | const id = Protocol.resolvedId("collector", { |
| 40 | file: "/src/example.ts", |
| 41 | version: "version-1" |
| 42 | }) |
| 43 | |
| 44 | assert.deepStrictEqual(Protocol.resolvedRequest(id), { |
| 45 | file: "/src/example.ts", |
| 46 | index: undefined, |
| 47 | version: "version-1", |
| 48 | kind: "collector" |
| 49 | }) |
| 50 | }) |
| 51 | |
| 52 | it("round-trips resolved snippet identifiers", () => { |
| 53 | const id = Protocol.resolvedId("snippet", { |
| 54 | file: "/src/example.ts", |
| 55 | index: 2 |
| 56 | }) |
| 57 | |
| 58 | assert.deepStrictEqual(Protocol.resolvedRequest(id), { |
| 59 | file: "/src/example.ts", |
| 60 | index: 2, |
| 61 | version: undefined, |
| 62 | kind: "snippet" |
| 63 | }) |
| 64 | }) |
| 65 | |
| 66 | it("rejects malformed resolved identifiers", () => { |
| 67 | for ( |
| 68 | const id of [ |
| 69 | "/src/example.ts", |
| 70 | "/src/example.ts?effect-doctest=unknown", |
| 71 | "/src/example.ts?effect-doctest=snippet", |
| 72 | "/src/example.ts?effect-doctest=snippet&index=one" |
| 73 | ] |
| 74 | ) { |
| 75 | assert.isUndefined(Protocol.resolvedRequest(id)) |
| 76 | } |
| 77 | }) |
| 78 | }) |