File size: 3,950 Bytes
253e783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { readConfigMachineState } from "../state/config-machine-state.js";
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
import {
  markRemoteModelCatalogChecked,
  readRemoteModelCatalog,
  writeRemoteModelCatalog,
} from "./remote-store.js";

const roots: string[] = [];
afterEach(() => {
  closeOpenClawStateDatabaseForTest();
  for (const root of roots.splice(0)) {
    fs.rmSync(root, { recursive: true, force: true });
  }
});

describe("remote model catalog store", () => {
  it("stores one machine-state snapshot and rejects stale refreshes", () => {
    const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-catalog-")));
    roots.push(root);
    const options = { path: path.join(root, "state.sqlite") };
    expect(readRemoteModelCatalog(options)).toBeUndefined();
    expect(
      markRemoteModelCatalogChecked(
        1,
        {
          expected: {
            source_url: "https://catalog.test/one",
            generated_at: 1,
            etag: null,
            last_modified: null,
          },
        },
        options,
      ),
    ).toBe(false);
    expect(readConfigMachineState("modelCatalog.remote", options)).toBeUndefined();
    writeRemoteModelCatalog(
      {
        bundle_json: '{"schemaVersion":1}',
        generated_at: 1,
        min_version: null,
        source_url: "https://catalog.test/one",
        etag: null,
        last_modified: null,
        checked_at: 2,
      },
      options,
    );
    writeRemoteModelCatalog(
      {
        bundle_json: '{"schemaVersion":1,"updated":true}',
        generated_at: 3,
        min_version: "2026.7.0",
        source_url: "https://catalog.test/two",
        etag: '"two"',
        last_modified: null,
        checked_at: 4,
      },
      options,
    );
    const retained = writeRemoteModelCatalog(
      {
        bundle_json: '{"schemaVersion":1,"older":true}',
        generated_at: 2,
        min_version: null,
        source_url: "https://catalog.test/two",
        etag: '"older"',
        last_modified: null,
        checked_at: 5,
      },
      options,
    );
    expect(retained).toMatchObject({ status: "retained-newer", row: { generated_at: 3 } });
    expect(
      writeRemoteModelCatalog(
        {
          bundle_json: '{"schemaVersion":1,"sameGenerationDifferentBody":true}',
          generated_at: 3,
          min_version: null,
          source_url: "https://catalog.test/two",
          etag: '"different"',
          last_modified: null,
          checked_at: 5,
        },
        options,
      ),
    ).toMatchObject({
      status: "retained-newer",
      row: { bundle_json: expect.stringContaining("updated") },
    });
    expect(
      markRemoteModelCatalogChecked(
        5,
        {
          expected: {
            source_url: "https://catalog.test/two",
            generated_at: 3,
            etag: '"older"',
            last_modified: null,
          },
        },
        options,
      ),
    ).toBe(false);
    expect(
      markRemoteModelCatalogChecked(
        6,
        {
          expected: {
            source_url: "https://catalog.test/two",
            generated_at: 3,
            etag: '"two"',
            last_modified: null,
          },
        },
        options,
      ),
    ).toBe(true);
    expect(readRemoteModelCatalog(options)).toMatchObject({
      id: 1,
      generated_at: 3,
      source_url: "https://catalog.test/two",
      checked_at: 6,
    });
    expect(readConfigMachineState("modelCatalog.remote", options)).toEqual({
      bundle_json: '{"schemaVersion":1,"updated":true}',
      generated_at: 3,
      min_version: "2026.7.0",
      source_url: "https://catalog.test/two",
      etag: '"two"',
      last_modified: null,
      checked_at: 6,
    });
  });
});