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(glance): pin the widget card's bindings to the payload the worker sends

nirholas9a687ec2bf99
diff --git a/tests/glance-sw.test.js b/tests/glance-sw.test.js
index 71eec8f4f..8e7eaee31 100644
--- a/tests/glance-sw.test.js
+++ b/tests/glance-sw.test.js
@@ -8,6 +8,7 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
+import { adaptiveTemplate } from '../api/_lib/glance-adaptive.js';
const SW_SOURCE = readFileSync(resolve(process.cwd(), 'public/glance-sw.js'), 'utf8');
@@ -295,3 +296,93 @@ describe('glance widget worker', () => {
expect(SW_SOURCE).toMatch(/AbortSignal\.timeout\(GLANCE_TIMEOUT_MS\)/);
});
});
+
+// The board binds the server's card to the worker's payload, and neither half
+// can see the other: a template that names a field the worker stopped sending
+// renders an empty row on the widgets board and nowhere else, which is the one
+// surface no test machine here has. So the two shapes are checked against each
+// other directly.
+describe('the card the board renders binds to the payload the worker sends', () => {
+ /** Every `${...}` in the template, as a path plus whether it has a fallback. */
+ function bindings(node, found = []) {
+ if (typeof node === 'string') {
+ const match = node.match(/^\$\{(?:if\((?<guarded>[\w.]+),|(?<plain>[\w.]+)\})/);
+ if (match) {
+ const path = match.groups.plain || match.groups.guarded;
+ found.push({ path, optional: Boolean(match.groups.guarded) });
+ }
+ return found;
+ }
+ if (Array.isArray(node)) {
+ for (const item of node) bindings(item, found);
+ return found;
+ }
+ if (node && typeof node === 'object') {
+ for (const value of Object.values(node)) bindings(value, found);
+ }
+ return found;
+ }
+
+ const resolvePath = (payload, path) =>
+ path.split('.').reduce((at, key) => (at === undefined || at === null ? undefined : at[key]), payload);
+
+ const template = adaptiveTemplate();
+ const bound = bindings(template);
+
+ const AGENT = {
+ signedIn: true,
+ card: {
+ name: 'Atlas Scout',
+ headline: 'Working.',
+ image: 'https://cdn.example/thumb.png',
+ url: 'https://three.ws/agents/abc',
+ status: 'active',
+ metric: { label: 'Moves today', value: 17 },
+ // The server sends exactly three, always: api/_lib/glance-card.js fills
+ // the third with "Days live" when the agent has no skills yet.
+ stats: [
+ { label: 'This week', value: 96 },
+ { label: 'All time', value: 400 },
+ { label: 'Days live', value: 12 },
+ ],
+ updatedAt: '2026-09-04T12:00:00.000Z',
+ },
+ };
+
+ it('reads a binding for every field the card draws', () => {
+ // `name` binds twice, as the title and as the image's alt text.
+ expect([...new Set(bound.map((b) => b.path))].sort()).toEqual([
+ 'createUrl',
+ 'headline',
+ 'image',
+ 'metric.label',
+ 'metric.value',
+ 'name',
+ 'stats.0.label',
+ 'stats.0.value',
+ 'stats.1.label',
+ 'stats.1.value',
+ 'stats.2.label',
+ 'stats.2.value',
+ 'url',
+ ]);
+ });
+
+ it.each([
+ ['signed out', { signedIn: false, signInUrl: 'https://three.ws/login' }],
+ ['no agent yet', { signedIn: true, card: null, createUrl: 'https://three.ws/create' }],
+ ['a live agent', AGENT],
+ ])('resolves every required binding for %s', (_label, body) => {
+ const payload = loadWorker().self.__threewsGlancePayload(body);
+ const missing = bound
+ .filter((b) => !b.optional && resolvePath(payload, b.path) === undefined)
+ .map((b) => b.path);
+ expect(missing).toEqual([]);
+ });
+
+ it('fills the third fact slot rather than leaving the card ragged', () => {
+ const payload = loadWorker().self.__threewsGlancePayload(AGENT);
+ expect(payload.stats).toHaveLength(3);
+ expect(payload.stats[2]).toEqual({ label: 'Days live', value: '12' });
+ });
+});