mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* refactor(mobile): give relay and encryption concrete owners * refactor(mobile): organize session and review feature owners * refactor(mobile): organize RPC and host ownership * test(mobile): point render fixtures at concrete owners * refactor(mobile): use explicit type-only owner imports
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { decodePairingUrl, extractPairingCodeFromUrl, parsePairingCode } from './pairing'
|
|
import type { PairingOffer } from '../../../src/shared/remote-runtime/pairing/mobile-relay-pairing-offer'
|
|
|
|
const offer: PairingOffer = {
|
|
v: 2,
|
|
endpoint: 'ws://100.102.47.57:6768',
|
|
deviceToken: 'token-abc',
|
|
publicKeyB64: 'pubkey-xyz'
|
|
}
|
|
|
|
function encodeOffer(input: PairingOffer = offer): string {
|
|
return btoa(JSON.stringify(input)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
describe('pairing deep links', () => {
|
|
it('rejects lookalike routes', () => {
|
|
expect(extractPairingCodeFromUrl('orca://pairing?code=abc123')).toBeNull()
|
|
expect(extractPairingCodeFromUrl('orca://pair-extra?code=abc123')).toBeNull()
|
|
})
|
|
|
|
it('decodes desktop QR payloads when atob requires base64 padding', () => {
|
|
const realAtob = globalThis.atob
|
|
vi.stubGlobal('atob', (input: string) => {
|
|
if (input.length % 4 !== 0) {
|
|
throw new Error('Invalid base64 length')
|
|
}
|
|
return realAtob(input)
|
|
})
|
|
|
|
expect(decodePairingUrl(`orca://pair?code=${encodeOffer()}`)).toEqual(offer)
|
|
})
|
|
|
|
it('preserves legacy padding and aliases with the same decoded offer', () => {
|
|
const paddedOffer = { ...offer, endpoint: `${offer.endpoint}/a` }
|
|
const code = encodeOffer(paddedOffer)
|
|
expect(code.length % 4).toBe(2)
|
|
const alias = `${code.slice(0, -1)}${base64Alias(code.at(-1)!)}`
|
|
|
|
expect(parsePairingCode(`${code}=`)).toEqual(paddedOffer)
|
|
expect(parsePairingCode(`${code}==`)).toEqual(paddedOffer)
|
|
expect(parsePairingCode(alias)).toEqual(paddedOffer)
|
|
})
|
|
})
|
|
|
|
function base64Alias(character: string): string {
|
|
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
|
|
return alphabet[alphabet.indexOf(character) + 1]!
|
|
}
|