import { describe, expect, it } from 'vitest' import { away } from './link' /** * Whether an address leaves this interface. * * Worth a test of its own because both wrong answers are silent. A foreign host * read as a path is handed to the router, which looks for a screen at * `https://example.com` and finds none; a path read as foreign opens a second * tab onto the app somebody was already in. */ describe('whether an address leaves this interface', () => { it('keeps a path, which is a screen here', () => { expect(away('/vote')).toBe(false) expect(away('/vote/0x1?tab=cast')).toBe(false) expect(away('vote')).toBe(false) expect(away('')).toBe(false) }) it('lets go of anything with a scheme in front of it', () => { expect(away('https://lux.network')).toBe(true) expect(away('http://lux.network')).toBe(true) expect(away('mailto:hello@lux.network')).toBe(true) expect(away('ipfs://bafy')).toBe(true) }) it('reads a fragment and a query as this document, not another', () => { expect(away('#terms')).toBe(false) expect(away('?tab=cast')).toBe(false) }) it('lets go of an address that reads as a path and resolves somewhere else', () => { // `//host` and its backslash spellings resolve to a foreign origin from any // page. Read as paths they are handed to the router, and where no router is // stated the anchor leaves the app in the same tab with a referrer — which // is what a link drawn out of a record somebody else wrote would do. expect(away('//lux.network')).toBe(true) expect(away('/\\lux.network')).toBe(true) expect(away('\\\\lux.network')).toBe(true) expect(away('\\/lux.network')).toBe(true) // One of either is still a path. expect(away('/lux')).toBe(false) expect(away('\\lux')).toBe(false) }) it('does not mistake a colon inside a path for a scheme', () => { // A tenant is addressed `chain:address` on some screens, and the colon is // in the path rather than in front of it. expect(away('/home?dao=lux:0xF66B')).toBe(false) expect(away('/6:30')).toBe(false) }) })