File size: 3,836 Bytes
5448d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**

 * T-08 options-schema tests.

 *

 * Covers `parseOmniRoutePluginOptions(opts)` β€” the strict Zod gate that

 * validates the second-arg `PluginOptions` bag from opencode.json before

 * any hook is wired. Anti-pattern checklist mirrored here:

 *

 *  - `null` / `undefined` must collapse to `{}` (defaults apply downstream).

 *  - Unknown keys must THROW (`.strict()` catches opencode.json typos).

 *  - Validation runs at parse time, not import time (module loads cleanly).

 */

import test from "node:test";
import assert from "node:assert/strict";
import { parseOmniRoutePluginOptions } from "../src/index.js";

test("parseOmniRoutePluginOptions: undefined β†’ {}", () => {
  assert.deepEqual(parseOmniRoutePluginOptions(undefined), {});
});

test("parseOmniRoutePluginOptions: null β†’ {}", () => {
  assert.deepEqual(parseOmniRoutePluginOptions(null), {});
});

test("parseOmniRoutePluginOptions: empty object β†’ {}", () => {
  assert.deepEqual(parseOmniRoutePluginOptions({}), {});
});

test("parseOmniRoutePluginOptions: valid providerId β†’ returns it", () => {
  const r = parseOmniRoutePluginOptions({ providerId: "omniroute-preprod" });
  assert.equal(r.providerId, "omniroute-preprod");
});

test("parseOmniRoutePluginOptions: invalid providerId (special chars) β†’ throws", () => {
  assert.throws(
    () => parseOmniRoutePluginOptions({ providerId: "omniroute prod!" }),
    /providerId.*slug/i
  );
});

test("parseOmniRoutePluginOptions: empty providerId β†’ throws", () => {
  assert.throws(() => parseOmniRoutePluginOptions({ providerId: "" }), /providerId/i);
});

test("parseOmniRoutePluginOptions: valid modelCacheTtl β†’ returns it", () => {
  const r = parseOmniRoutePluginOptions({ modelCacheTtl: 60_000 });
  assert.equal(r.modelCacheTtl, 60_000);
});

test("parseOmniRoutePluginOptions: negative modelCacheTtl β†’ throws", () => {
  assert.throws(() => parseOmniRoutePluginOptions({ modelCacheTtl: -1 }), /modelCacheTtl/i);
});

test("parseOmniRoutePluginOptions: zero modelCacheTtl β†’ throws (positive required)", () => {
  assert.throws(() => parseOmniRoutePluginOptions({ modelCacheTtl: 0 }), /modelCacheTtl/i);
});

test("parseOmniRoutePluginOptions: invalid baseURL (not a URL) β†’ throws", () => {
  assert.throws(() => parseOmniRoutePluginOptions({ baseURL: "not-a-url" }), /baseURL/i);
});

test("parseOmniRoutePluginOptions: unknown key β†’ throws (strict mode catches typos)", () => {
  assert.throws(
    () =>
      parseOmniRoutePluginOptions({
        providerId: "omniroute",
        provider_id: "typo-here",
      }),
    /provider_id|unrecognized/i
  );
});

test("parseOmniRoutePluginOptions: all four fields populated correctly β†’ returns them", () => {
  const opts = {
    providerId: "omniroute-prod",
    displayName: "OmniRoute Production",
    modelCacheTtl: 120_000,
    baseURL: "https://or.example.com/v1",
  };
  const r = parseOmniRoutePluginOptions(opts);
  assert.deepEqual(r, opts);
});

test("parseOmniRoutePluginOptions: error message lists every issue path", () => {
  // Two bad fields at once β†’ error string should mention BOTH.
  try {
    parseOmniRoutePluginOptions({
      providerId: "",
      baseURL: "garbage",
    });
    assert.fail("expected throw");
  } catch (err) {
    const msg = (err as Error).message;
    assert.match(msg, /providerId/);
    assert.match(msg, /baseURL/);
  }
});

test("parseOmniRoutePluginOptions: module import alone does NOT throw", async () => {
  // Re-importing the entry must not trigger validation; validation only fires
  // on explicit parseOmniRoutePluginOptions / OmniRoutePlugin invocation.
  const mod = await import("../src/index.js");
  assert.equal(typeof mod.parseOmniRoutePluginOptions, "function");
});