Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

3.9 KB
Raw Blame
/**
 * The iOS app's web-side bridge (ios/src/native-bridge.js).
 *
 * This module ships with the SITE, not the .ipa: Capacitor injects its native
 * bridge into the remote page, so `window.Capacitor` exists on three.ws only
 * when the site is being viewed inside the iOS app. Two properties matter more
 * than the rest and are pinned here.
 *
 * First, it must be inert in a browser. It is injected into 671 built pages, so
 * anything it does unconditionally, it does to every visitor on every surface.
 *
 * Second, isInternalUrl decides which links stay in the app's only WebView. Get
 * it wrong in the permissive direction and a tap on an off-site link navigates
 * the app away from three.ws with no back button to return; get it wrong in the
 * strict direction and ordinary in-app navigation bounces out to Safari.
 */

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';

describe('ios native bridge', () => {
	let mod;

	beforeEach(async () => {
		vi.resetModules();
		vi.stubGlobal('document', {
			readyState: 'complete',
			addEventListener: vi.fn(),
			documentElement: { classList: { add: vi.fn() }, style: { setProperty: vi.fn() } },
		});
		vi.stubGlobal('location', { href: 'https://three.ws/create' });
		mod = await import('../ios/src/native-bridge.js');
	});

	afterEach(() => {
		vi.unstubAllGlobals();
	});

	it('reports a plain browser as not native', () => {
		expect(mod.isNativeIOS()).toBe(false);
	});

	it('installs nothing in a browser', () => {
		expect(mod.bootNativeIOS()).toBe(false);
		expect(document.documentElement.classList.add).not.toHaveBeenCalled();
	});

	it('leaves navigator untouched in a browser', async () => {
		// haptic() must be safe to call anywhere: surfaces invoke it directly.
		await expect(mod.haptic('heavy')).resolves.toBeUndefined();
		expect(globalThis.threeWsHaptic).toBeUndefined();
	});

	describe('isInternalUrl', () => {
		for (const href of [
			'/create',
			'#gallery',
			'?tab=agents',
			'https://three.ws/viewer?src=x',
			'https://www.three.ws/agents/1',
			'mailto:[email protected]',
			'threews://create',
		]) {
			it(`keeps ${href} in the app`, () => {
				expect(mod.isInternalUrl(href)).toBe(true);
			});
		}

		for (const href of [
			'https://solscan.io/tx/abc',
			'https://x.com/threews',
			'http://example.com',
			// A lookalike host is the case worth being explicit about: substring
			// matching on "three.ws" would hand the WebView to an attacker.
			'https://three.ws.evil.com/phish',
			'https://notthree.ws/',
		]) {
			it(`sends ${href} out to Safari`, () => {
				expect(mod.isInternalUrl(href)).toBe(false);
			});
		}
	});

	describe('pushTapTarget', () => {
		const ID = '2b1f4c1e-7d0a-4a55-9b8e-1f2d3c4b5a69';

		it('opens a same-origin path in the app, marked as a push return', () => {
			expect(mod.pushTapTarget({ url: '/agents/7', notificationId: ID })).toEqual({
				internal: `https://three.ws/agents/7?source=push&n=${ID}`,
			});
		});

		it('keeps the query the notification already carried', () => {
			const { internal } = mod.pushTapTarget({ url: '/dashboard/?tab=sales', notificationId: ID });
			const url = new URL(internal);
			expect(url.searchParams.get('tab')).toBe('sales');
			expect(url.searchParams.get('source')).toBe('push');
		});

		it('pins a www link to the app origin', () => {
			expect(mod.pushTapTarget({ url: 'https://www.three.ws/create' }).internal).toBe(
				'https://three.ws/create?source=push',
			);
		});

		it('sends an off-site link out of the app', () => {
			expect(mod.pushTapTarget({ url: 'https://solscan.io/tx/abc' })).toEqual({
				external: 'https://solscan.io/tx/abc',
			});
		});

		it('falls back to the inbox when the payload has no link', () => {
			expect(new URL(mod.pushTapTarget({}).internal).pathname).toBe('/notifications');
		});

		it('refuses a script URL outright', () => {
			expect(mod.pushTapTarget({ url: 'javascript:alert(1)' })).toBeNull();
		});
	});
});