Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
refactor([issue-3174]): extract sw.js test sandbox to swSandbox.js
[._.]/ Adam Eivy349381407255
diff --git a/client/src/test/sw.test.js b/client/src/test/sw.test.jsindex d64f631a50..782fa4d5eb 100644--- a/client/src/test/sw.test.js+++ b/client/src/test/sw.test.js@@ -1,98 +1,11 @@-import { readFileSync } from 'node:fs';-import { fileURLToPath } from 'node:url';-import vm from 'node:vm';-import path from 'node:path';import { describe, it, expect } from 'vitest';--// sw.js is a plain classic service-worker script (no ES imports/exports — it's-// registered without { type: 'module' }), served as-is from client/public/.-// To exercise navigationHandler's fallback branches without extracting it out-// of the worker, evaluate the real file in an isolated vm sandbox that-// implements just enough of the SW globals (self, caches, fetch, Response) to-// drive its 'fetch' event listener, then read back the Response it produces.--const SW_PATH = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../public/sw.js');-const SW_SOURCE = readFileSync(SW_PATH, 'utf-8');--function createSandbox() {- const listeners = {};- // name -> Map(url -> Response)- const cacheStores = new Map();-- function openCache(name) {- if (!cacheStores.has(name)) cacheStores.set(name, new Map());- const store = cacheStores.get(name);- return {- match: async (req) => store.get(typeof req === 'string' ? req : req.url),- put: async (req, res) => store.set(typeof req === 'string' ? req : req.url, res),- keys: async () => [...store.keys()].map((url) => ({ url })),- delete: async (req) => store.delete(typeof req === 'string' ? req : req.url),- };- }-- const cachesApi = {- open: async (name) => openCache(name),- // Global lookup across every opened cache, mirroring the real Cache Storage API.- match: async (req) => {- const key = typeof req === 'string' ? req : req.url;- for (const store of cacheStores.values()) {- if (store.has(key)) return store.get(key);- }- return undefined;- },- keys: async () => [...cacheStores.keys()],- delete: async (name) => cacheStores.delete(name),- };-- const sandbox = {- caches: cachesApi,- console,- Response,- Request,- URL,- fetch: async () => {- throw new Error('network unavailable');- },- };- sandbox.self = {- addEventListener: (type, handler) => {- listeners[type] = handler;- },- skipWaiting: () => {},- location: { origin: 'https://portos.example' },- registration: {}, // no navigationPreload in this sandbox- clients: { claim: async () => {} },- };- sandbox.globalThis = sandbox;- vm.createContext(sandbox);- vm.runInContext(SW_SOURCE, sandbox, { filename: 'sw.js' });-- return { listeners, cacheStores, cachesApi, openCache };-}--function makeNavigationEvent(url) {- const event = {- request: { method: 'GET', url, mode: 'navigate' },- preloadResponse: Promise.resolve(undefined),- respondWith: (promise) => {- event._responded = promise;- },- waitUntil: () => {},- };- return event;-}--async function dispatchNavigation(listeners, url) {- const event = makeNavigationEvent(url);- listeners.fetch(event);- return event._responded;-}+import { createSwSandbox, dispatchNavigation } from './swSandbox.js';describe('sw.js navigationHandler — offline fallback branches', () => {const NAV_URL = 'https://portos.example/local-llm/playground?backend=ollama&model=x';it('serves the cached offline shell when the network fetch fails and a shell is cached', async () => {- const { listeners, openCache } = createSandbox();+ const { listeners, openCache } = createSwSandbox();const shellCache = openCache('portos-shell-v1');await shellCache.put('/index.html', new Response('<html>shell</html>', { status: 200 }));@@ -103,7 +16,7 @@ describe('sw.js navigationHandler — offline fallback branches', () => {});it('serves an exact cached response for the navigation URL when no shell is cached', async () => {- const { listeners, openCache } = createSandbox();+ const { listeners, openCache } = createSwSandbox();const exactCache = openCache('some-other-cache');await exactCache.put(NAV_URL, new Response('<html>exact match</html>', { status: 200 }));@@ -113,7 +26,7 @@ describe('sw.js navigationHandler — offline fallback branches', () => {});it('returns a renderable fallback page (not Response.error()) when nothing is cached', async () => {- const { listeners } = createSandbox();+ const { listeners } = createSwSandbox();const response = await dispatchNavigation(listeners, NAV_URL);diff --git a/client/src/test/swSandbox.js b/client/src/test/swSandbox.jsnew file mode 100644index 0000000000..5e3aa6134c--- /dev/null+++ b/client/src/test/swSandbox.js@@ -0,0 +1,95 @@+import { readFileSync } from 'node:fs';+import { fileURLToPath } from 'node:url';+import vm from 'node:vm';+import path from 'node:path';++// sw.js is a plain classic service-worker script (no ES imports/exports — it's+// registered without { type: 'module' }), served as-is from client/public/.+// To exercise it in a vitest suite without extracting logic out of the worker,+// evaluate the real file in an isolated vm sandbox implementing just enough of+// the SW globals (self, caches, fetch, Response) to drive its event listeners,+// then read back the Response(s) it produces. Reusable across any sw.js test —+// not just navigationHandler's fallback branches.++const SW_PATH = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../public/sw.js');+const SW_SOURCE = readFileSync(SW_PATH, 'utf-8');++/**+ * Evaluate sw.js in a fresh vm sandbox with an in-memory Cache Storage+ * implementation. Returns the captured event listeners and a cache-seeding+ * helper; the network `fetch` always rejects, so callers exercise the+ * offline/failure paths.+ */+export function createSwSandbox() {+ const listeners = {};+ // name -> Map(url -> Response)+ const cacheStores = new Map();++ function openCache(name) {+ if (!cacheStores.has(name)) cacheStores.set(name, new Map());+ const store = cacheStores.get(name);+ return {+ match: async (req) => store.get(typeof req === 'string' ? req : req.url),+ put: async (req, res) => store.set(typeof req === 'string' ? req : req.url, res),+ keys: async () => [...store.keys()].map((url) => ({ url })),+ delete: async (req) => store.delete(typeof req === 'string' ? req : req.url),+ };+ }++ const cachesApi = {+ open: async (name) => openCache(name),+ // Global lookup across every opened cache, mirroring the real Cache Storage API.+ match: async (req) => {+ const key = typeof req === 'string' ? req : req.url;+ for (const store of cacheStores.values()) {+ if (store.has(key)) return store.get(key);+ }+ return undefined;+ },+ keys: async () => [...cacheStores.keys()],+ delete: async (name) => cacheStores.delete(name),+ };++ const sandbox = {+ caches: cachesApi,+ console,+ Response,+ Request,+ URL,+ fetch: async () => {+ throw new Error('network unavailable');+ },+ };+ sandbox.self = {+ addEventListener: (type, handler) => {+ listeners[type] = handler;+ },+ skipWaiting: () => {},+ location: { origin: 'https://portos.example' },+ registration: {}, // no navigationPreload in this sandbox+ clients: { claim: async () => {} },+ };+ sandbox.globalThis = sandbox;+ vm.createContext(sandbox);+ vm.runInContext(SW_SOURCE, sandbox, { filename: 'sw.js' });++ return { listeners, openCache };+}++export function makeNavigationEvent(url) {+ const event = {+ request: { method: 'GET', url, mode: 'navigate' },+ preloadResponse: Promise.resolve(undefined),+ respondWith: (promise) => {+ event._responded = promise;+ },+ waitUntil: () => {},+ };+ return event;+}++export async function dispatchNavigation(listeners, url) {+ const event = makeNavigationEvent(url);+ listeners.fetch(event);+ return event._responded;+}