openpencil/packages/kiwi/tests/protocol.test.ts
Danila Poyarkov 7f91594d0c refactor: standardize acronym casing
- Rename first-party API, RPC, JSON, CORS, SVG, JSX, and related identifiers to preserve acronym casing
- Keep upstream and serialized boundary names unchanged
- Add a lint guardrail and migration notes for exported APIs
2026-08-10 15:48:16 +03:00

57 lines
1.6 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import {
buildMultiplayerURL,
getKiwiMessageType,
hasFigWireHeader,
isKiwiMessage,
isZstdCompressed,
parseVarint,
skipFigWireHeader,
ZSTD_MAGIC
} from '../src/fig'
describe('Figma Kiwi protocol helpers', () => {
test('detects Zstd magic bytes', () => {
expect(isZstdCompressed(ZSTD_MAGIC)).toBe(true)
expect(isZstdCompressed(new Uint8Array([1, 2, 3, 4]))).toBe(false)
})
test('reads Kiwi message type from minimal payloads', () => {
const payload = new Uint8Array([1, 4, 0])
expect(isKiwiMessage(payload)).toBe(true)
expect(getKiwiMessageType(payload)).toBe(4)
expect(getKiwiMessageType(new Uint8Array([2, 4, 0]))).toBeNull()
})
test('parses representative varints', () => {
expect(parseVarint(new Uint8Array([0]), 0)).toEqual([0, 1])
expect(parseVarint(new Uint8Array([127]), 0)).toEqual([127, 1])
expect(parseVarint(new Uint8Array([128, 1]), 0)).toEqual([128, 2])
})
test('handles fig-wire headers', () => {
const payload = new Uint8Array([
...new TextEncoder().encode('fig-wire'),
1,
0,
0,
0,
40,
181,
47,
253
])
expect(hasFigWireHeader(payload)).toBe(true)
expect(skipFigWireHeader(payload)).toEqual(ZSTD_MAGIC)
})
test('builds multiplayer URLs', () => {
const url = new URL(buildMultiplayerURL('file-key', 'tracking-id'))
expect(url.protocol).toBe('wss:')
expect(url.pathname).toBe('/api/multiplayer/file-key')
expect(url.searchParams.get('role')).toBe('editor')
expect(url.searchParams.get('tracking_session_id')).toBe('tracking-id')
})
})