import { __awaiter } from "tslib";
import { assert, createLogicSuite } from '@genesislcap/foundation-testing';
import { WebStorageSessionProvider } from './session-persistence-provider';
import { createPersistedSession } from './session-snapshot';
// GENC-1351 §5.2: the WebStorageSessionProvider against a fake Storage (no real
// sessionStorage in Node). Covers round-trip, absent/corrupt entries, clear,
// and keyPrefix isolation.
/** Minimal in-memory Storage matching the DOM `Storage` shape the provider uses. */
function fakeStorage() {
    const m = new Map();
    return {
        get length() {
            return m.size;
        },
        clear: () => m.clear(),
        getItem: (k) => (m.has(k) ? m.get(k) : null),
        key: (i) => { var _a; return (_a = [...m.keys()][i]) !== null && _a !== void 0 ? _a : null; },
        removeItem: (k) => {
            m.delete(k);
        },
        setItem: (k, v) => {
            m.set(k, v);
        },
    };
}
const snap = (sessionKey) => createPersistedSession({
    sessionKey,
    host: 'app.example',
    messages: [],
    pinnedAgentName: null,
    flowOwnerAgentName: null,
    sessionCostUsd: 0,
});
const Suite = createLogicSuite('web-storage-session-provider');
Suite('save → load round-trips a snapshot', () => __awaiter(void 0, void 0, void 0, function* () {
    const provider = new WebStorageSessionProvider(fakeStorage());
    const s = snap('s1');
    yield provider.save('s1', s);
    const loaded = yield provider.load('s1');
    // Compare through JSON: createPersistedSession leaves optional keys undefined,
    // which JSON drops — so normalise both sides the same way.
    assert.equal(loaded, JSON.parse(JSON.stringify(s)));
}));
Suite('load returns null for an unknown key', () => __awaiter(void 0, void 0, void 0, function* () {
    const provider = new WebStorageSessionProvider(fakeStorage());
    assert.is(yield provider.load('missing'), null);
}));
Suite('load returns null (not throw) on a corrupt entry', () => __awaiter(void 0, void 0, void 0, function* () {
    const storage = fakeStorage();
    storage.setItem('genesis-ai-session:s1', '{ not valid json');
    const provider = new WebStorageSessionProvider(storage);
    assert.is(yield provider.load('s1'), null);
}));
Suite('clear removes the snapshot', () => __awaiter(void 0, void 0, void 0, function* () {
    const provider = new WebStorageSessionProvider(fakeStorage());
    yield provider.save('s1', snap('s1'));
    yield provider.clear('s1');
    assert.is(yield provider.load('s1'), null);
}));
Suite('keyPrefix isolates snapshots stored in the same Storage', () => __awaiter(void 0, void 0, void 0, function* () {
    const storage = fakeStorage();
    const a = new WebStorageSessionProvider(storage, { keyPrefix: 'appA' });
    const b = new WebStorageSessionProvider(storage, { keyPrefix: 'appB' });
    yield a.save('k', snap('k'));
    assert.is(yield b.load('k'), null);
    assert.ok(yield a.load('k'));
}));
Suite('saveDebounceMs defaults to 0 (web storage is sync) and is overridable', () => {
    assert.is(new WebStorageSessionProvider(fakeStorage()).saveDebounceMs, 0);
    assert.is(new WebStorageSessionProvider(fakeStorage(), { saveDebounceMs: 1500 }).saveDebounceMs, 1500);
});
// GENC-1351: user preferences (remember-chat + display toggles) — persisted
// independently of the snapshot and of the enabled toggle.
Suite('loadPreferences is undefined until set, then round-trips a blob', () => __awaiter(void 0, void 0, void 0, function* () {
    const provider = new WebStorageSessionProvider(fakeStorage());
    assert.is(yield provider.loadPreferences('s1'), undefined, 'never saved → undefined (use defaults)');
    const prefs = {
        enabled: false,
        showToolCalls: true,
        showThinkingSteps: true,
        showNarration: false,
        showAgentSwitchIndicator: true,
        animations: ['dots'],
    };
    yield provider.savePreferences('s1', prefs);
    assert.equal(yield provider.loadPreferences('s1'), prefs);
}));
Suite('loadPreferences returns undefined (not throw) on a corrupt entry', () => __awaiter(void 0, void 0, void 0, function* () {
    const storage = fakeStorage();
    storage.setItem('genesis-ai-session:prefs:s1', '{ not valid json');
    const provider = new WebStorageSessionProvider(storage);
    assert.is(yield provider.loadPreferences('s1'), undefined);
}));
Suite('clear wipes the snapshot but keeps the preferences', () => __awaiter(void 0, void 0, void 0, function* () {
    const provider = new WebStorageSessionProvider(fakeStorage());
    yield provider.save('s1', snap('s1'));
    yield provider.savePreferences('s1', { enabled: false, showToolCalls: true });
    yield provider.clear('s1');
    assert.is(yield provider.load('s1'), null, 'snapshot cleared');
    assert.equal(yield provider.loadPreferences('s1'), { enabled: false, showToolCalls: true }, 'preferences survive clear');
}));
Suite('preferences are keyPrefix-isolated too', () => __awaiter(void 0, void 0, void 0, function* () {
    const storage = fakeStorage();
    const a = new WebStorageSessionProvider(storage, { keyPrefix: 'appA' });
    const b = new WebStorageSessionProvider(storage, { keyPrefix: 'appB' });
    yield a.savePreferences('k', { enabled: true });
    assert.is(yield b.loadPreferences('k'), undefined);
    assert.equal(yield a.loadPreferences('k'), { enabled: true });
}));
Suite.run();
