import { describe, expect, it } from 'vitest'; import { API_ENDPOINT, dataPath, MCP_PATH, moveOldStoredKeys, sitePath, socketUrl, } from '../../src/core/site'; function memoryStore(initial: Record) { const items = new Map(Object.entries(initial)); return { items, get length() { return items.size; }, key: (i: number) => [...items.keys()][i] ?? null, getItem: (key: string) => items.get(key) ?? null, setItem: (key: string, value: string) => void items.set(key, value), removeItem: (key: string) => void items.delete(key), }; } describe('site', () => { it('serves the API and the data files under /feuerwerk/', () => { expect(API_ENDPOINT).toBe('/feuerwerk/api/index.php'); expect(dataPath('textures')).toBe('/feuerwerk/data/textures'); }); it('opens the MCP relay on the page’s own host, encrypted on an encrypted page (#429)', () => { expect(MCP_PATH).toBe('/feuerwerk/mcp'); expect(socketUrl('/mcp', { protocol: 'https:', host: 'feuerwerk.nockal.com' })).toBe( 'wss://feuerwerk.nockal.com/mcp', ); expect(socketUrl('/feuerwerk/mcp', { protocol: 'http:', host: '127.0.0.1:8081' })).toBe( 'ws://127.0.0.1:8081/feuerwerk/mcp', ); }); it('takes the path from the folder of the open page', () => { expect(sitePath('/feuerwerk/')).toBe('/feuerwerk/'); expect(sitePath('/feuerwerk/index.html')).toBe('/feuerwerk/'); expect(sitePath('/feuerwerk/play.html')).toBe('/feuerwerk/'); expect(sitePath('/')).toBe('/'); expect(sitePath('/play.html')).toBe('/'); }); it('moves the keys stored under the old name and leaves the others alone', () => { const store = memoryStore({ 'game-maker.panel-sizes': '{"left":300}', 'game-maker.saves.p1.Savegame': '{"Lives":2}', 'other-site.key': 'x', }); expect(moveOldStoredKeys(store)).toBe(2); expect(Object.fromEntries(store.items)).toEqual({ 'feuerwerk.panel-sizes': '{"left":300}', 'feuerwerk.saves.p1.Savegame': '{"Lives":2}', 'other-site.key': 'x', }); }); it('keeps a value the new name already has and drops the old one', () => { const store = memoryStore({ 'game-maker.general-settings': 'old', 'feuerwerk.general-settings': 'new' }); expect(moveOldStoredKeys(store)).toBe(0); expect(Object.fromEntries(store.items)).toEqual({ 'feuerwerk.general-settings': 'new' }); }); it('does nothing the second time', () => { const store = memoryStore({ 'game-maker.folded-sections': '[]' }); moveOldStoredKeys(store); expect(moveOldStoredKeys(store)).toBe(0); expect(Object.fromEntries(store.items)).toEqual({ 'feuerwerk.folded-sections': '[]' }); }); });