mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-23 11:42:21 +00:00
fix(recent-imagery): address dateline review (halves availability, voice guard, export, renderer fallback)
This commit is contained in:
1 parent
9a34d63f1d
commit
d60f2420f4
9 files changed
+209
-54
No files matched your search
@@ -148,8 +148,9 @@ export function createRecentImageryRenderer({
|
||||
}
|
||||
|
||||
function mount(slotId, { candidate, box, alpha, split }) {
|
||||
const rectangles = nonCrossingBoxes(box);
|
||||
const parts = rectangles.length ? rectangles : [box];
|
||||
// [] means "not a box": add no layer rather than drape the raw box.
|
||||
const parts = nonCrossingBoxes(box);
|
||||
if (!parts.length) return false;
|
||||
// Build every provider before adding any layer, so a throw on the second
|
||||
// half of a dateline box cannot leave the first layer untracked.
|
||||
const providers = parts.map((part) => createProvider(candidate, part));
|
||||
@@ -176,6 +177,7 @@ export function createRecentImageryRenderer({
|
||||
split,
|
||||
};
|
||||
applyLook(owned[slotId]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -215,7 +217,7 @@ export function createRecentImageryRenderer({
|
||||
return true;
|
||||
}
|
||||
removeOwned(slotId);
|
||||
mount(slotId, { candidate, box, ...look });
|
||||
if (!mount(slotId, { candidate, box, ...look })) return false;
|
||||
requestRender('recent-imagery-show');
|
||||
return true;
|
||||
},
|
||||
|
||||
@@ -287,3 +287,13 @@ test('a dateline provider that fails on the second half adds no layer', () => {
|
||||
assert.equal(globe.layers.length, 0);
|
||||
assert.equal(renderer.ownedCount(), 0);
|
||||
});
|
||||
|
||||
test('a box that is not a box adds no layer and drapes nothing', () => {
|
||||
const { renderer, globe, renders } = fixture();
|
||||
for (const bad of [{ west: NaN, south: 0, east: 1, north: 1 }, { west: 0 }]) {
|
||||
assert.equal(renderer.showSlot('a', S30, bad), false);
|
||||
}
|
||||
assert.equal(globe.layers.length, 0);
|
||||
assert.equal(renderer.ownedCount(), 0);
|
||||
assert.deepEqual(renders, []);
|
||||
});
|
||||
@@ -3,17 +3,34 @@ import { gibsTemplate, nonCrossingBoxes, wvsSnapshotUrl } from './model.js';
|
||||
|
||||
/**
|
||||
* Worldview Snapshots rejects a BBOX with west > east, so a dateline box is
|
||||
* probed on its wider side. That is enough for the Data-Present answer, and
|
||||
* the thumbnail shows that side.
|
||||
* probed as its two non-crossing halves, wider first. Granules do not cross
|
||||
* +/-180, so a day has data when either half does.
|
||||
*/
|
||||
function requestBox(box) {
|
||||
if (!(box?.west > box?.east)) return box;
|
||||
function requestBoxes(box) {
|
||||
if (!(box?.west > box?.east)) return [box];
|
||||
const halves = nonCrossingBoxes(box);
|
||||
return halves.reduce(
|
||||
(wider, half) =>
|
||||
half.east - half.west > wider.east - wider.west ? half : wider,
|
||||
halves[0] ?? box,
|
||||
);
|
||||
if (!halves.length) return [box];
|
||||
return [...halves].sort((a, b) => b.east - b.west - (a.east - a.west));
|
||||
}
|
||||
|
||||
const isPresent = (response) => dataPresent(response) === 'true';
|
||||
|
||||
const dataPresent = (response) =>
|
||||
response?.ok
|
||||
? String(response.headers?.get?.('Data-Present') ?? '').toLowerCase()
|
||||
: '';
|
||||
|
||||
/**
|
||||
* One answer for a crossing box: the first half that has data (the wider side
|
||||
* wins a tie, so the thumbnail shows it); both confirmed empty is empty; any
|
||||
* other mix (a failed or unreadable half) is returned as that half's response
|
||||
* so the day is never called empty on partial evidence.
|
||||
*/
|
||||
function combineHalves(responses) {
|
||||
const present = responses.find(isPresent);
|
||||
if (present) return present;
|
||||
const unproven = responses.find((r) => dataPresent(r) !== 'false');
|
||||
return unproven ?? responses[0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,18 +50,29 @@ export function createRecentImagerySource({
|
||||
searchHls: (query) => search({ ...query, fetchImpl }),
|
||||
async getThumbnail(candidate, box, { size = 256, signal } = {}) {
|
||||
signal?.throwIfAborted();
|
||||
const response = await fetchImpl(
|
||||
thumbnailUrl({
|
||||
product: candidate.product,
|
||||
day: candidate.day,
|
||||
box: requestBox(box),
|
||||
width: size,
|
||||
height: size,
|
||||
}),
|
||||
{ signal },
|
||||
const settled = await Promise.allSettled(
|
||||
requestBoxes(box).map((half) =>
|
||||
fetchImpl(
|
||||
thumbnailUrl({
|
||||
product: candidate.product,
|
||||
day: candidate.day,
|
||||
box: half,
|
||||
width: size,
|
||||
height: size,
|
||||
}),
|
||||
{ signal },
|
||||
),
|
||||
),
|
||||
);
|
||||
signal?.throwIfAborted();
|
||||
return response;
|
||||
const responses = settled
|
||||
.filter((result) => result.status === 'fulfilled')
|
||||
.map((result) => result.value);
|
||||
// A half that data proves present settles the day; otherwise a
|
||||
// rejected half is an error, never an empty day.
|
||||
if (responses.length < settled.length && !responses.some(isPresent))
|
||||
throw settled.find((result) => result.status === 'rejected').reason;
|
||||
return responses.length > 1 ? combineHalves(responses) : responses[0];
|
||||
},
|
||||
getTileTemplate: (product, day) => tileTemplate(product, day),
|
||||
};
|
||||
|
||||
@@ -41,7 +41,7 @@ test('cancelled thumbnail acquisition does not return stale responses or retry',
|
||||
assert.equal(calls, 1);
|
||||
});
|
||||
|
||||
test('a dateline thumbnail is requested for the wider side, never as a crossing BBOX', async () => {
|
||||
test('a dateline thumbnail is requested as two non-crossing halves, never as a crossing BBOX', async () => {
|
||||
const requested = [];
|
||||
const source = createRecentImagerySource({
|
||||
thumbnailUrl: ({ box }) => {
|
||||
@@ -57,23 +57,100 @@ test('a dateline thumbnail is requested for the wider side, never as a crossing
|
||||
east: -179,
|
||||
north: 1,
|
||||
});
|
||||
assert.deepEqual(requested.at(-1), {
|
||||
west: -180,
|
||||
south: 0,
|
||||
east: -179,
|
||||
north: 1,
|
||||
});
|
||||
assert.deepEqual(requested, [
|
||||
{ west: -180, south: 0, east: -179, north: 1 },
|
||||
{ west: 179.9, south: 0, east: 180, north: 1 },
|
||||
]);
|
||||
requested.length = 0;
|
||||
await source.getThumbnail(candidate, {
|
||||
west: 179,
|
||||
south: 0,
|
||||
east: -179.9,
|
||||
north: 1,
|
||||
});
|
||||
assert.deepEqual(requested.at(-1), {
|
||||
west: 179,
|
||||
south: 0,
|
||||
east: 180,
|
||||
north: 1,
|
||||
});
|
||||
assert.deepEqual(requested[0], { west: 179, south: 0, east: 180, north: 1 });
|
||||
assert.equal(requested.length, 2);
|
||||
assert.ok(requested.every((box) => box.west <= box.east));
|
||||
});
|
||||
|
||||
const NARROW_WEST = { west: 179.9, south: 0, east: -179, north: 1 };
|
||||
const dated = (present, body = 'image') =>
|
||||
new Response(body, { headers: { 'Data-Present': present } });
|
||||
function halvesSource(answers) {
|
||||
const requested = [];
|
||||
const source = createRecentImagerySource({
|
||||
thumbnailUrl: ({ box }) => `https://imagery.example/${box.west}/${box.east}`,
|
||||
fetchImpl: async (url) => {
|
||||
requested.push(url);
|
||||
const answer = answers[url.split('imagery.example/')[1]];
|
||||
if (answer instanceof Error) throw answer;
|
||||
return answer.clone ? answer : answer();
|
||||
},
|
||||
});
|
||||
return { source, requested };
|
||||
}
|
||||
const candidate = { product: 'S30', day: '2026-10-01' };
|
||||
|
||||
test('a crossing box probes both halves and is present when only the narrow half has data', async () => {
|
||||
const { source, requested } = halvesSource({
|
||||
'-180/-179': dated('false'),
|
||||
'179.9/180': dated('true', 'narrow'),
|
||||
});
|
||||
const response = await source.getThumbnail(candidate, NARROW_WEST);
|
||||
assert.equal(requested.length, 2);
|
||||
assert.equal(response.headers.get('Data-Present'), 'true');
|
||||
assert.equal(await response.text(), 'narrow');
|
||||
});
|
||||
|
||||
test('a crossing box with data only on the wider half is present and shows the wider side', async () => {
|
||||
const { source } = halvesSource({
|
||||
'-180/-179': dated('true', 'wide'),
|
||||
'179.9/180': dated('false'),
|
||||
});
|
||||
const response = await source.getThumbnail(candidate, NARROW_WEST);
|
||||
assert.equal(await response.text(), 'wide');
|
||||
});
|
||||
|
||||
test('a crossing box is empty only when both halves are confirmed empty', async () => {
|
||||
const { source } = halvesSource({
|
||||
'-180/-179': dated('false'),
|
||||
'179.9/180': dated('false'),
|
||||
});
|
||||
const response = await source.getThumbnail(candidate, NARROW_WEST);
|
||||
assert.equal(response.headers.get('Data-Present'), 'false');
|
||||
});
|
||||
|
||||
test('a crossing box is not called empty when the other half failed', async () => {
|
||||
const { source } = halvesSource({
|
||||
'-180/-179': dated('false'),
|
||||
'179.9/180': () => new Response('x', { status: 503 }),
|
||||
});
|
||||
const response = await source.getThumbnail(candidate, NARROW_WEST);
|
||||
assert.equal(response.ok, false);
|
||||
});
|
||||
|
||||
test('a crossing box with both halves present shows the wider half', async () => {
|
||||
const { source } = halvesSource({
|
||||
'-180/-179': dated('true', 'wide'),
|
||||
'179.9/180': dated('true', 'narrow'),
|
||||
});
|
||||
const response = await source.getThumbnail(candidate, NARROW_WEST);
|
||||
assert.equal(await response.text(), 'wide');
|
||||
});
|
||||
|
||||
test('a rejected half does not hide data on the other half, and never reads as empty', async () => {
|
||||
const { source } = halvesSource({
|
||||
'-180/-179': new Error('network'),
|
||||
'179.9/180': dated('true', 'narrow'),
|
||||
});
|
||||
const response = await source.getThumbnail(candidate, NARROW_WEST);
|
||||
assert.equal(await response.text(), 'narrow');
|
||||
const failing = halvesSource({
|
||||
'-180/-179': new Error('network'),
|
||||
'179.9/180': dated('false'),
|
||||
});
|
||||
await assert.rejects(
|
||||
failing.source.getThumbnail(candidate, NARROW_WEST),
|
||||
/network/,
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user