import { describe, expect, it } from 'vitest' import { OPENCODE_SERVER_LINK_VERSION, buildManagerAppLink, parseManagerAppLink, parseOpenCodeConnectLink, type ManagerAppLink, } from '@opencode-manager/shared/schemas' const CODE = 'a'.repeat(43) const MANAGER_LINK: ManagerAppLink = { kind: 'manager', name: 'Home', baseUrl: 'https://m.example.com', code: CODE, headers: [ { name: 'CF-Access-Client-Id', value: 'id.access' }, { name: 'X-Secret', value: 'a:b=c&d' }, ], } function managerLink(extra: Record): string { const params = new URLSearchParams({ v: OPENCODE_SERVER_LINK_VERSION, kind: 'manager', name: 'Home', url: 'https://m.example.com', code: CODE, }) for (const [key, value] of Object.entries(extra)) { params.delete(key) for (const item of Array.isArray(value) ? value : [value]) params.append(key, item) } return `ocm://link?${params.toString()}` } describe('buildManagerAppLink', () => { it('builds an ocm link carrying the version and the manager kind', () => { const link = buildManagerAppLink(MANAGER_LINK) const params = new URL(link).searchParams expect(link.startsWith('ocm://link?')).toBe(true) expect(params.get('v')).toBe(OPENCODE_SERVER_LINK_VERSION) expect(params.get('kind')).toBe('manager') }) it('keeps the exact wire format of a manager link', () => { expect(buildManagerAppLink(MANAGER_LINK)).toBe( `ocm://link?v=2&kind=manager&name=Home&url=https%3A%2F%2Fm.example.com&code=${CODE}` + '&h=CF-Access-Client-Id%3Aid.access&h=X-Secret%3Aa%3Ab%3Dc%26d', ) }) it('appends one h parameter per header in order', () => { const params = new URL(buildManagerAppLink(MANAGER_LINK)).searchParams expect(params.getAll('h')).toEqual(['CF-Access-Client-Id:id.access', 'X-Secret:a:b=c&d']) }) }) describe('parseManagerAppLink', () => { it('round-trips a manager link with headers in order', () => { expect(parseManagerAppLink(buildManagerAppLink(MANAGER_LINK))).toEqual(MANAGER_LINK) }) it('round-trips a manager link without headers', () => { const link = { ...MANAGER_LINK, headers: [] } expect(parseManagerAppLink(buildManagerAppLink(link))).toEqual(link) }) it.each([ ['a wrong version', managerLink({ v: '3' })], ['a header without a colon', managerLink({ h: 'X-Secret' })], ['a blocked header name', managerLink({ h: 'Cookie:session' })], ['a forwarding header name', managerLink({ h: 'X-Forwarded-For:1.2.3.4' })], ['more than eight headers', managerLink({ h: Array.from({ length: 9 }, (_, index) => `X-Header-${index}:value`) })], ['garbage text', 'not a link'], ])('returns null for %s', (_label, text) => { expect(parseManagerAppLink(text)).toBeNull() }) }) describe('parseOpenCodeConnectLink', () => { it('reads a native link exposed through a tunnel', () => { expect(parseOpenCodeConnectLink('https://abc.def.opentunnel.xyz/auth/connect/AbC_12-x')).toEqual({ name: 'abc.def.opentunnel.xyz', baseUrl: 'https://abc.def.opentunnel.xyz', pairing: { connectCode: 'AbC_12-x' }, }) }) it('reads a native LAN link and keeps its port', () => { expect(parseOpenCodeConnectLink('http://192.168.1.2:49374/auth/connect/abc')).toEqual({ name: '192.168.1.2', baseUrl: 'http://192.168.1.2:49374', pairing: { connectCode: 'abc' }, }) }) it('trims surrounding whitespace', () => { expect(parseOpenCodeConnectLink(' https://abc.def.opentunnel.xyz/auth/connect/abc\n')).toMatchObject({ baseUrl: 'https://abc.def.opentunnel.xyz', pairing: { connectCode: 'abc' }, }) }) it('reads the QR JSON payload and uses the first URL with its name', () => { const payload = JSON.stringify({ code: 'AbC_12-x', urls: ['https://abc.def.opentunnel.xyz', 'http://192.168.1.2:49374'], name: 'Kitchen', }) expect(parseOpenCodeConnectLink(payload)).toEqual({ name: 'Kitchen', baseUrl: 'https://abc.def.opentunnel.xyz', pairing: { connectCode: 'AbC_12-x' }, }) }) it('falls back to the first URL hostname when the QR payload has no name', () => { const payload = JSON.stringify({ code: 'abc', urls: ['http://192.168.1.2:49374'] }) expect(parseOpenCodeConnectLink(payload)).toEqual({ name: '192.168.1.2', baseUrl: 'http://192.168.1.2:49374', pairing: { connectCode: 'abc' }, }) }) it('caps a long QR payload name to the create request maximum', () => { const payload = JSON.stringify({ code: 'abc', urls: ['https://abc.def.opentunnel.xyz'], name: 'n'.repeat(150), }) expect(parseOpenCodeConnectLink(payload)?.name).toBe('n'.repeat(100)) }) it.each([ ['no urls', JSON.stringify({ code: 'abc', urls: [] })], ['a non-string url', JSON.stringify({ code: 'abc', urls: [42] })], ['a url with a path', JSON.stringify({ code: 'abc', urls: ['https://host.example.com/path'] })], ['a url with a query', JSON.stringify({ code: 'abc', urls: ['https://host.example.com?x=1'] })], ['a url with credentials', JSON.stringify({ code: 'abc', urls: ['https://user:pass@host.example.com'] })], ['a non-http url', JSON.stringify({ code: 'abc', urls: ['ftp://host.example.com'] })], ['an invalid code', JSON.stringify({ code: 'a.b', urls: ['https://host.example.com'] })], ['malformed json', '{"code":'], ])('returns null for a QR payload with %s', (_label, text) => { expect(parseOpenCodeConnectLink(text)).toBeNull() }) it.each([ ['an extra path segment', 'https://abc.def.opentunnel.xyz/auth/connect/abc/extra'], ['an empty code', 'https://abc.def.opentunnel.xyz/auth/connect/'], ['a code with a dot', 'https://abc.def.opentunnel.xyz/auth/connect/a.b'], ['a query string', 'https://abc.def.opentunnel.xyz/auth/connect/abc?x=1'], ['a hash', 'https://abc.def.opentunnel.xyz/auth/connect/abc#frag'], ['user credentials', 'https://user:pass@abc.def.opentunnel.xyz/auth/connect/abc'], ['a non-http scheme', 'ftp://abc.def.opentunnel.xyz/auth/connect/abc'], ['an ocm link', `ocm://link?v=2&kind=manager&name=Home&url=https%3A%2F%2Fm.example.com&code=${CODE}`], ['garbage text', 'not a link'], ])('returns null for %s', (_label, text) => { expect(parseOpenCodeConnectLink(text)).toBeNull() }) })