File size: 2,605 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Plugin state test helpers seed SQLite plugin state fixtures.
import type { PluginStateStoreProbeResult } from "./plugin-state-store.types.js";
import "./plugin-state-store.js";
import "./plugin-state-store.sqlite.js";

// Test-only seed helpers for plugin state. Values are serialized through the
// same JSON storage path used by the production sqlite store.
type PluginStateSeedEntry = {
  pluginId: string;
  namespace: string;
  key: string;
  value: unknown;
  createdAt?: number;
  expiresAt?: number | null;
};

type PluginStateDatabaseSeedEntry = {
  pluginId: string;
  namespace: string;
  key: string;
  valueJson: string;
  createdAt?: number;
  expiresAt?: number | null;
};

type PluginStateSqliteTestApi = {
  probePluginStateStore(): PluginStateStoreProbeResult;
  seedPluginStateDatabaseEntriesForTests(entries: readonly PluginStateDatabaseSeedEntry[]): void;
  setMaxPluginStateEntriesPerPluginForTests(value?: number): void;
};

type PluginStateStoreTestApi = {
  clearPluginStateStoreForTests(): void;
};

function getSqliteTestApi(): PluginStateSqliteTestApi {
  return (globalThis as Record<PropertyKey, unknown>)[
    Symbol.for("openclaw.pluginStateSqliteTestApi")
  ] as PluginStateSqliteTestApi;
}

function getStoreTestApi(): PluginStateStoreTestApi {
  return (globalThis as Record<PropertyKey, unknown>)[
    Symbol.for("openclaw.pluginStateStoreTestApi")
  ] as PluginStateStoreTestApi;
}

export function clearPluginStateStoreForTests(): void {
  getStoreTestApi().clearPluginStateStoreForTests();
}

export function probePluginStateStore(): PluginStateStoreProbeResult {
  return getSqliteTestApi().probePluginStateStore();
}

export function setMaxPluginStateEntriesPerPluginForTests(value?: number): void {
  getSqliteTestApi().setMaxPluginStateEntriesPerPluginForTests(value);
}

/** Seeds plugin state entries for tests without opening public store handles. */
export function seedPluginStateEntriesForTests(entries: PluginStateSeedEntry[]): void {
  if (entries.length === 0) {
    return;
  }

  getSqliteTestApi().seedPluginStateDatabaseEntriesForTests(
    entries.map((entry) => {
      const valueJson = JSON.stringify(entry.value);
      if (valueJson == null) {
        throw new Error("plugin state seed value must be JSON serializable");
      }
      return {
        pluginId: entry.pluginId,
        namespace: entry.namespace,
        key: entry.key,
        valueJson,
        ...(entry.createdAt != null ? { createdAt: entry.createdAt } : {}),
        ...(entry.expiresAt !== undefined ? { expiresAt: entry.expiresAt } : {}),
      };
    }),
  );
}