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(assets): cover clip compaction and the resize-proxy URL builder

nirholasbd5cebcd6248
diff --git a/scripts/compact-clips.mjs b/scripts/compact-clips.mjs
index 5a9463d8b..d243bc54b 100644
--- a/scripts/compact-clips.mjs
+++ b/scripts/compact-clips.mjs
@@ -25,8 +25,7 @@ const SIGNIFICANT_DIGITS = 7;
export function compactNumber(n) {
if (!Number.isFinite(n) || Number.isInteger(n)) return n;
- const rounded = Number(n.toPrecision(SIGNIFICANT_DIGITS));
- return Object.is(rounded, -0) ? 0 : rounded;
+ return Number(n.toPrecision(SIGNIFICANT_DIGITS));
}
// Deep copy with every number compacted. Track arrays (times/values) are the
diff --git a/tests/compact-clips.test.js b/tests/compact-clips.test.js
new file mode 100644
index 000000000..207ff7122
--- /dev/null
+++ b/tests/compact-clips.test.js
@@ -0,0 +1,71 @@
+// scripts/compact-clips.mjs: baked clip JSON is stored at float32 precision.
+// The baker applies compactClipJson() to every clip it writes and the CLI
+// rewrites the committed library the same way, so both rely on the same
+// guarantees: numbers round to 7 significant digits, integers and non-numeric
+// data pass through untouched, and a second pass changes nothing.
+import { describe, it, expect } from 'vitest';
+import { compactNumber, compactClipJson, compactClipText } from '../scripts/compact-clips.mjs';
+
+describe('compactNumber', () => {
+ it('rounds a double-printed float32 to 7 significant digits', () => {
+ expect(compactNumber(0.03333333507180214)).toBe(0.03333334);
+ expect(compactNumber(15.800000190734863)).toBe(15.8);
+ expect(compactNumber(-0.9998477101325989)).toBe(-0.9998477);
+ });
+ it('leaves integers, zero, and non-finite values alone', () => {
+ expect(compactNumber(30)).toBe(30);
+ expect(compactNumber(0)).toBe(0);
+ expect(compactNumber(-3)).toBe(-3);
+ expect(compactNumber(Infinity)).toBe(Infinity);
+ expect(Number.isNaN(compactNumber(NaN))).toBe(true);
+ });
+ it('keeps tiny values instead of flushing them to zero (precision is relative)', () => {
+ expect(compactNumber(-1e-12)).toBe(-1e-12);
+ expect(compactNumber(3.4028234e-38)).toBe(3.402823e-38);
+ });
+ it('keeps the relative error within float32 precision', () => {
+ for (const v of [0.123456789, 1234.56789, 3.4028234e-5, 0.7071067811865476]) {
+ expect(Math.abs(compactNumber(v) - v) / Math.abs(v)).toBeLessThan(1e-6);
+ }
+ });
+});
+
+describe('compactClipJson', () => {
+ const clip = {
+ name: 'idle',
+ duration: 15.800000190734863,
+ tracks: [
+ { name: 'Hips.position', type: 'vector', times: [0, 0.03333333507180214], values: [0, 0.9800000190734863, 0.012345678901234] },
+ { name: 'Spine.quaternion', type: 'quaternion', times: [0], values: [0, 0, 0, 1] },
+ ],
+ uuid: 'abc-123',
+ blendMode: 2500,
+ };
+ it('compacts every number in tracks and metadata, preserving structure', () => {
+ const out = compactClipJson(clip);
+ expect(out.duration).toBe(15.8);
+ expect(out.tracks[0].times).toEqual([0, 0.03333334]);
+ expect(out.tracks[0].values).toEqual([0, 0.98, 0.01234568]);
+ expect(out.tracks[1].values).toEqual([0, 0, 0, 1]);
+ expect(out.name).toBe('idle');
+ expect(out.uuid).toBe('abc-123');
+ expect(out.blendMode).toBe(2500);
+ expect(out.tracks[1].type).toBe('quaternion');
+ });
+ it('does not mutate its input', () => {
+ const before = JSON.stringify(clip);
+ compactClipJson(clip);
+ expect(JSON.stringify(clip)).toBe(before);
+ });
+ it('is idempotent as text', () => {
+ const once = compactClipText(JSON.stringify(clip));
+ expect(compactClipText(once)).toBe(once);
+ });
+ it('shrinks a double-precision clip substantially', () => {
+ const times = Array.from({ length: 300 }, (_, i) => i / 30);
+ const values = times.map((t) => Math.sin(t) * 0.5);
+ const raw = JSON.stringify({ name: 'wave', duration: times[times.length - 1], tracks: [{ name: 'Arm.position', times, values }] });
+ const compact = compactClipText(raw);
+ expect(compact.length).toBeLessThan(raw.length * 0.6);
+ });
+});
diff --git a/tests/image-url.test.js b/tests/image-url.test.js
new file mode 100644
index 000000000..789a09707
--- /dev/null
+++ b/tests/image-url.test.js
@@ -0,0 +1,31 @@
+// src/shared/image-url.js: remote artwork goes through the same-origin resize
+// proxy; local, data: and blob: sources are returned untouched.
+// @vitest-environment jsdom
+import { describe, it, expect } from 'vitest';
+import { resizedImageUrl } from '../src/shared/image-url.js';
+
+describe('resizedImageUrl', () => {
+ it('routes a remote https image through /api/img with the requested width', () => {
+ const out = resizedImageUrl('https://pub-example.r2.dev/forge/a/b.png', 480);
+ const u = new URL(out, 'http://localhost');
+ expect(u.pathname).toBe('/api/img');
+ expect(u.searchParams.get('url')).toBe('https://pub-example.r2.dev/forge/a/b.png');
+ expect(u.searchParams.get('w')).toBe('480');
+ });
+ it('rounds a fractional width and omits w when no width is given', () => {
+ expect(new URL(resizedImageUrl('https://cdn.example/x.jpg', 239.6), 'http://l').searchParams.get('w')).toBe('240');
+ expect(new URL(resizedImageUrl('https://cdn.example/x.jpg'), 'http://l').searchParams.has('w')).toBe(false);
+ });
+ it('leaves same-origin, relative, data: and blob: sources untouched', () => {
+ const same = `${location.origin}/thumb/abc.png`;
+ expect(resizedImageUrl(same, 480)).toBe(same);
+ expect(resizedImageUrl('/avatars/default.png', 480)).toBe('/avatars/default.png');
+ expect(resizedImageUrl('data:image/png;base64,AAAA', 480)).toBe('data:image/png;base64,AAAA');
+ expect(resizedImageUrl('blob:http://localhost/1234', 480)).toBe('blob:http://localhost/1234');
+ });
+ it('passes through empty and non-string inputs', () => {
+ expect(resizedImageUrl('', 480)).toBe('');
+ expect(resizedImageUrl(null, 480)).toBe(null);
+ expect(resizedImageUrl(undefined, 480)).toBe(undefined);
+ });
+});