import { describe, expect, it } from 'vitest'; import { serializeState, deserializeState, type SerdeType } from './serde'; import { defaultState } from './state'; import type { State } from '$lib/types'; const verifySerde = (state: State, serde?: SerdeType): string => { const serialized = serializeState(state, serde); const deserialized = deserializeState(serialized); expect(deserialized).to.deep.equal(state); return serialized; }; describe('Serde tests', () => { it('should serialize and deserialize with default serde', () => { expect(verifySerde(defaultState)).toMatchInlineSnapshot( '"pako:eNpVjk2Lg0AMhv9KyGkL9Q94WGh1t5fCFurN6SFo7AztfDBGpKj_fcd62c0pvM_zhkzY-JYxx-7px0ZTFKhK5SDNoS50NL1Y6m-QZZ_ziQWsd_ya4fhx8tBrH4Jx993mH1cJium8agyijXssGyre_R_HM5T1mYL4cPtLqtHP8FWbi07n_xMdObW-647yjrKGIhQU3wru0XK0ZNr0_rQmCkWzZYV5WlvuaHiKQuWWpNIg_vpyDeYSB97jEFoSLg3dI9ktXH4B_cJWqw"' ); }); it('should serialize and deserialize with base64 serde', () => { expect(verifySerde(defaultState, 'base64')).toMatchInlineSnapshot( '"base64:eyJjb2RlIjoiZmxvd2NoYXJ0IFREXG4gICAgQVtDaHJpc3RtYXNdIC0tPnxHZXQgbW9uZXl8IEIoR28gc2hvcHBpbmcpXG4gICAgQiAtLT4gQ3tMZXQgbWUgdGhpbmt9XG4gICAgQyAtLT58T25lfCBEW0xhcHRvcF1cbiAgICBDIC0tPnxUd298IEVbaVBob25lXVxuICAgIEMgLS0-fFRocmVlfCBGW2ZhOmZhLWNhciBDYXJdXG4gICIsIm1lcm1haWQiOiJ7XG4gIFwidGhlbWVcIjogXCJkZWZhdWx0XCJcbn0iLCJhdXRvU3luYyI6dHJ1ZSwidXBkYXRlRGlhZ3JhbSI6dHJ1ZX0"' ); }); it('should serialize and deserialize with pako serde', () => { expect(verifySerde(defaultState, 'pako')).toMatchInlineSnapshot( '"pako:eNpVjk2Lg0AMhv9KyGkL9Q94WGh1t5fCFurN6SFo7AztfDBGpKj_fcd62c0pvM_zhkzY-JYxx-7px0ZTFKhK5SDNoS50NL1Y6m-QZZ_ziQWsd_ya4fhx8tBrH4Jx993mH1cJium8agyijXssGyre_R_HM5T1mYL4cPtLqtHP8FWbi07n_xMdObW-647yjrKGIhQU3wru0XK0ZNr0_rQmCkWzZYV5WlvuaHiKQuWWpNIg_vpyDeYSB97jEFoSLg3dI9ktXH4B_cJWqw"' ); }); it('should throw error for unrecognized serde', () => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error expect(() => serializeState(defaultState, 'unknown')).toThrowError( 'Unknown serde type: unknown' ); expect(() => deserializeState('unknown:hello')).toThrowError('Unknown serde type: unknown'); }); });