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.

7.6 KB
Raw Blame
// Glance card: the model, the SVG renderer, and the Adaptive Card binding.
//
// All three are pure functions over already-loaded rows, so this suite runs the
// real code with no database and no network.

import { describe, it, expect } from 'vitest';
import { buildGlanceCard, monogramOf, accentOf, relativeTime } from '../api/_lib/glance-card.js';
import { renderGlanceSvg, escapeXml, truncate, formatCount } from '../api/_lib/glance-svg.js';
import { adaptiveTemplate, adaptiveCardFor } from '../api/_lib/glance-adaptive.js';

const NOW = new Date('2026-08-28T12:00:00.000Z');

function agent(patch = {}) {
	return {
		id: '11111111-2222-3333-4444-555555555555',
		name: 'Atlas Scout',
		description: 'Watches the launch feed.',
		skills: ['watch', 'report'],
		created_at: new Date('2026-08-01T00:00:00.000Z'),
		avatar_thumbnail_key: null,
		avatar_visibility: null,
		...patch,
	};
}

describe('buildGlanceCard', () => {
	it('reads the live number off the last 24 hours of real actions', () => {
		const card = buildGlanceCard({
			agent: agent(),
			activity: { total: 412, day: 17, week: 96 },
			last: { type: 'skill.invoke', created_at: new Date(NOW - 3_600_000) },
			now: NOW,
		});
		expect(card.metric).toEqual({ label: 'Moves today', value: 17 });
		expect(card.stats[0]).toEqual({ label: 'This week', value: 96 });
		expect(card.stats[1]).toEqual({ label: 'All time', value: 412 });
		expect(card.status).toBe('active');
		expect(card.lastAction.relative).toBe('1h ago');
		expect(card.url).toBe('https://three.ws/agents/11111111-2222-3333-4444-555555555555');
	});

	it('calls an agent idle once its last action is over a day old', () => {
		const card = buildGlanceCard({
			agent: agent(),
			activity: { total: 8, day: 0, week: 2 },
			last: { type: 'speak', created_at: new Date(NOW - 3 * 86_400_000) },
			now: NOW,
		});
		expect(card.status).toBe('idle');
		expect(card.headline).toMatch(/quiet today/i);
	});

	it('designs the never-acted state instead of showing a bare zero', () => {
		const card = buildGlanceCard({
			agent: agent(),
			activity: { total: 0, day: 0, week: 0 },
			last: null,
			now: NOW,
		});
		expect(card.status).toBe('new');
		expect(card.headline).toMatch(/not started yet/i);
		expect(card.lastAction).toBeNull();
	});

	it('shows skills when the agent has some, and days live when it does not', () => {
		const withSkills = buildGlanceCard({ agent: agent(), activity: {}, now: NOW });
		expect(withSkills.stats[2]).toEqual({ label: 'Skills', value: 2 });

		const without = buildGlanceCard({ agent: agent({ skills: [] }), activity: {}, now: NOW });
		expect(without.stats[2]).toEqual({ label: 'Days live', value: 27 });
	});

	it('never attaches the thumbnail of an avatar the public may not see', () => {
		const private_ = buildGlanceCard({
			agent: agent({ avatar_thumbnail_key: 'u/1/thumb.png', avatar_visibility: 'private' }),
			activity: {},
			now: NOW,
		});
		expect(private_.image).toBeNull();
		expect(private_.monogram).toBe('AS');
	});

	it('gives every agent a stable accent of its own', () => {
		const a = accentOf('11111111-2222-3333-4444-555555555555');
		const b = accentOf('99999999-8888-7777-6666-555555555555');
		expect(a).toEqual(accentOf('11111111-2222-3333-4444-555555555555'));
		expect(a.hue).not.toBe(b.hue);
	});

	it('handles a nameless agent without producing an empty monogram', () => {
		expect(monogramOf('')).toBe('3');
		expect(monogramOf('solo')).toBe('S');
		expect(monogramOf('Atlas Scout Prime')).toBe('AS');
	});

	it('formats elapsed time the way a widget slot needs it', () => {
		expect(relativeTime(new Date(NOW - 5_000), NOW)).toBe('just now');
		expect(relativeTime(new Date(NOW - 5 * 60_000), NOW)).toBe('5m ago');
		expect(relativeTime(new Date(NOW - 40 * 86_400_000), NOW)).toBe('1mo ago');
	});
});

describe('renderGlanceSvg', () => {
	const card = buildGlanceCard({
		agent: agent(),
		activity: { total: 412, day: 17, week: 96 },
		last: { type: 'skill.invoke', created_at: new Date(NOW - 3_600_000) },
		now: NOW,
	});

	it('renders every declared size at its declared dimensions', () => {
		expect(renderGlanceSvg(card, { size: 'small' })).toContain('width="240" height="240"');
		expect(renderGlanceSvg(card, { size: 'medium' })).toContain('width="480" height="200"');
		expect(renderGlanceSvg(card, { size: 'large' })).toContain('width="480" height="300"');
	});

	it('falls back to the default size rather than rendering nothing', () => {
		expect(renderGlanceSvg(card, { size: 'enormous' })).toContain('width="480" height="200"');
	});

	it('ships both palettes on auto and only one when a theme is pinned', () => {
		expect(renderGlanceSvg(card, { theme: 'auto' })).toContain('prefers-color-scheme:dark');
		expect(renderGlanceSvg(card, { theme: 'dark' })).not.toContain('prefers-color-scheme');
	});

	it('scopes its ids and CSS so two cards on one page cannot capture each other', () => {
		const a = renderGlanceSvg(card, { size: 'medium', theme: 'dark' });
		const b = renderGlanceSvg(card, { size: 'medium', theme: 'light' });
		const nsOf = (svg) => svg.match(/class="(g[a-z0-9]+)"/)[1];
		expect(nsOf(a)).not.toBe(nsOf(b));
		expect(a).toContain(`.${nsOf(a)} .t{`);
	});

	it('escapes agent-supplied text instead of letting it break the document', () => {
		const hostile = buildGlanceCard({
			agent: agent({ name: '<script>alert(1)</script>', description: 'a & b "c"' }),
			activity: {},
			now: NOW,
		});
		const svg = renderGlanceSvg(hostile);
		expect(svg).not.toContain('<script>');
		expect(svg).toContain('&lt;script&gt;');
		expect(svg).toContain('&amp;');
	});

	it('strips control characters, which is the byte class that stalls parsers', () => {
		expect(escapeXml('safe\u0000 te\u001fxt')).toBe('safe text');
	});

	it('truncates on characters, not bytes, so an emoji name never splits', () => {
		expect(truncate('abcdef', 4)).toBe('abc…');
		expect(truncate('abc', 4)).toBe('abc');
	});

	it('abbreviates counts that would otherwise overflow the slot', () => {
		expect(formatCount(999)).toBe('999');
		expect(formatCount(4123)).toBe('4.1k');
		expect(formatCount(41_230)).toBe('41k');
		expect(formatCount(4_123_000)).toBe('4.1m');
	});

	it('carries an accessible label for a screen reader', () => {
		expect(renderGlanceSvg(card)).toContain('role="img"');
		expect(renderGlanceSvg(card)).toMatch(/aria-label="Atlas Scout: 17 moves today on three\.ws"/);
	});
});

describe('adaptive card', () => {
	const card = buildGlanceCard({
		agent: agent(),
		activity: { total: 412, day: 17, week: 96 },
		last: { type: 'skill.invoke', created_at: new Date(NOW - 3_600_000) },
		now: NOW,
	});

	it('emits a template whose bindings match the card model', () => {
		const template = adaptiveTemplate();
		expect(JSON.stringify(template)).toContain('${name}');
		expect(JSON.stringify(template)).toContain('${metric.value}');
	});

	it('binds the same layout with real values', () => {
		const bound = adaptiveCardFor(card);
		expect(JSON.stringify(bound)).not.toContain('${');
		expect(bound.body[0].columns[1].items[0].text).toBe('Atlas Scout');
		expect(bound.body[2].facts.map((f) => f.title)).toEqual(['This week', 'All time', 'Skills']);
		expect(bound.actions[0].url).toBe('https://three.ws/agents/11111111-2222-3333-4444-555555555555');
	});

	it('substitutes the platform icon when the agent has no public thumbnail', () => {
		expect(adaptiveCardFor(card).body[0].columns[0].items[0].url).toBe(
			'https://three.ws/pwa-192x192.png',
		);
	});

	it('keeps template and bound card structurally identical', () => {
		const shape = (node) =>
			JSON.stringify(node, (key, value) => (typeof value === 'string' ? '#' : value));
		expect(shape(adaptiveTemplate())).toBe(shape(adaptiveCardFor(card)));
	});
});