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.

test(companion): pin what the checkout extractor pulls off a page

nirholasd71c83799d7e
diff --git a/src/portal/render.js b/src/portal/render.js
index 7ae631736..477fc113b 100644
--- a/src/portal/render.js
+++ b/src/portal/render.js
@@ -77,6 +77,8 @@ export function createPortalWorld({ canvas, world, onDoor, onReady }) {
const scene = new THREE.Scene();
scene.background = new THREE.Color(world.ground.sky);
+ /** Every world-space label, faded by distance each frame so a dense city stays readable. */
+ const labels = [];
scene.fog = new THREE.Fog(world.ground.fog, 34, world.ground.radius * 1.5);
const camera = new THREE.PerspectiveCamera(58, 1, 0.1, world.ground.radius * 4);
@@ -86,6 +88,35 @@ export function createPortalWorld({ canvas, world, onDoor, onReady }) {
return obj;
};
+ // ── sky ───────────────────────────────────────────────────────────────────
+ // A two-stop vertical gradient on the inside of a large sphere. Cheaper than
+ // an environment map, and it gives the horizon a real edge for the city to
+ // stand against instead of a flat clear colour.
+ const skyGeo = track(new THREE.SphereGeometry(world.ground.radius * 3, 32, 16));
+ const skyMat = track(new THREE.ShaderMaterial({
+ side: THREE.BackSide,
+ depthWrite: false,
+ fog: false,
+ uniforms: {
+ top: { value: new THREE.Color(world.ground.sky) },
+ bottom: { value: new THREE.Color(world.ground.fog) },
+ horizon: { value: new THREE.Color(world.palette.accent) },
+ },
+ vertexShader: `varying vec3 vWorld;
+ void main() {
+ vWorld = (modelMatrix * vec4(position, 1.0)).xyz;
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
+ }`,
+ fragmentShader: `uniform vec3 top; uniform vec3 bottom; uniform vec3 horizon; varying vec3 vWorld;
+ void main() {
+ float h = clamp(normalize(vWorld).y * 0.5 + 0.5, 0.0, 1.0);
+ vec3 base = mix(bottom, top, smoothstep(0.35, 0.85, h));
+ float glow = pow(1.0 - abs(h - 0.5) * 2.0, 6.0);
+ gl_FragColor = vec4(base + horizon * glow * 0.18, 1.0);
+ }`,
+ }));
+ scene.add(new THREE.Mesh(skyGeo, skyMat));
+
// ── lighting ──────────────────────────────────────────────────────────────
const hemi = new THREE.HemisphereLight(world.palette.accent, world.ground.color, 1.15);
scene.add(hemi);
@@ -140,11 +171,29 @@ export function createPortalWorld({ canvas, world, onDoor, onReady }) {
monument.castShadow = true;
scene.add(monument);
const titleLabel = makeLabel(world.plaza.monument.label, { size: 52 });
- titleLabel.position.set(0, world.plaza.monument.h + 1.4, 0);
- titleLabel.scale.multiplyScalar(2.1);
+ titleLabel.position.set(0, world.plaza.monument.h + 1.5, 0);
+ titleLabel.scale.multiplyScalar(1.15);
scene.add(titleLabel);
+ labels.push({ sprite: titleLabel, far: 120 });
disposables.push({ dispose: () => titleLabel.userData.dispose() });
+ // A ring at the monument's foot, so the plaza reads as the centre of a place
+ // rather than as a disc that happens to have a block on it.
+ const ringGeo = track(new THREE.RingGeometry(world.plaza.radius * 0.94, world.plaza.radius, 96));
+ const ring = new THREE.Mesh(ringGeo, track(new THREE.MeshBasicMaterial({ color: world.palette.accent, transparent: true, opacity: 0.5, side: THREE.DoubleSide })));
+ ring.rotation.x = -Math.PI / 2;
+ ring.position.y = 0.03;
+ scene.add(ring);
+
+ // A faint grid over the ground: it gives the eye scale while walking and
+ // disappears into the fog, so it never reads as a debug overlay.
+ const grid = new THREE.GridHelper(world.ground.radius * 2, Math.round(world.ground.radius / 2), world.palette.accent, world.palette.accent);
+ grid.material.transparent = true;
+ grid.material.opacity = 0.06;
+ grid.position.y = 0.01;
+ scene.add(grid);
+ disposables.push(grid.material, grid.geometry);
+
// ── buildings ─────────────────────────────────────────────────────────────
const edgeMat = track(new THREE.LineBasicMaterial({ color: world.palette.accent, transparent: true, opacity: 0.55 }));
for (const b of world.buildings) {
@@ -174,8 +223,9 @@ export function createPortalWorld({ canvas, world, onDoor, onReady }) {
const label = makeLabel(b.label, { size: 40 });
label.position.set(b.x, b.h + 1.1, b.z);
- label.scale.multiplyScalar(1.35);
+ label.scale.multiplyScalar(0.78);
scene.add(label);
+ labels.push({ sprite: label, far: 70 });
disposables.push({ dispose: () => label.userData.dispose() });
}
@@ -200,8 +250,10 @@ export function createPortalWorld({ canvas, world, onDoor, onReady }) {
doorMeshes.push({ mesh, door: d, material: mat });
const label = makeLabel(d.label || new URL(d.href).host, { size: 34, color: d.internal ? '#eaf6ff' : '#ffeede' });
- label.position.set(d.x, d.h + 0.75, d.z);
+ label.position.set(d.x, d.h + 0.7, d.z);
+ label.scale.multiplyScalar(0.62);
scene.add(label);
+ labels.push({ sprite: label, far: 26 });
disposables.push({ dispose: () => label.userData.dispose() });
}
@@ -234,7 +286,9 @@ export function createPortalWorld({ canvas, world, onDoor, onReady }) {
if (p.label) {
const alt = makeLabel(p.label, { size: 30 });
alt.position.set(p.x, p.h + 1.6, p.z);
+ alt.scale.multiplyScalar(0.62);
scene.add(alt);
+ labels.push({ sprite: alt, far: 30 });
disposables.push({ dispose: () => alt.userData.dispose() });
}
},
@@ -490,7 +544,15 @@ export function createPortalWorld({ canvas, world, onDoor, onReady }) {
onDoor?.(best, 'near');
}
- titleLabel.position.y = world.plaza.monument.h + 1.4 + Math.sin(elapsed * 1.4) * 0.08;
+ // Labels fade with distance so a 24-district city reads as a skyline from
+ // the plaza and as signage up close, instead of as a wall of text.
+ for (const { sprite, far } of labels) {
+ const dist = Math.hypot(sprite.position.x - player.position.x, sprite.position.z - player.position.z);
+ const opacity = clamp(1 - (dist - far * 0.45) / (far * 0.55), 0, 1);
+ sprite.material.opacity = opacity;
+ sprite.visible = opacity > 0.02;
+ }
+ titleLabel.position.y = world.plaza.monument.h + 1.5 + Math.sin(elapsed * 1.4) * 0.08;
renderer.render(scene, camera);
}
frame();
diff --git a/tests/checkout-extract.test.js b/tests/checkout-extract.test.js
new file mode 100644
index 000000000..4e7e330e4
--- /dev/null
+++ b/tests/checkout-extract.test.js
@@ -0,0 +1,192 @@
+// The client half of the checkout companion: what it reads off a live page.
+//
+// The load-bearing test in this file is "never reads an input". Everything else
+// here is money parsing, which matters because a mis-parsed total produces a
+// confident, specific, wrong warning on somebody's payment screen. The
+// separator cases are the ones that break: "1.234,56" and "1,234.56" are the
+// same amount written by two continents, and "$1,299" has no cents at all.
+
+import { describe, it, expect, beforeEach } from 'vitest';
+import { JSDOM } from 'jsdom';
+import {
+ parseAmount,
+ roleFor,
+ looksLikeCheckout,
+ collectText,
+ collectAmounts,
+ primaryTotal,
+ buildExtract,
+} from '../extensions/checkout-companion/extract.js';
+
+describe('parseAmount', () => {
+ it('reads the common US form', () => {
+ expect(parseAmount('$49.99')).toEqual({ value: 4999, currency: 'USD' });
+ expect(parseAmount('Total: $1,234.56')).toEqual({ value: 123456, currency: 'USD' });
+ });
+
+ it('reads the European form where the comma is the decimal point', () => {
+ expect(parseAmount('1.234,56 EUR')).toEqual({ value: 123456, currency: 'EUR' });
+ expect(parseAmount('€9,99')).toEqual({ value: 999, currency: 'EUR' });
+ });
+
+ it('treats a thousands separator with no cents as whole units', () => {
+ // "$1,299" is twelve hundred and ninety-nine dollars, not $12.99.
+ expect(parseAmount('$1,299')).toEqual({ value: 129900, currency: 'USD' });
+ });
+
+ it('does not invent cents for a zero-decimal currency', () => {
+ expect(parseAmount('¥1250')).toEqual({ value: 1250, currency: 'JPY' });
+ expect(parseAmount('JPY 1,250')).toEqual({ value: 1250, currency: 'JPY' });
+ });
+
+ it('refuses a number with no currency marker', () => {
+ expect(parseAmount('12 items')).toBeNull();
+ expect(parseAmount('2026-08-28')).toBeNull();
+ expect(parseAmount('Order 1234567')).toBeNull();
+ expect(parseAmount('')).toBeNull();
+ });
+
+ it('reads a negative or bracketed amount as a credit', () => {
+ expect(parseAmount('-$10.00').value).toBe(-1000);
+ expect(parseAmount('($10.00)').value).toBe(-1000);
+ });
+
+ it('honours a currency hint when the page omits the symbol', () => {
+ expect(parseAmount('49.99', { currencyHint: 'GBP' })).toEqual({ value: 4999, currency: 'GBP' });
+ });
+});
+
+describe('roleFor', () => {
+ it('classifies the labels a checkout actually uses', () => {
+ expect(roleFor('Order total')).toBe('total');
+ expect(roleFor('Amount due today')).toBe('total');
+ expect(roleFor('Subtotal')).toBe('subtotal');
+ expect(roleFor('Shipping & handling')).toBe('shipping');
+ expect(roleFor('Sales tax')).toBe('tax');
+ expect(roleFor('Service fee')).toBe('service');
+ expect(roleFor('Booking fee')).toBe('fee');
+ expect(roleFor('Promo discount')).toBe('discount');
+ expect(roleFor('Something else entirely')).toBe('unknown');
+ });
+});
+
+describe('looksLikeCheckout', () => {
+ it('recognises a checkout by url and page language together', () => {
+ expect(looksLikeCheckout({ url: 'https://shop.example/checkout', text: 'Place your order' })).toBe(true);
+ expect(looksLikeCheckout({ url: 'https://x.example/a', text: 'Order summary. Payment method. Pay now' })).toBe(true);
+ });
+
+ it('leaves an ordinary page alone', () => {
+ expect(looksLikeCheckout({ url: 'https://news.example/article', text: 'A long article about shoes.' })).toBe(false);
+ // A url alone is not enough: /order shows up on order-history pages.
+ expect(looksLikeCheckout({ url: 'https://shop.example/orders', text: 'Your past orders' })).toBe(false);
+ });
+});
+
+describe('reading the DOM', () => {
+ let dom;
+ const load = (html) => {
+ dom = new JSDOM(`<!doctype html><body>${html}</body>`);
+ return dom.window;
+ };
+
+ beforeEach(() => {
+ dom = null;
+ });
+
+ it('never reads a value out of an input, a textarea or a select', () => {
+ const win = load(`
+ <p>Card on file</p>
+ <input type="text" value="4242424242424242" placeholder="Card number">
+ <textarea>cvv 123</textarea>
+ <select><option>secret option</option></select>
+ `);
+ const text = collectText(win.document.body, { view: win });
+ expect(text).toContain('Card on file');
+ expect(text).not.toContain('4242424242424242');
+ expect(text).not.toContain('cvv 123');
+ expect(text).not.toContain('secret option');
+ });
+
+ it('never reads a contenteditable surface', () => {
+ const win = load('<div contenteditable="true">4242 4242 4242 4242</div><p>Total $10.00</p>');
+ const text = collectText(win.document.body, { view: win });
+ expect(text).not.toContain('4242');
+ expect(text).toContain('Total $10.00');
+ });
+
+ it('skips iframes and scripts entirely', () => {
+ const win = load('<script>var card="4242";</script><iframe></iframe><p>Order summary</p>');
+ const text = collectText(win.document.body, { view: win });
+ expect(text).not.toContain('4242');
+ expect(text).toBe('Order summary');
+ });
+
+ it('skips content hidden from the person reading the page', () => {
+ const win = load('<div style="display:none">Hidden $99.00</div><p>Shown $10.00</p>');
+ const text = collectText(win.document.body, { view: win });
+ expect(text).not.toContain('Hidden');
+ expect(text).toContain('Shown $10.00');
+ });
+
+ it('stops at maxChars so a huge page cannot stall the tab', () => {
+ const win = load(`<p>${'word '.repeat(5000)}</p>`);
+ const text = collectText(win.document.body, { view: win, maxChars: 500 });
+ expect(text.length).toBeLessThanOrEqual(500);
+ });
+
+ it('pairs each amount with the label in its own table row', () => {
+ const win = load(`
+ <table>
+ <tr><td>Subtotal</td><td>$49.99</td></tr>
+ <tr><td>Shipping</td><td>$5.00</td></tr>
+ <tr><td>Service fee</td><td>$7.50</td></tr>
+ <tr><td>Order total</td><td>$62.49</td></tr>
+ </table>
+ `);
+ const amounts = collectAmounts(win.document.body, { view: win });
+ const byRole = Object.fromEntries(amounts.map((a) => [a.role, a.value]));
+ expect(byRole.subtotal).toBe(4999);
+ expect(byRole.shipping).toBe(500);
+ expect(byRole.service).toBe(750);
+ expect(byRole.total).toBe(6249);
+ });
+
+ it('does not collect an amount that only exists inside an input', () => {
+ const win = load('<input type="text" value="$99.99"><p>Total $10.00</p>');
+ const amounts = collectAmounts(win.document.body, { view: win });
+ expect(amounts.map((a) => a.value)).toEqual([1000]);
+ });
+
+ it('picks the largest total as the primary one', () => {
+ expect(
+ primaryTotal([
+ { value: 1000, role: 'total' },
+ { value: 6249, role: 'total' },
+ { value: 9999, role: 'line' },
+ ]).value,
+ ).toBe(6249);
+ expect(primaryTotal([{ value: 10, role: 'line' }])).toBeNull();
+ });
+});
+
+describe('buildExtract', () => {
+ it('drops the url fragment and caps the payload', () => {
+ const out = buildExtract({
+ url: 'https://shop.example/checkout#step2',
+ title: 'x'.repeat(500),
+ text: 'y'.repeat(70_000),
+ amounts: [{ value: 100, currency: 'GBP', role: 'total' }],
+ quoted: { value: 90 },
+ });
+ expect(out.url).toBe('https://shop.example/checkout');
+ expect(out.title).toHaveLength(300);
+ expect(out.text).toHaveLength(60_000);
+ expect(out.currency).toBe('GBP');
+ expect(out.quoted).toEqual({ value: 90, currency: 'GBP' });
+ });
+
+ it('carries no quoted price when there is nothing remembered', () => {
+ expect(buildExtract({ url: 'https://a.example/', text: 'x', amounts: [] }).quoted).toBeNull();
+ });
+});