import test from 'node:test'; import assert from 'node:assert/strict'; import { mkdtemp, readdir, readFile, rm, stat } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { UserPreferencesStore } from '../server/user-preferences.mjs'; test('user GUI metadata is isolated, durable, private and merges independent writes', async t => { const root = await mkdtemp(join(tmpdir(), 'cms-user-meta-')); t.after(() => rm(root, { recursive: true, force: true })); const store = new UserPreferencesStore(root); assert.equal((await store.read('alice')).exists, false); await Promise.all([ store.patch('alice', { appearance: { theme: 'light', accent: '#AABBCC' } }), store.patch('alice', { gui: { modals: { 'media-library': { width: 1250, height: 800 } } } }), store.patch('bob', { gui: { modals: { 'media-library': { width: 640, height: 480 } } } }), ]); const restarted = new UserPreferencesStore(root); const alice = (await restarted.read('alice')).preferences; assert.equal(alice.appearance.theme, 'light'); assert.equal(alice.appearance.accent, '#aabbcc'); assert.deepEqual(alice.gui.modals['media-library'], {width:1250,height:800}); assert.equal((await restarted.read('bob')).preferences.gui.modals['media-library'].width, 640); assert.deepEqual((await restarted.patch('alice', { appearance: { theme: 'dark' } }, { ifAbsent: true })).preferences, alice); const files = await readdir(join(root,'.user-meta')); assert.equal(files.length, 2); for (const file of files) { assert.match(file, /^[a-f\d]{64}\.json$/); assert.equal((await stat(join(root,'.user-meta',file))).mode & 0o777, 0o600); assert.ok(JSON.parse(await readFile(join(root,'.user-meta',file))).gui); } for (const patch of [{ user: 'bob' }, { gui: { modals: { '../evil': {width:900,height:600} } } }, { gui: {modals: {'media-library': {width:1e7,height:800}}} }, {appearance:{accent:'red'}}, JSON.parse('{"gui":{"modals":{"__proto__":null}}}')]) { await assert.rejects(store.patch('alice', patch), {status:400}); } await assert.rejects(store.read(''), { status: 401 }); assert.deepEqual((await store.read('alice')).preferences, alice); });