File size: 2,258 Bytes
b694417
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterEach, describe, expect, it, vi } from "vitest";

import { getTiers, toClientResponse } from "./client";

afterEach(() => {
  vi.unstubAllGlobals();
  delete (globalThis as typeof globalThis & { __TEXTMOSAIC_CONFIG__?: object })
    .__TEXTMOSAIC_CONFIG__;
});

describe("toClientResponse", () => {
  it("preserves the current one-word API schema at the fetch boundary", () => {
    expect(
      toClientResponse({
        tokens: ["Ada"],
        entities: [{ type: "Peop", start: 0, end: 1 }],
        relations: [],
      }),
    ).toEqual({
      tokens: ["Ada"],
      entities: [{ type: "Peop", start: 0, end: 1 }],
      relations: [],
    });
  });

  it("loads the live model tier contract through the API boundary", async () => {
    const fetchMock = vi.fn().mockResolvedValue(
      new Response(
        JSON.stringify({
          tiers: [
            { name: "speed", description: "Fastest" },
            { name: "balanced", description: "Default" },
            { name: "accuracy", description: "Strongest" },
          ],
        }),
        { status: 200, headers: { "Content-Type": "application/json" } },
      ),
    );
    vi.stubGlobal("fetch", fetchMock);

    await expect(getTiers()).resolves.toEqual([
      { name: "speed", description: "Fastest" },
      { name: "balanced", description: "Default" },
      { name: "accuracy", description: "Strongest" },
    ]);
    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(
      "http://127.0.0.1:7860/tiers",
      expect.objectContaining({
        headers: { "Content-Type": "application/json" },
      }),
    );
  });

  it("uses runtime container configuration instead of a compile-time API URL", async () => {
    (
      globalThis as typeof globalThis & {
        __TEXTMOSAIC_CONFIG__?: { apiBaseUrl: string };
      }
    ).__TEXTMOSAIC_CONFIG__ = { apiBaseUrl: "https://api.example.test/" };
    const fetchMock = vi
      .fn()
      .mockResolvedValue(
        new Response(JSON.stringify({ tiers: [] }), { status: 200 }),
      );
    vi.stubGlobal("fetch", fetchMock);

    await getTiers();

    expect(fetchMock).toHaveBeenCalledWith(
      "https://api.example.test/tiers",
      expect.any(Object),
    );
  });
});