fix(cctv): keep the Context camera card's still and tidy the visibility port

The still preview gate looked only at the CCTV panel, so the Context
camera card, which reads the same frame, stopped updating while the
panel was collapsed or another phone sheet was open. A mounted card now
counts itself in the CCTV store, and the still is requested while the
panel or a card is shown; live video still follows the panel only.

Review follow-ups in the same area:
- visiblePoints is registered once, in createCesiumPorts, and drops
  checks for input that cannot happen; the contract documents
  horizonMarginM and when it answers false or null.
- The layer passes the mount and the four corners once each and asks
  findCamera for the port.
- Phone sheet names come from a shared module instead of literals.
- Layer tests cover the fetch gate; the geometry cases moved to the
  camera port tests. Comments and CURRENT-STATE match the code.
This commit is contained in:
German Khokhlov committed 2026-11-19 18:28:51 +04:00
1 parent b020a1cd91
commit 99a70a0e0c
18 files changed
+545 -430

No files matched your search

+68 -44
View File
@@ -20,7 +20,7 @@ const BUTTONS = {
_cctvProjectionBtn: 'cctv-projection-btn',
};
function panel(t) {
function panel(t, { video = false } = {}) {
document.body.replaceChildren();
const host = document.body.appendChild(document.createElement('div'));
const pinia = createPinia();
@@ -32,6 +32,7 @@ function panel(t) {
document.getElementById(id),
]),
);
if (video) elements._cctvVideo = document.getElementById('cctv-video');
const controls = new CctvControls({
elements: {
...elements,
@@ -377,16 +378,7 @@ test('a second edit of the same chip gets a fresh input', async (t) => {
});
test('the frame preview renders each step of a camera frame request', async (t) => {
const prior = globalThis.Image;
const requests = [];
globalThis.Image = class {
constructor() {
requests.push(this);
}
};
t.after(() => {
globalThis.Image = prior;
});
const requests = mockImages(t);
const { controls, byId } = panel(t);
// The preview loads while the panel is open; the panel starts collapsed.
byId('cctv-panel').classList.remove('collapsed');
@@ -610,15 +602,7 @@ test('the meta line of a disabled layer does not describe its last camera', () =
});
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
const stillState = (frameUrl) => ({
enabled: true,
count: 1,
cameraIndex: [camera],
activeCameraId: 'a',
activeCamera: { ...camera, frameUrl, sourceKind: 'snapshot' },
});
test('the still preview is fetched only while the tab and the panel are shown', async (t) => {
function mockImages(t) {
const prior = globalThis.Image;
const requests = [];
globalThis.Image = class {
@@ -626,6 +610,22 @@ test('the still preview is fetched only while the tab and the panel are shown',
requests.push(this);
}
};
t.after(() => {
globalThis.Image = prior;
});
return requests;
}
const stillState = (frameUrl, id = 'a') => ({
enabled: true,
count: 2,
cameraIndex: [camera, { ...camera, id: 'b' }],
activeCameraId: id,
activeCamera: { ...camera, id, frameUrl, sourceKind: 'snapshot' },
});
test('the still preview is fetched only while the tab and the panel are shown', async (t) => {
const requests = mockImages(t);
const hidden = Object.getOwnPropertyDescriptor(document, 'hidden');
let tabHidden = false;
Object.defineProperty(document, 'hidden', {
@@ -633,11 +633,11 @@ test('the still preview is fetched only while the tab and the panel are shown',
get: () => tabHidden,
});
t.after(() => {
globalThis.Image = prior;
if (hidden) Object.defineProperty(document, 'hidden', hidden);
else delete document.hidden;
});
const { controls, byId } = panel(t);
assert.equal(controls._cctvVideo, undefined);
const panelElement = byId('cctv-panel');
const state = stillState;
@@ -668,15 +668,8 @@ test('the still preview is fetched only while the tab and the panel are shown',
});
test('the still preview waits for the Cameras sheet on phones', async (t) => {
const prior = globalThis.Image;
const requests = [];
globalThis.Image = class {
constructor() {
requests.push(this);
}
};
const requests = mockImages(t);
t.after(() => {
globalThis.Image = prior;
document.body.classList.remove('mobile-shell');
document.body.removeAttribute('data-mobile-sheet');
});
@@ -704,22 +697,53 @@ test('the still preview waits for the Cameras sheet on phones', async (t) => {
assert.equal(requests.length, 2, 'desktop ignores the sheet');
});
test('a still-only panel (no video element) resumes from the class change', async (t) => {
const prior = globalThis.Image;
const requests = [];
globalThis.Image = class {
constructor() {
requests.push(this);
}
};
t.after(() => {
globalThis.Image = prior;
});
test('a camera picked while the panel is collapsed shows its own still on expanding', async (t) => {
const requests = mockImages(t);
const { controls, byId } = panel(t);
assert.equal(controls._cctvVideo, undefined);
const panelElement = byId('cctv-panel');
panelElement.classList.remove('collapsed');
controls._renderCctvState(stillState('a1.jpg'));
assert.equal(requests.length, 0);
byId('cctv-panel').classList.remove('collapsed');
await settle();
assert.equal(requests.length, 1);
assert.equal(requests[0].src, 'a1.jpg');
panelElement.classList.add('collapsed');
await settle();
controls._renderCctvState(stillState('b1.jpg', 'b'));
assert.equal(requests.length, 1);
requests[0].onload();
panelElement.classList.remove('collapsed');
await settle();
await nextTick();
assert.equal(requests.length, 2);
assert.equal(requests[1].src, 'b1.jpg');
assert.equal(byId('cctv-frame').dataset.cameraId, 'b');
assert.notEqual(byId('cctv-frame').getAttribute('src'), 'a1.jpg');
});
test('leaving the Cameras sheet stops the live video and coming back starts it again', async (t) => {
const { controls, byId } = panel(t, { video: true });
byId('cctv-panel').classList.remove('collapsed');
document.body.classList.add('mobile-shell');
document.body.setAttribute('data-mobile-sheet', 'cameras');
t.after(() => {
document.body.classList.remove('mobile-shell');
document.body.removeAttribute('data-mobile-sheet');
});
controls._renderCctvState({
enabled: true,
count: 1,
cameraIndex: [camera],
activeCameraId: 'a',
activeCamera: { ...camera, isVideo: true },
});
const first = controls._cctvVideoSurface;
assert.ok(first);
const stop = t.mock.method(first, 'stop');
document.body.setAttribute('data-mobile-sheet', 'layers');
await settle();
assert.equal(stop.mock.callCount(), 1);
assert.equal(controls._cctvVideoSurface, null);
document.body.setAttribute('data-mobile-sheet', 'cameras');
await settle();
assert.ok(controls._cctvVideoSurface);
assert.notEqual(controls._cctvVideoSurface, first);
});