Street Level photo viewer: keyless ranking, closed waits, a moved canvas

Review follow-ups to the previous commit:

- photoViewers.js ranks a presentation without a mountKey as mounted when
  it began presenting (after the latest key seen, before any later mount),
  so a photo viewer mounting while a keyless one shows is drawn and the
  keyless one superseded; supersede is queued whenever more than one
  presents after any change, not only when the pick changes.
- A panorama's open still waiting on a draw when the last view leaves
  fails as closed and leaves no photo shown, instead of uploading to a
  canvas never drawn. A mount waiting on its first draw then also fails as
  closed and is unmounted, so a view subscribing later sees nothing.
- A view handing back another canvas element than the renderer's drops
  the renderer and draws the panorama again on the new one, as after a
  lost context.
- The guard test allows a .src write in loadImage only on the one `image`
  binding, created by new Image() and never rebound or shadowed, and
  refuses `(expr).src =` and bracket property writes anywhere.
- The test fakes' shared view is reset before each test
  (resetPhotoView), so a viewer left by an earlier test is neither drawn
  with nor superseded by a later test's.

Nothing changes in the running app.

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-19 16:04:21 -08:00
1 parent 33ac0545ef
commit e6f21777ba
11 files changed
+327 -50

No files matched your search

+104 -3
View File
@@ -1,7 +1,14 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createPhotoViewer } from './photoViewer.js';
import { fakeHost, until } from '../../testSupport/kartaViewFakes.mjs';
import {
fakeHost,
until,
resetPhotoView,
} from '../../testSupport/kartaViewFakes.mjs';
// Each test draws its own viewers only.
test.beforeEach(resetPhotoView);
const PHOTOS = {
a: { id: 'a', sequenceId: 's', lon: 1, lat: 2, heading: 0, isPano: true },
@@ -697,12 +704,14 @@ test('a view that draws at once, inside its listener, opens a panorama', async (
unsubscribe();
});
test('an open waiting on the view’s draw goes on when the view unsubscribes', async () => {
const { viewer } = panoramaViewer();
test('a panorama’s open waiting on the view’s draw fails as closed when the view leaves', async () => {
const { viewer, images } = panoramaViewer();
const parts = elements();
const kinds = [];
const presented = [];
// Draws the mount, then nothing more.
const unsubscribe = viewer.subscribeView((view) => {
presented.push(view);
if (!view) return;
kinds.push(view.kind);
if (kinds.length > 1) return;
@@ -714,9 +723,85 @@ test('an open waiting on the view’s draw goes on when the view unsubscribes',
assert.ok(await until(() => kinds.includes('pano')), 'waiting on a draw');
unsubscribe();
assert.equal(await settles(opening), true, 'not left pending');
await assert.rejects(opening, /viewer was closed/);
assert.deepEqual(images, [], 'nothing uploaded to a canvas never shown');
// A view back draws the viewer mounted, with no photo shown.
let again = null;
const resubscribe = viewer.subscribeView((view) => (again = view));
assert.ok(again, 'still mounted');
assert.equal(again.kind, '');
resubscribe();
viewer.unmount();
});
test('a mount waiting on the view’s draw fails as closed when the view leaves, and is unmounted', async () => {
const { viewer } = panoramaViewer();
const unsubscribe = viewer.subscribeView(() => {});
const mounting = viewer.mount(fakeHost());
unsubscribe();
assert.equal(await settles(mounting), true, 'not left pending');
await assert.rejects(mounting, /viewer was closed/);
let current;
const resubscribe = viewer.subscribeView((view) => (current = view));
assert.equal(current, null, 'nothing presented');
await assert.rejects(viewer.open('a'), /not mounted/);
resubscribe();
});
test('a view handing back another canvas gets the panorama drawn again on it', async () => {
const renderers = [];
const reopened = [];
const viewer = createPhotoViewer({
providerId: 'test',
name: 'Test',
sources: SOURCES,
getPhoto: async (id) => PHOTOS[id],
step: async () => 'b',
onStep() {},
onReopen: (id) => {
reopened.push(id);
viewer.open(id);
},
load: async (url) => ({ src: url }),
createRenderer: (canvas) => {
const renderer = {
canvas,
images: [],
destroyed: false,
setImage: (image) => renderer.images.push(image.src),
setView() {},
getView: () => ({ yaw: 10, pitch: 0, fov: 80 }),
resize() {},
clear() {},
destroy: () => (renderer.destroyed = true),
};
renderers.push(renderer);
return renderer;
},
});
const drawWith = (parts) => (view) => {
if (!view) return;
for (const [name, element] of Object.entries(parts))
view.actions.attach(name, element);
};
const first = elements();
const unsubscribe = viewer.subscribeView(drawWith(first));
await viewer.mount(fakeHost());
await viewer.open('a');
assert.equal(renderers[0].canvas, first.canvas);
// Another view takes over, with elements of its own.
const second = elements();
const resubscribe = viewer.subscribeView(drawWith(second));
unsubscribe();
assert.ok(await until(() => renderers[1]?.images.length > 0));
assert.deepEqual(reopened, ['a']);
assert.equal(renderers[0].destroyed, true);
assert.equal(renderers[1].canvas, second.canvas);
assert.deepEqual(renderers[1].images, ['a-first']);
viewer.unmount();
resubscribe();
});
test('the shared fake view draws only the later of two mounts, as the app does', async () => {
const one = setup();
const two = setup();
@@ -727,3 +812,19 @@ test('the shared fake view draws only the later of two mounts, as the app does',
await assert.rejects(one.viewer.open('f'), /not mounted/, 'superseded');
two.viewer.unmount();
});
test('a viewer left by an earlier test is not superseded by a later test’s', async () => {
const earlier = setup();
await earlier.viewer.mount(earlier.host);
// As before each test.
resetPhotoView();
const later = setup();
await later.viewer.mount(later.host);
await tick();
const opening = earlier.viewer.open('f');
await until(() => earlier.loads.includes('f-first'));
earlier.release('f-first');
await opening;
earlier.viewer.unmount();
later.viewer.unmount();
});