|
1 | | -import { existsSync, readdirSync } from "fs"; |
2 | | -import { resolve } from "path"; |
| 1 | +import { existsSync, readdirSync } from "node:fs"; |
| 2 | +import { resolve } from "node:path"; |
3 | 3 | import { describe, expect, test as it } from "vitest"; |
4 | 4 |
|
| 5 | +import { BinaryDecisionDiagram } from "./bdd/BinaryDecisionDiagram"; |
| 6 | +import { decideEndpoint } from "./decideEndpoint"; |
5 | 7 | import { resolveEndpoint } from "./resolveEndpoint"; |
6 | 8 | import { EndpointError } from "./types"; |
7 | 9 |
|
@@ -46,3 +48,59 @@ describe(resolveEndpoint.name, () => { |
46 | 48 | } |
47 | 49 | }); |
48 | 50 | }); |
| 51 | + |
| 52 | +describe(decideEndpoint.name, () => { |
| 53 | + describe("split, ite, and getAttr with negative index", () => { |
| 54 | + const r = 100_000_000; |
| 55 | + |
| 56 | + const conditions = [ |
| 57 | + ["split", [{ ref: "Splittable" }, ".", 0], "parts"], |
| 58 | + ["getAttr", [{ ref: "parts" }, "[-2]"], "tld"], |
| 59 | + ["stringEquals", [{ ref: "tld" }, "com"], "isCom"], |
| 60 | + ]; |
| 61 | + |
| 62 | + const results = [[{ fn: "ite", argv: [{ ref: "isCom" }, "https://api.___.com", "https://api.___.net"] }, {}, {}]]; |
| 63 | + |
| 64 | + const nodes = new Int32Array([ |
| 65 | + 0, |
| 66 | + 0, |
| 67 | + 0, |
| 68 | + // (2, start) - split Splittable by "." with unlimited parts and proceed to node 3. |
| 69 | + 0, |
| 70 | + 3, |
| 71 | + -1, |
| 72 | + // (3) - getAttr [-2] and assign to tld, proceed to node 4. |
| 73 | + 1, |
| 74 | + 4, |
| 75 | + -1, |
| 76 | + // (4) - compare tld to "com" as isCom and proceed to terminal 0. |
| 77 | + 2, |
| 78 | + r + 0, |
| 79 | + r + 0, |
| 80 | + ]); |
| 81 | + |
| 82 | + const bdd = BinaryDecisionDiagram.from(nodes, 2, conditions, results); |
| 83 | + |
| 84 | + it("should resolve endpoint using split + getAttr[-1] + ite", () => { |
| 85 | + const endpoint = decideEndpoint(bdd, { |
| 86 | + endpointParams: { Splittable: "___.com.___" }, |
| 87 | + }); |
| 88 | + expect(endpoint).toEqual({ |
| 89 | + url: new URL("https://api.___.com"), |
| 90 | + properties: {}, |
| 91 | + headers: {}, |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should pick alternate URL when getAttr[-1] yields non-com TLD", () => { |
| 96 | + const endpoint = decideEndpoint(bdd, { |
| 97 | + endpointParams: { Splittable: "___.org.___" }, |
| 98 | + }); |
| 99 | + expect(endpoint).toEqual({ |
| 100 | + url: new URL("https://api.___.net"), |
| 101 | + properties: {}, |
| 102 | + headers: {}, |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments