Files
gods-eye-view/src/scenes/storyThumb.test.mjs
T
Manjunath b8e622aac8 Stories: reach the globe through the renderer's ports
Main now keeps Cesium behind the renderer contract, so Stories stops
reaching Cesium and the viewer directly.

- Camera: flights, the -60 degree story framing, cancel, the pose saved
  on open and put back on Close go through the camera port; a follow is
  released with stopFollowing.
- Pins: placed in the frame port's postRender from the query port's
  projection and horizon test, still written straight to each pin's
  transform on every drawn frame.
- Tile settling: a continuous-render hold until the globe port reports
  the base settled, for at most 10 s.
- Clock: Close and Back to Explore put back the page's world clock (live,
  or the time it showed, playing on), and leave a clock that is still
  where it should be alone.
- Card images: drawn from Esri World Imagery's own Web Mercator tiles
  while the Esri map stack is active, read as images; no Cesium imagery
  provider is touched.
- THUMBNAIL: the director hands captureStoryThumb the frame port and the
  renderer's canvas, so the scene document's module stays renderer-free.
- Credits: Stories places and keeps visible the application's credit
  line (#gev-credits) and its list as well as the renderer's own display.
- Tests fake the ports instead of a Cesium viewer, and cover pins per
  frame, the flights, the settling bound and the clock restore; the
  share link tests Stories added build their manager over a camera port.
2026-11-16 20:43:53 +05:30

181 lines
5.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
STORY_THUMB_HEIGHT,
STORY_THUMB_MAX_LENGTH,
STORY_THUMB_WIDTH,
captureStoryThumb,
centreCrop,
encodeStoryThumb,
isStoryThumb,
} from './storyThumb.js';
import { STORY_TAGS, isStoryTagList } from './storyTags.js';
/** Canvas double recording each draw; toDataURL answers `url(quality)`. */
function fakeCanvases(url = () => 'data:image/jpeg;base64,AAAA') {
const made = [];
const create = () => {
const canvas = {
width: 0,
height: 0,
draws: [],
qualities: [],
getContext: () => ({
drawImage: (...args) => canvas.draws.push(args),
}),
toDataURL: (type, quality) => {
canvas.qualities.push([type, quality]);
return url(quality);
},
};
made.push(canvas);
return canvas;
};
return { made, create };
}
test('centreCrop keeps the centred 16:9 part of a wider or taller source', () => {
assert.deepEqual(centreCrop(1920, 1080), {
x: 0,
y: 0,
width: 1920,
height: 1080,
});
assert.deepEqual(centreCrop(2000, 1000), {
x: 111,
y: 0,
width: 1778,
height: 1000,
});
assert.deepEqual(centreCrop(800, 1000), {
x: 0,
y: 275,
width: 800,
height: 450,
});
});
test('encodeStoryThumb halves a large view down to 320x180 JPEG at 0.82', () => {
const { made, create } = fakeCanvases();
const source = { width: 2000, height: 1000 };
const url = encodeStoryThumb(source, create);
assert.equal(url, 'data:image/jpeg;base64,AAAA');
// 1778 wide halves to 889, then 445, then the final 320x180.
assert.deepEqual(
made.map(({ width, height }) => [width, height]),
[
[889, 500],
[445, 250],
[STORY_THUMB_WIDTH, STORY_THUMB_HEIGHT],
],
);
assert.deepEqual(
made[0].draws[0].slice(1),
[111, 0, 1778, 1000, 0, 0, 889, 500],
);
assert.deepEqual(made.at(-1).qualities, [['image/jpeg', 0.82]]);
});
test('encodeStoryThumb lowers the quality until the image fits, then gives up', () => {
const big = 'data:image/jpeg;base64,' + 'A'.repeat(STORY_THUMB_MAX_LENGTH);
const fits = fakeCanvases((quality) =>
quality > 0.6 ? big : 'data:image/jpeg;base64,AAAA',
);
assert.equal(
encodeStoryThumb({ width: 320, height: 180 }, fits.create),
'data:image/jpeg;base64,AAAA',
);
assert.deepEqual(
fits.made.at(-1).qualities.map(([, quality]) => quality),
[0.82, 0.7, 0.55],
);
const never = fakeCanvases(() => big);
assert.throws(
() => encodeStoryThumb({ width: 320, height: 180 }, never.create),
/too large/,
);
assert.throws(
() => encodeStoryThumb({ width: 0, height: 0 }, never.create),
/Nothing to capture/,
);
});
/**
* The renderer's frame port double, running postRender listeners on
* requestRender, and the canvas it draws in.
*/
function fakeGlobe({ draws = true } = {}) {
const listeners = new Set();
const frame = {
renders: 0,
reasons: [],
on(phase, listener) {
assert.equal(phase, 'postRender');
listeners.add(listener);
return () => listeners.delete(listener);
},
requestRender(reason) {
frame.renders++;
frame.reasons.push(reason);
if (draws) for (const listener of [...listeners]) listener({});
},
listeners,
};
return { globe: { frame, canvas: { width: 640, height: 360 } }, frame };
}
test('captureStoryThumb reads the canvas inside the postRender it requested', async () => {
const { globe, frame } = fakeGlobe();
const { create } = fakeCanvases();
const url = await captureStoryThumb(globe, { createCanvas: create });
assert.equal(url, 'data:image/jpeg;base64,AAAA');
assert.equal(frame.renders, 1);
assert.deepEqual(frame.reasons, ['story-thumb']);
assert.equal(frame.listeners.size, 0, 'the listener is removed');
});
test('captureStoryThumb gives up when no frame is drawn or there is no globe', async () => {
const { globe, frame } = fakeGlobe({ draws: false });
await assert.rejects(
captureStoryThumb(globe, {
createCanvas: fakeCanvases().create,
timeoutMs: 1,
}),
/No frame was drawn/,
);
assert.equal(frame.listeners.size, 0);
await assert.rejects(
captureStoryThumb(
{ frame: null, canvas: null },
{ createCanvas: fakeCanvases().create },
),
/Globe not ready/,
);
});
test('isStoryThumb accepts only bounded base64 JPEG or PNG data URLs', () => {
assert.equal(isStoryThumb('data:image/jpeg;base64,AAAA'), true);
assert.equal(isStoryThumb('data:image/png;base64,AAA='), true);
for (const value of [
'',
null,
42,
'data:image/gif;base64,AAAA',
'data:image/jpeg,AAAA',
'https://example.com/a.jpg',
'data:image/jpeg;base64,AA"A',
'data:image/jpeg;base64,' + 'A'.repeat(STORY_THUMB_MAX_LENGTH),
])
assert.equal(isStoryThumb(value), false, String(value).slice(0, 40));
});
test('isStoryTagList accepts up to five different known tags', () => {
assert.equal(STORY_TAGS.length, 10);
assert.equal(isStoryTagList([]), true);
assert.equal(isStoryTagList(STORY_TAGS.slice(0, 5)), true);
assert.equal(isStoryTagList(STORY_TAGS.slice(0, 6)), false);
assert.equal(isStoryTagList(['Floods', 'Floods']), false);
assert.equal(isStoryTagList(['floods']), false);
assert.equal(isStoryTagList('Floods'), false);
});