File size: 10,997 Bytes
a20a23c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { build } from 'esbuild';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';

import { createBrowserEnvironment } from './mini-dom.mts';

export { createBrowserEnvironment };

const __dirname = dirname(fileURLToPath(import.meta.url));
const root = resolve(__dirname, '..', '..');
const entry = resolve(root, 'src/components/RuntimeConfigPanel.ts');

function snapshotGlobal(name) {
  return {
    exists: Object.prototype.hasOwnProperty.call(globalThis, name),
    value: globalThis[name],
  };
}

function restoreGlobal(name, snapshot) {
  if (snapshot.exists) {
    globalThis[name] = snapshot.value;
    return;
  }
  delete globalThis[name];
}

function createRuntimeState() {
  return {
    features: [],
    availableIds: new Set(),
    configuredCount: 0,
    listeners: new Set(),
  };
}

async function loadRuntimeConfigPanel() {
  const tempDir = mkdtempSync(join(tmpdir(), 'wm-runtime-config-panel-'));
  const outfile = join(tempDir, 'RuntimeConfigPanel.bundle.mjs');

  const stubModules = new Map([
    ['runtime-config-stub', `
      const state = globalThis.__wmRuntimeConfigPanelTestState;

      export const RUNTIME_FEATURES = state.features;

      export function getEffectiveSecrets() {
        return [];
      }

      export function getRuntimeConfigSnapshot() {
        const secrets = Object.fromEntries(
          Array.from({ length: state.configuredCount }, (_, index) => [
            'SECRET_' + (index + 1),
            { value: 'set', source: 'vault' },
          ]),
        );
        return { featureToggles: {}, secrets };
      }

      export function getSecretState() {
        return { present: false, valid: false, source: 'missing' };
      }

      export function isFeatureAvailable(featureId) {
        return state.availableIds.has(featureId);
      }

      export function isFeatureEnabled() {
        return true;
      }

      export function setFeatureToggle() {}

      export async function setSecretValue() {}

      export function subscribeRuntimeConfig(listener) {
        state.listeners.add(listener);
        return () => state.listeners.delete(listener);
      }

      export function validateSecret() {
        return { valid: true };
      }

      export async function verifySecretWithApi() {
        return { valid: true };
      }
    `],
    ['runtime-stub', `export function isDesktopRuntime() { return true; }`],
    ['tauri-bridge-stub', `export async function invokeTauri() {}`],
    ['i18n-stub', `export function t(key) { return key; }`],
    ['dom-utils-stub', `
      function append(parent, child) {
        if (child == null || child === false) return;
        if (typeof child === 'string' || typeof child === 'number') {
          parent.appendChild(document.createTextNode(String(child)));
          return;
        }
        parent.appendChild(child);
      }

      export function h(tag, propsOrChild, ...children) {
        const el = document.createElement(tag);
        let allChildren = children;

        if (
          propsOrChild != null &&
          typeof propsOrChild === 'object' &&
          !('tagName' in propsOrChild) &&
          !('textContent' in propsOrChild)
        ) {
          for (const [key, value] of Object.entries(propsOrChild)) {
            if (value == null || value === false) continue;
            if (key === 'className') {
              el.className = value;
            } else if (key === 'style' && typeof value === 'object') {
              Object.assign(el.style, value);
            } else if (key === 'dataset' && typeof value === 'object') {
              Object.assign(el.dataset, value);
            } else if (key.startsWith('on') && typeof value === 'function') {
              el.addEventListener(key.slice(2).toLowerCase(), value);
            } else if (value === true) {
              el.setAttribute(key, '');
            } else {
              el.setAttribute(key, String(value));
            }
          }
        } else {
          allChildren = [propsOrChild, ...children];
        }

        allChildren.forEach((child) => append(el, child));
        return el;
      }

      export function replaceChildren(el, ...children) {
        el.innerHTML = '';
        children.forEach((child) => append(el, child));
      }

      export function trustedHtml(html) {
        return String(html ?? '');
      }

      export function setTrustedHtml(el, html) {
        el.innerHTML = String(html ?? '');
      }

      export function safeHtml() {
        return document.createDocumentFragment();
      }
    `],
    ['analytics-stub', `export function trackPanelResized() {} export function trackFeatureToggle() {}`],
    ['ai-flow-settings-stub', `export function getAiFlowSettings() { return { badgeAnimation: false }; }`],
    ['sanitize-stub', `
      export function escapeHtml(value) { return String(value); }
      export function safeHtmlToString(value) { return String(value ?? ''); }
    `],
    ['ollama-models-stub', `export async function fetchOllamaModels() { return []; }`],
    ['settings-constants-stub', `
      export const SIGNUP_URLS = {};
      export const PLAINTEXT_KEYS = new Set();
      export const MASKED_SENTINEL = '***';
    `],
    ['panel-gating-stub', `
      export const PanelGateReason = { NONE: 'none', ANONYMOUS: 'anonymous', UNVERIFIED: 'unverified', FREE_TIER: 'free_tier' };
      export function getPanelGateReason() { return PanelGateReason.NONE; }
    `],
    ['dodo-checkout-stub', `
      export const DodoPayments = {
        Initialize() {},
        Checkout: {
          open() {},
        },
      };
    `],
    ['dodo-empty-stub', 'export {};'],
  ]);

  const aliasMap = new Map([
    ['@/services/runtime-config', 'runtime-config-stub'],
    ['../services/runtime', 'runtime-stub'],
    ['@/services/runtime', 'runtime-stub'],
    ['../services/tauri-bridge', 'tauri-bridge-stub'],
    ['@/services/tauri-bridge', 'tauri-bridge-stub'],
    ['../services/i18n', 'i18n-stub'],
    ['@/services/i18n', 'i18n-stub'],
    ['../utils/dom-utils', 'dom-utils-stub'],
    ['@/services/analytics', 'analytics-stub'],
    ['@/services/ai-flow-settings', 'ai-flow-settings-stub'],
    ['@/utils/sanitize', 'sanitize-stub'],
    ['@/services/ollama-models', 'ollama-models-stub'],
    ['@/services/settings-constants', 'settings-constants-stub'],
    ['@/services/panel-gating', 'panel-gating-stub'],
    ['dodopayments-checkout', 'dodo-checkout-stub'],
    ['dodopayments', 'dodo-empty-stub'],
    ['@dodopayments/core', 'dodo-empty-stub'],
    ['@dodopayments/convex', 'dodo-empty-stub'],
  ]);

  const plugin = {
    name: 'runtime-config-panel-test-stubs',
    setup(buildApi) {
      buildApi.onResolve({ filter: /.*/ }, (args) => {
        const target = aliasMap.get(args.path);
        return target ? { path: target, namespace: 'stub' } : null;
      });

      buildApi.onLoad({ filter: /.*/, namespace: 'stub' }, (args) => ({
        contents: stubModules.get(args.path),
        loader: 'js',
      }));
    },
  };

  const result = await build({
    entryPoints: [entry],
    bundle: true,
    format: 'esm',
    platform: 'browser',
    target: 'es2020',
    write: false,
    plugins: [plugin],
  });

  writeFileSync(outfile, result.outputFiles[0].text, 'utf8');

  const mod = await import(`${pathToFileURL(outfile).href}?t=${Date.now()}`);
  return {
    RuntimeConfigPanel: mod.RuntimeConfigPanel,
    cleanupBundle() {
      rmSync(tempDir, { recursive: true, force: true });
    },
  };
}

export async function createRuntimeConfigPanelHarness() {
  const originalGlobals = {
    document: snapshotGlobal('document'),
    window: snapshotGlobal('window'),
    localStorage: snapshotGlobal('localStorage'),
    requestAnimationFrame: snapshotGlobal('requestAnimationFrame'),
    cancelAnimationFrame: snapshotGlobal('cancelAnimationFrame'),
  };
  const browserEnvironment = createBrowserEnvironment();
  const runtimeState = createRuntimeState();

  globalThis.document = browserEnvironment.document;
  globalThis.window = browserEnvironment.window;
  globalThis.localStorage = browserEnvironment.localStorage;
  globalThis.requestAnimationFrame = browserEnvironment.requestAnimationFrame;
  globalThis.cancelAnimationFrame = browserEnvironment.cancelAnimationFrame;
  globalThis.__wmRuntimeConfigPanelTestState = runtimeState;

  let RuntimeConfigPanel;
  let cleanupBundle;
  try {
    ({ RuntimeConfigPanel, cleanupBundle } = await loadRuntimeConfigPanel());
  } catch (error) {
    delete globalThis.__wmRuntimeConfigPanelTestState;
    restoreGlobal('document', originalGlobals.document);
    restoreGlobal('window', originalGlobals.window);
    restoreGlobal('localStorage', originalGlobals.localStorage);
    restoreGlobal('requestAnimationFrame', originalGlobals.requestAnimationFrame);
    restoreGlobal('cancelAnimationFrame', originalGlobals.cancelAnimationFrame);
    throw error;
  }
  const activePanels = [];

  function setRuntimeState({
    totalFeatures,
    availableFeatures,
    configuredCount,
  }) {
    runtimeState.features.splice(
      0,
      runtimeState.features.length,
      ...Array.from({ length: totalFeatures }, (_, index) => ({ id: `feature-${index + 1}` })),
    );
    runtimeState.availableIds = new Set(
      runtimeState.features.slice(0, availableFeatures).map((feature) => feature.id),
    );
    runtimeState.configuredCount = configuredCount;
  }

  function createPanel(options = { mode: 'alert' }) {
    const panel = new RuntimeConfigPanel(options);
    activePanels.push(panel);
    return panel;
  }

  function emitRuntimeConfigChange() {
    for (const listener of [...runtimeState.listeners]) {
      listener();
    }
  }

  function isHidden(panel) {
    return panel.getElement().classList.contains('hidden');
  }

  function getAlertState(panel) {
    const match = panel.content.innerHTML.match(/data-alert-state="([^"]+)"/);
    return match?.[1] ?? null;
  }

  function reset() {
    while (activePanels.length > 0) {
      activePanels.pop()?.destroy();
    }
    runtimeState.features.length = 0;
    runtimeState.availableIds = new Set();
    runtimeState.configuredCount = 0;
    runtimeState.listeners.clear();
    browserEnvironment.localStorage.clear();
  }

  function cleanup() {
    reset();
    cleanupBundle();
    delete globalThis.__wmRuntimeConfigPanelTestState;
    restoreGlobal('document', originalGlobals.document);
    restoreGlobal('window', originalGlobals.window);
    restoreGlobal('localStorage', originalGlobals.localStorage);
    restoreGlobal('requestAnimationFrame', originalGlobals.requestAnimationFrame);
    restoreGlobal('cancelAnimationFrame', originalGlobals.cancelAnimationFrame);
  }

  return {
    createPanel,
    emitRuntimeConfigChange,
    getAlertState,
    isHidden,
    reset,
    cleanup,
    setRuntimeState,
  };
}