// A daemon E2E cannot catch the browser reading a nonexistent top-level value // and silently ignoring the confirmed configuration after a successful save. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; const source = ( await readFile(new URL('../src/lib/api.ts', import.meta.url), 'utf8') ).replace(/^import type .*;\n/gm, ''); const javascript = stripTypeScriptTypes(source, { mode: 'transform' }); const api = await import( `data:text/javascript;base64,${Buffer.from(javascript).toString('base64')}` ); test('a configuration save returns the confirmed embedded value', async (t) => { const originalFetch = globalThis.fetch; t.after(() => { globalThis.fetch = originalFetch; }); const saved = { id: 'session', preferences: { pinned: true, archived: false }, }; globalThis.fetch = async (_url, options) => { if (options.method === 'PATCH') { assert.equal(options.headers['if-match'], '"observed"'); assert.deepEqual(JSON.parse(options.body), { preferences: { pinned: true }, }); return Response.json({ session: { configuration_resource: { etag: '"saved"', value: saved } }, move: null, }); } return Response.json({}, { headers: { etag: '"observed"' } }); }; assert.deepEqual( await api.configureSession('session', { preferences: { pinned: true } }), saved, ); });