306 lines
17 KiB
HTML
306 lines
17 KiB
HTML
<!doctype html>
|
|
<html lang="en"><meta charset="utf-8"><title>Companion movement checks</title>
|
|
<body><pre id="results">Running…</pre>
|
|
<script src="../static/dashboard/companion.js?test=followup-4"></script>
|
|
<script>
|
|
(() => {
|
|
const movement = window.BrowserCompanionMovement;
|
|
const config = movement.configure();
|
|
const results = [];
|
|
function check(label, value) { results.push({ label, passed: !!value }); }
|
|
function scenario(specification) {
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const entries = specification.map(([x, y, width, height, border = '1px solid black', edge = 'top']) => {
|
|
const element = document.createElement('div');
|
|
element.style.cssText = `position:absolute;left:${x}px;top:${y}px;width:${width}px;height:${height}px;border:${border}`;
|
|
host.append(element); return { element, edge };
|
|
});
|
|
const geometry = movement.discover(entries, document.body, config);
|
|
const graph = movement.graphFor(geometry.surfaces, geometry.edges, config);
|
|
host.remove(); return { geometry, graph };
|
|
}
|
|
const short = scenario([[10, 200, 100, 20], [130, 187, 100, 20]]);
|
|
check('short jump up', short.graph.get('0:top').some(step => step.to === '1:top' && step.kind === 'jump'));
|
|
const long = scenario([[10, 200, 100, 20], [240, 187, 100, 20]]);
|
|
check('long jump rejected', long.graph.get('0:top').length === 0);
|
|
const tall = scenario([[10, 200, 100, 20], [10, 50, 100, 150]]);
|
|
check('tall visible side offers climb', tall.graph.get('0:top').some(step => step.kind === 'climb'));
|
|
check('tall side is not jumped', !tall.graph.get('0:top').some(step => step.kind === 'jump'));
|
|
{
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const make = top => {
|
|
const element = document.createElement('div');
|
|
element.style.cssText = `position:absolute;left:20px;top:${top}px;width:280px;height:180px;border:1px solid black`;
|
|
host.append(element); return element;
|
|
};
|
|
const upper = make(20), lower = make(216);
|
|
const entries = [{ element: upper, edge: 'bottom', explicit: true },
|
|
{ element: upper, edge: 'top', explicit: true },
|
|
{ element: lower, edge: 'top', explicit: true },
|
|
{ element: lower, edge: 'bottom', explicit: true }];
|
|
const geometry = movement.discover(entries, document.body, config);
|
|
const graph = movement.graphFor(geometry.surfaces, geometry.edges, config);
|
|
check('first drop has reachable return climb across grid gap',
|
|
graph.get('0:bottom').some(step => step.kind === 'drop' && step.to === '2:top') &&
|
|
graph.get('2:top').some(step => step.kind === 'climb' && step.to === '1:top' &&
|
|
step.attachRise <= config.maxJumpRiseS * config.bodySize));
|
|
host.remove();
|
|
}
|
|
{
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const make = top => {
|
|
const element = document.createElement('div');
|
|
element.style.cssText = `position:absolute;left:20px;top:${top}px;width:280px;height:180px;border:1px solid black`;
|
|
host.append(element); return element;
|
|
};
|
|
const upper = make(20), lower = make(216);
|
|
const entries = [{ element: upper, edge: 'bottom', explicit: true },
|
|
{ element: upper, edge: 'top', explicit: true },
|
|
{ element: lower, edge: 'top', explicit: true },
|
|
{ element: lower, edge: 'bottom', explicit: true }];
|
|
const companion = new window.BrowserCompanion({ surfaces: () => entries,
|
|
config: { activeMinTransitions: 2, activeMaxTransitions: 2 }, reducedMotion: () => false });
|
|
companion.mode = 'active'; companion.activeRemaining = 2;
|
|
const originalRandom = Math.random;
|
|
Math.random = () => .1;
|
|
try {
|
|
const finishMotion = () => {
|
|
for (let i = 0; i < 7 && companion.motion; i++)
|
|
companion.advance(companion.motion.start + companion.motion.duration + 1);
|
|
};
|
|
companion.choose(100);
|
|
finishMotion();
|
|
const dropped = companion.state === 'landing' && companion.platform?.element === lower;
|
|
companion.tick(companion.deadline + 1); cancelAnimationFrame(companion.frame);
|
|
const climbing = !!companion.motion && companion.motion.step?.kind === 'climb';
|
|
const beforeShift = companion.position.y;
|
|
upper.style.top = '0px'; lower.style.top = '196px';
|
|
companion.refresh();
|
|
const attachedAfterShift = !!companion.motion && Math.abs(companion.position.y - beforeShift + 20) < 1;
|
|
finishMotion();
|
|
check('first drop continues into return climb', dropped && climbing &&
|
|
attachedAfterShift &&
|
|
companion.platform?.element === upper && companion.platform.edge === 'top' &&
|
|
companion.activeRemaining === 0);
|
|
} finally { Math.random = originalRandom; companion.destroy(); host.remove(); }
|
|
}
|
|
{
|
|
const element = document.createElement('div');
|
|
element.style.cssText = 'position:absolute;left:10px;top:50px;width:100px;height:150px;border:1px solid black';
|
|
document.body.append(element);
|
|
const geometry = movement.discover([{ element, edge: 'bottom' }, { element, edge: 'top' }], document.body, config);
|
|
const graph = movement.graphFor(geometry.surfaces, geometry.edges, config);
|
|
check('radar bottom can climb to its top', graph.get('0:bottom').some(step =>
|
|
step.to === '1:top' && step.kind === 'climb'));
|
|
element.remove();
|
|
}
|
|
const descent = scenario([[10, 50, 100, 20], [10, 80, 100, 20],
|
|
[10, 110, 100, 20], [10, 140, 100, 20]]);
|
|
const downRoute = movement.routes(descent.graph, '0:top', 16).find(route => route.id === '3:top');
|
|
check('descent uses three safe stops', downRoute?.steps.length === 3 && downRoute.steps.every(step => step.kind === 'drop'));
|
|
const stranded = scenario([[10, 50, 100, 20], [10, 200, 100, 20]]);
|
|
check('unsafe fall has no route', movement.routes(stranded.graph, '0:top', 16).length === 0);
|
|
const borderless = scenario([[10, 50, 100, 20], [10, 80, 100, 20, '0']]);
|
|
check('invisible border rejected', borderless.geometry.surfaces.length === 1 &&
|
|
borderless.geometry.rejected[0]?.reason === 'borderless');
|
|
const transparent = scenario([[10, 50, 100, 20, '1px solid rgba(0, 0, 0, 0)']]);
|
|
check('transparent border rejected', transparent.geometry.surfaces.length === 0);
|
|
check('config is bounded', movement.configure({ maxJumpHorizontalS: 100, bodySize: -1 }).maxJumpHorizontalS === 6 &&
|
|
movement.configure({ bodySize: -1 }).bodySize === 8);
|
|
const button = document.createElement('button'); button.style.cssText =
|
|
'position:absolute;left:10px;top:250px;width:100px;height:25px;border:0';
|
|
document.body.append(button);
|
|
check('borderless button is not a platform', movement.discover([{ element: button }], document.body, config).surfaces.length === 0);
|
|
button.remove();
|
|
function climbSequence(hopChance) {
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const make = (y, height) => {
|
|
const element = document.createElement('div');
|
|
element.style.cssText = `position:absolute;left:10px;top:${y}px;width:100px;height:${height}px;border:1px solid black`;
|
|
host.append(element); return { element, edge: 'top' };
|
|
};
|
|
const entries = [make(200, 20), make(50, 150)];
|
|
const companion = new window.BrowserCompanion({ surfaces: () => entries,
|
|
config: { climbHopChance: hopChance }, reducedMotion: () => false });
|
|
const climb = companion.graph.get('0:top').find(step => step.kind === 'climb');
|
|
companion.startTransition(climb, companion.surfaces[1], 100);
|
|
const sequence = [companion.state];
|
|
for (let i = 0; i < 5 && companion.motion; i++) {
|
|
companion.advance(companion.motion.start + companion.motion.duration + 1);
|
|
sequence.push(companion.state);
|
|
}
|
|
companion.destroy(); host.remove(); return sequence;
|
|
}
|
|
const pull = climbSequence(0).join(',');
|
|
const hop = climbSequence(1).join(',');
|
|
check(`climb pull-up settles without landing (${pull})`, pull ===
|
|
'climbing,climbing,pulling-up,idle');
|
|
check(`short climb hop reaches landing (${hop})`, hop ===
|
|
'climbing,climbing,climb-hopping,landing');
|
|
{
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const make = (x, y) => {
|
|
const element = document.createElement('div');
|
|
element.style.cssText = `position:absolute;left:${x}px;top:${y}px;width:100px;height:20px;border:1px solid black`;
|
|
host.append(element); return { element, edge: 'top' };
|
|
};
|
|
const first = make(10, 200), second = make(130, 187);
|
|
const companion = new window.BrowserCompanion({ surfaces: () => [first, second],
|
|
name: () => 'Pixel', reducedMotion: () => false });
|
|
const before = companion.position.y;
|
|
first.element.style.top = '180px'; second.element.style.top = '167px';
|
|
companion.refresh();
|
|
check('position follows moving source element', Math.abs(companion.position.y - before + 20) < 1);
|
|
const jump = companion.graph.get('0:top').find(step => step.kind === 'jump');
|
|
companion.startTransition(jump, companion.surfaces[1], 100);
|
|
second.element.remove(); companion.refresh();
|
|
check('removed landing cancels motion safely', companion.motion === null &&
|
|
companion.platform?.element === first.element);
|
|
companion.setReducedMotion(true);
|
|
check('reduced motion stops autonomous state', companion.state === 'idle' && companion.motion === null);
|
|
companion.setName('Dahlia');
|
|
check('theme name updates without restarting', companion.renderer.label.textContent.startsWith('Dahlia'));
|
|
companion.destroy(); host.remove();
|
|
}
|
|
{
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const make = (x, y, height) => {
|
|
const element = document.createElement('div');
|
|
element.style.cssText = `position:absolute;left:${x}px;top:${y}px;width:100px;height:${height}px;border:1px solid black`;
|
|
host.append(element); return { element, edge: 'top' };
|
|
};
|
|
const entries = [make(10, 220, 20), make(130, 220, 20), make(-100, 70, 150)];
|
|
const companion = new window.BrowserCompanion({ surfaces: () => entries, reducedMotion: () => false });
|
|
const available = companion.graph.get(companion.platform.id);
|
|
companion.mode = 'active'; companion.activeRemaining = 2;
|
|
const originalRandom = Math.random;
|
|
const values = [.1, .5, .5];
|
|
Math.random = () => values.shift() ?? .5;
|
|
try {
|
|
companion.choose(100);
|
|
check(`safe jump gets selected when climb also exists (${available.map(e=>e.kind).join(',')}; selected ${companion.motion?.step?.kind})`, available.some(edge => edge.kind === 'jump') &&
|
|
available.some(edge => edge.kind === 'climb') &&
|
|
(companion.motion?.step?.kind === 'jump'));
|
|
} finally { Math.random = originalRandom; companion.destroy(); host.remove(); }
|
|
}
|
|
{
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const make = x => {
|
|
const element = document.createElement('div');
|
|
element.style.cssText = `position:absolute;left:${x}px;top:250px;width:100px;height:20px;border:1px solid black`;
|
|
host.append(element); return { element, edge: 'top' };
|
|
};
|
|
const entries = [make(10), make(130)];
|
|
const companion = new window.BrowserCompanion({ surfaces: () => entries, reducedMotion: () => false });
|
|
companion.mode = 'active'; companion.activeRemaining = 3;
|
|
const originalRandom = Math.random;
|
|
Math.random = () => .9;
|
|
try {
|
|
const sequence = [];
|
|
companion.choose(1000);
|
|
for (let count = 0; count < 3; count++) {
|
|
while (companion.motion?.phase === 'approach')
|
|
companion.advance(companion.motion.start + companion.motion.duration + 1);
|
|
sequence.push(companion.state);
|
|
companion.advance(companion.motion.start + companion.motion.duration + 1);
|
|
sequence.push(companion.state);
|
|
const done = companion.deadline + 1;
|
|
companion.tick(done); cancelAnimationFrame(companion.frame);
|
|
sequence.push(companion.state);
|
|
}
|
|
check(`three successive legitimate jumps (${sequence.join(',')})`,
|
|
[0, 3, 6].every(index => sequence[index] === 'jumping' &&
|
|
sequence[index + 1] === 'landing') && sequence[2] === 'jumping' &&
|
|
sequence[5] === 'jumping' && sequence[8] === 'idle');
|
|
check('landing exits once and allows another route', companion.trace.filter(event =>
|
|
event.reason === 'landing interval complete').length === 3);
|
|
} finally { Math.random = originalRandom; companion.destroy(); host.remove(); }
|
|
}
|
|
{
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const element = document.createElement('div');
|
|
element.style.cssText = 'position:absolute;left:10px;top:300px;width:120px;height:20px;border:1px solid black';
|
|
host.append(element);
|
|
const companion = new window.BrowserCompanion({ surfaces: () => [{ element, edge: 'top' }],
|
|
reducedMotion: () => false });
|
|
companion.beginNext(100);
|
|
check('empty route finishes idle', companion.state === 'idle');
|
|
companion.choose(200);
|
|
if (companion.motion?.phase === 'patrol')
|
|
companion.advance(companion.motion.start + companion.motion.duration + 1);
|
|
check('grounded walking never lands', companion.state !== 'landing');
|
|
const groundedStates = [];
|
|
for (const now of [1000, 2000, 3000, 4000, 5000]) {
|
|
companion.tick(now); cancelAnimationFrame(companion.frame);
|
|
groundedStates.push(companion.state);
|
|
}
|
|
check('grounded frames do not retrigger landing', !groundedStates.includes('landing'));
|
|
companion.destroy(); host.remove();
|
|
}
|
|
{
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const element = document.createElement('div');
|
|
element.style.cssText = 'position:absolute;left:10px;top:400px;width:120px;height:20px;border:1px solid black';
|
|
host.append(element);
|
|
const companion = new window.BrowserCompanion({ surfaces: () => [{ element, edge: 'top' }],
|
|
config: { idleMinMs: 12000, idleMaxMs: 12000, sleepMinMs: 20000, sleepMaxMs: 20000 },
|
|
reducedMotion: () => false });
|
|
const firstIdle = companion.deadline - performance.now();
|
|
companion.lastSleepAt = -Infinity; companion.burstsSinceRest = 2;
|
|
const originalRandom = Math.random;
|
|
Math.random = () => 0;
|
|
try {
|
|
const afterIdle = companion.deadline + 1;
|
|
companion.tick(afterIdle); cancelAnimationFrame(companion.frame);
|
|
const slept = companion.state === 'sleeping' && companion.deadline - afterIdle >= 20000;
|
|
const afterSleep = companion.deadline + 1;
|
|
companion.tick(afterSleep); cancelAnimationFrame(companion.frame);
|
|
check('normal idle and sleep dwell without flicker', firstIdle >= 11900 && slept &&
|
|
companion.state === 'idle' && companion.deadline - afterSleep >= 12000);
|
|
companion.lastSleepAt = companion.deadline;
|
|
const afterSecondIdle = companion.deadline + 1;
|
|
companion.tick(afterSecondIdle); cancelAnimationFrame(companion.frame);
|
|
const emoted = ['purring', 'batting'].includes(companion.state) &&
|
|
companion.deadline - afterSecondIdle >= companion.config.emoteMinMs;
|
|
const afterEmote = companion.deadline + 1;
|
|
companion.tick(afterEmote); cancelAnimationFrame(companion.frame);
|
|
check('emote completes into calm idle', emoted && companion.state === 'idle' &&
|
|
companion.deadline - afterEmote >= 12000);
|
|
} finally { Math.random = originalRandom; companion.destroy(); host.remove(); }
|
|
}
|
|
{
|
|
const host = document.createElement('div'); document.body.append(host);
|
|
const entries = [10, 130].map(x => {
|
|
const element = document.createElement('div');
|
|
element.style.cssText = `position:absolute;left:${x}px;top:550px;width:100px;height:20px;border:1px solid black`;
|
|
host.append(element); return { element, edge: 'top' };
|
|
});
|
|
const originalRandom = Math.random;
|
|
let seed = 42;
|
|
Math.random = () => ((seed = (seed * 1664525 + 1013904223) >>> 0) / 4294967296);
|
|
const companion = new window.BrowserCompanion({ surfaces: () => entries, reducedMotion: () => false });
|
|
const events = [];
|
|
const originalSetState = companion.setState.bind(companion);
|
|
companion.setState = (state, reason, now) => {
|
|
if (companion.state !== state) events.push({ state, reason, at: now });
|
|
originalSetState(state, reason, now);
|
|
};
|
|
try {
|
|
let now = performance.now(), start = now;
|
|
for (let i = 0; i < 500 && now - start < 300000; i++) {
|
|
const next = companion.motion ? companion.motion.start + companion.motion.duration + 1 : companion.deadline + 1;
|
|
now = Math.max(now + 1, next);
|
|
companion.tick(now); cancelAnimationFrame(companion.frame);
|
|
}
|
|
const states = new Set(events.map(event => event.state));
|
|
check('five-minute schedule includes exploration, play and rest', now - start >= 300000 &&
|
|
['jumping', 'landing', 'idle', 'purring', 'batting', 'sleeping'].every(state => states.has(state)));
|
|
} finally { Math.random = originalRandom; companion.destroy(); host.remove(); }
|
|
}
|
|
const failed = results.filter(result => !result.passed);
|
|
document.getElementById('results').textContent = results.map(result =>
|
|
`${result.passed ? 'PASS' : 'FAIL'} ${result.label}`).join('\n');
|
|
document.documentElement.dataset.testsPassed = String(failed.length === 0);
|
|
})();
|
|
</script></body></html>
|