File size: 2,187 Bytes
50720ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Tests that model-store writes cache to data/ (gitignored),
 * NOT to config/models.yaml (git-tracked).
 */

import { describe, it, expect, beforeEach, vi } from "vitest";

vi.mock("../../config.js", () => ({
  getConfig: vi.fn(() => ({
    server: {},
    model: { default: "gpt-5.2-codex" },
    api: { base_url: "https://chatgpt.com/backend-api" },
    client: { app_version: "1.0.0" },
  })),
}));

vi.mock("../../paths.js", () => ({
  getConfigDir: vi.fn(() => "/fake/config"),
  getDataDir: vi.fn(() => "/fake/data"),
}));

vi.mock("fs", async (importOriginal) => {
  const actual = await importOriginal<typeof import("fs")>();
  return {
    ...actual,
    readFileSync: vi.fn(() => "models: []\naliases: {}"),
    writeFileSync: vi.fn(),
    writeFile: vi.fn((_p: string, _d: string, _e: string, cb: (err: Error | null) => void) => cb(null)),
    existsSync: vi.fn(() => false),
    mkdirSync: vi.fn(),
  };
});

vi.mock("js-yaml", () => ({
  default: {
    load: vi.fn(() => ({ models: [], aliases: {} })),
    dump: vi.fn(() => "models: []"),
  },
}));

import { writeFile, mkdirSync, existsSync } from "fs";
import { loadStaticModels, applyBackendModels } from "../model-store.js";

describe("model cache writes to data/, not config/", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(existsSync).mockReturnValue(false);
    loadStaticModels();
  });

  it("syncStaticModels writes to data/models-cache.yaml", () => {
    applyBackendModels([{ slug: "gpt-5.4", name: "GPT 5.4" }]);

    expect(writeFile).toHaveBeenCalledOnce();
    const writePath = vi.mocked(writeFile).mock.calls[0][0] as string;
    expect(writePath).toContain("/fake/data/models-cache.yaml");
  });

  it("syncStaticModels never writes to config/models.yaml", () => {
    applyBackendModels([{ slug: "gpt-5.4", name: "GPT 5.4" }]);

    const writePath = vi.mocked(writeFile).mock.calls[0][0] as string;
    expect(writePath).not.toContain("/fake/config/");
  });

  it("ensures data dir exists before writing cache", () => {
    applyBackendModels([{ slug: "gpt-5.4", name: "GPT 5.4" }]);

    expect(mkdirSync).toHaveBeenCalledWith("/fake/data", { recursive: true });
  });
});