Use a new bundled image for both the welcome screen and the dashboard fallback, and crossfade into the first feed photo only when arriving from the welcome screen or when the wallpaper changes after the first paint (a live rotation or a manual pick). A plain open shows the resolved photo outright, with no transition. - wallpaper.gjs: three layers (bundled base, last settled photo, current photo). The current photo fades in on load and the previous one stays beneath for a crossfade; the photo layer renders only from the cached displayUrl, so a change fades instead of flashing the bundled base. Reduced motion settles instantly. - photos.js: keep the last-known-good wallpaper on a cache miss or fetch error; add a one-shot timer that rotates at the day/week boundary while the dashboard stays open; expose animateEntry for the welcome entry. - routes/index.js: set photos.animateEntry from the transition origin. A strict-mode route template's `this` is not the controller, so a controller property never reached the component. - config/nostr.js: alias the welcome and fallback URLs to one image. Tests and docs updated.
603 lines
18 KiB
JavaScript
603 lines
18 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import { rotationIndex } from 'dashtab/utils/rotation';
|
|
import photoEvents from '../../fixtures/nostr/photo-events.json';
|
|
import feedEvents from '../../fixtures/nostr/feed-events.json';
|
|
|
|
class FakeStorage {
|
|
data = new Map();
|
|
|
|
key(namespace, key) {
|
|
return `${namespace}:${key}`;
|
|
}
|
|
|
|
async get(namespace, key) {
|
|
return this.data.get(this.key(namespace, key)) ?? null;
|
|
}
|
|
|
|
async set(namespace, key, value) {
|
|
this.data.set(this.key(namespace, key), value);
|
|
}
|
|
|
|
async keys(namespace) {
|
|
const prefix = `${namespace}:`;
|
|
return [...this.data.keys()]
|
|
.filter((key) => key.startsWith(prefix))
|
|
.map((key) => key.slice(prefix.length));
|
|
}
|
|
}
|
|
|
|
class FakeNostr {
|
|
feedEvents = feedEvents;
|
|
photoEvents = photoEvents;
|
|
calls = [];
|
|
|
|
async queryFeedEvents() {
|
|
this.calls.push('feeds');
|
|
return this.feedEvents;
|
|
}
|
|
|
|
async queryPhotos(ids) {
|
|
this.calls.push({ ids });
|
|
return this.photoEvents.filter((event) => ids.includes(event.id));
|
|
}
|
|
}
|
|
|
|
class FakePhotoCache {
|
|
cached = new Map();
|
|
calls = [];
|
|
deferred = new Map();
|
|
failing = new Set();
|
|
|
|
// Gate `prefetchOriginal` for this id behind a promise the test resolves.
|
|
defer(photoId) {
|
|
let resolve;
|
|
const promise = new Promise((settle) => {
|
|
resolve = settle;
|
|
});
|
|
this.deferred.set(photoId, { promise, resolve });
|
|
return { promise, resolve };
|
|
}
|
|
|
|
fail(photoId) {
|
|
this.failing.add(photoId);
|
|
}
|
|
|
|
async originalUrl(photo) {
|
|
this.calls.push(['originalUrl', photo.id]);
|
|
return this.cached.get(photo.id) ?? null;
|
|
}
|
|
|
|
async prefetchOriginal(photo) {
|
|
this.calls.push(['original', photo.id]);
|
|
|
|
if (this.failing.has(photo.id)) {
|
|
throw new Error('prefetch failed');
|
|
}
|
|
|
|
const gate = this.deferred.get(photo.id);
|
|
if (gate) {
|
|
const url = await gate.promise;
|
|
this.cached.set(photo.id, url);
|
|
return url;
|
|
}
|
|
|
|
const url = `blob:original:${photo.id}`;
|
|
this.cached.set(photo.id, url);
|
|
return url;
|
|
}
|
|
|
|
async thumbUrl(photo) {
|
|
return this.cached.get(`thumb:${photo.id}`) ?? null;
|
|
}
|
|
|
|
async prefetchThumb(photo) {
|
|
this.calls.push(['thumb', photo.id]);
|
|
const url = `blob:thumb:${photo.id}`;
|
|
this.cached.set(`thumb:${photo.id}`, url);
|
|
return url;
|
|
}
|
|
}
|
|
|
|
function flush() {
|
|
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
}
|
|
|
|
function photoByTitle(title) {
|
|
return photoEvents.find((event) =>
|
|
event.tags.some((tag) => tag[0] === 'title' && tag[1] === title)
|
|
);
|
|
}
|
|
|
|
module('Unit | Service | photos', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
function setup(context) {
|
|
const storage = new FakeStorage();
|
|
const nostr = new FakeNostr();
|
|
const photoCache = new FakePhotoCache();
|
|
|
|
context.owner.register('service:storage', storage, {
|
|
instantiate: false,
|
|
});
|
|
context.owner.register('service:nostr', nostr, { instantiate: false });
|
|
context.owner.register('service:photo-cache', photoCache, {
|
|
instantiate: false,
|
|
});
|
|
|
|
return {
|
|
photos: context.owner.lookup('service:photos'),
|
|
storage,
|
|
nostr,
|
|
photoCache,
|
|
};
|
|
}
|
|
|
|
test('defaults to Nature enabled and no feed loaded yet', async function (assert) {
|
|
const { photos } = setup(this);
|
|
|
|
await photos.loadCached();
|
|
|
|
assert.deepEqual(photos.enabledFeedIds, ['dt:nature']);
|
|
assert.deepEqual(photos.feeds, []);
|
|
assert.deepEqual(photos.photos, []);
|
|
assert.strictEqual(photos.currentPhoto, null);
|
|
assert.strictEqual(photos.rotation, 'weekly');
|
|
assert.strictEqual(typeof photos.rotationPeriodIndex, 'number');
|
|
assert.deepEqual(photos.rotationQueue, []);
|
|
assert.true(photos.initialized);
|
|
});
|
|
|
|
test('refreshes enabled feeds and selects the first Nature photo', async function (assert) {
|
|
const { photos, nostr } = setup(this);
|
|
|
|
await photos.refresh();
|
|
|
|
assert.strictEqual(photos.feeds.length, 2);
|
|
assert.strictEqual(photos.feeds[0].id, 'dt:nature');
|
|
assert.strictEqual(photos.feeds[1].id, 'dt:city-nights');
|
|
assert.strictEqual(photos.photos.length, 4);
|
|
assert.deepEqual(photos.enabledFeedIds, ['dt:nature']);
|
|
assert.strictEqual(
|
|
photos.currentPhoto.title,
|
|
'Winding road through autumn forest'
|
|
);
|
|
assert.strictEqual(
|
|
photos.currentWallpaper.url,
|
|
'https://blossom.kosmos.org/82d3d49b859c31152f0d779b11feaab6814c3346b550286a3faf1597b32366f1'
|
|
);
|
|
assert.notStrictEqual(
|
|
photos.currentWallpaper.url,
|
|
photos.currentWallpaper.thumbUrl
|
|
);
|
|
assert.strictEqual(photos.currentCredit.name, 'Adrian Infernus');
|
|
assert.strictEqual(
|
|
photos.currentCredit.url,
|
|
'https://unsplash.com/@adrian_infernus'
|
|
);
|
|
assert.deepEqual(nostr.calls[0], 'feeds');
|
|
});
|
|
|
|
test('prefetches all thumbs and the first three originals', async function (assert) {
|
|
const { photos, photoCache } = setup(this);
|
|
|
|
await photos.refresh();
|
|
photoCache.calls = [];
|
|
await photos.prefetchAssets();
|
|
|
|
const originals = photoCache.calls.filter(([type]) => type === 'original');
|
|
const thumbs = photoCache.calls.filter(([type]) => type === 'thumb');
|
|
|
|
assert.strictEqual(thumbs.length, 4);
|
|
assert.strictEqual(originals.length, 3);
|
|
assert.deepEqual(
|
|
originals.map(([, id]) => id),
|
|
[
|
|
photoByTitle('Winding road through autumn forest').id,
|
|
photoByTitle('Awakening trees').id,
|
|
photoByTitle('Mountain surrounded by water').id,
|
|
]
|
|
);
|
|
});
|
|
|
|
test('instantly paints from cached events and an object URL', async function (assert) {
|
|
const { photos, storage, photoCache } = setup(this);
|
|
const adrian = photoByTitle('Winding road through autumn forest');
|
|
|
|
for (const event of feedEvents) {
|
|
await storage.set('photos', `feed:${event.id}`, event);
|
|
}
|
|
for (const event of photoEvents) {
|
|
await storage.set('photos', `photo:${event.id}`, event);
|
|
}
|
|
await storage.set('preferences', 'photos.config', {
|
|
enabledFeedIds: ['dt:nature'],
|
|
currentPhotoId: adrian.id,
|
|
rotation: 'off',
|
|
});
|
|
photoCache.cached.set(adrian.id, 'blob:cached:adrian');
|
|
|
|
await photos.loadCached();
|
|
|
|
assert.strictEqual(photos.feeds.length, 2);
|
|
assert.strictEqual(photos.photos.length, 5);
|
|
assert.strictEqual(photos.currentPhoto.id, adrian.id);
|
|
assert.strictEqual(photos.displayUrl, 'blob:cached:adrian');
|
|
assert.strictEqual(
|
|
photos.currentWallpaper.displayUrl,
|
|
'blob:cached:adrian'
|
|
);
|
|
});
|
|
|
|
test('filters un-attributed photos out of the feed grid', async function (assert) {
|
|
const { photos, nostr } = setup(this);
|
|
const adrian = structuredClone(
|
|
photoByTitle('Winding road through autumn forest')
|
|
);
|
|
adrian.tags = adrian.tags.filter(
|
|
(tag) => tag[0] !== 'origin' && tag[0] !== 'r'
|
|
);
|
|
nostr.photoEvents = [
|
|
adrian,
|
|
...photoEvents.filter((event) => event.id !== adrian.id),
|
|
];
|
|
|
|
await photos.refresh();
|
|
|
|
assert.strictEqual(photos.photosForFeed('dt:nature').length, 3);
|
|
assert.notOk(
|
|
photos
|
|
.photosForFeed('dt:nature')
|
|
.some((photo) => photo.title === 'Winding road through autumn forest')
|
|
);
|
|
});
|
|
|
|
test('enabling City Nights fetches its photos and persists the choice', async function (assert) {
|
|
const { photos, storage } = setup(this);
|
|
|
|
await photos.refresh();
|
|
await photos.setFeedEnabled('dt:city-nights', true);
|
|
|
|
assert.deepEqual(photos.enabledFeedIds.sort(), [
|
|
'dt:city-nights',
|
|
'dt:nature',
|
|
]);
|
|
assert.strictEqual(photos.photos.length, 5);
|
|
assert.strictEqual(photos.photosForFeed('dt:city-nights').length, 1);
|
|
|
|
const config = await storage.get('preferences', 'photos.config');
|
|
assert.deepEqual(config.enabledFeedIds.sort(), [
|
|
'dt:city-nights',
|
|
'dt:nature',
|
|
]);
|
|
});
|
|
|
|
test('keeps cached feeds when the relay fails', async function (assert) {
|
|
const { photos, storage, nostr } = setup(this);
|
|
|
|
for (const event of feedEvents) {
|
|
await storage.set('photos', `feed:${event.id}`, event);
|
|
}
|
|
for (const event of photoEvents) {
|
|
await storage.set('photos', `photo:${event.id}`, event);
|
|
}
|
|
nostr.queryFeedEvents = async () => {
|
|
throw new Error('relay down');
|
|
};
|
|
|
|
await photos.refresh();
|
|
|
|
assert.strictEqual(photos.error, 'relay down');
|
|
assert.strictEqual(photos.feeds.length, 2);
|
|
assert.strictEqual(photos.photos.length, 5);
|
|
});
|
|
|
|
test('prefetchForWelcome loads and refreshes in the background', async function (assert) {
|
|
const { photos, nostr } = setup(this);
|
|
|
|
await photos.prefetchForWelcome();
|
|
|
|
assert.deepEqual(nostr.calls[0], 'feeds');
|
|
assert.strictEqual(photos.feeds.length, 2);
|
|
assert.strictEqual(photos.photos.length, 4);
|
|
});
|
|
|
|
test('selecting a photo updates the credit and persists it', async function (assert) {
|
|
const { photos, storage } = setup(this);
|
|
const alexander = photoByTitle('Train at night');
|
|
|
|
await photos.setFeedEnabled('dt:city-nights', true);
|
|
await photos.setCurrentPhoto(alexander.id);
|
|
|
|
assert.strictEqual(photos.currentCredit.name, 'Aleksandr Popov');
|
|
assert.strictEqual(photos.currentCredit.url, 'https://unsplash.com/@5tep5');
|
|
|
|
const config = await storage.get('preferences', 'photos.config');
|
|
assert.strictEqual(config.currentPhotoId, alexander.id);
|
|
});
|
|
|
|
test('migrates a legacy off config to weekly', async function (assert) {
|
|
const { photos, storage } = setup(this);
|
|
await storage.set('preferences', 'photos.config', {
|
|
enabledFeedIds: ['dt:nature'],
|
|
currentPhotoId: null,
|
|
rotation: 'off',
|
|
});
|
|
|
|
await photos.loadCached();
|
|
|
|
assert.strictEqual(photos.rotation, 'weekly');
|
|
assert.strictEqual(typeof photos.rotationPeriodIndex, 'number');
|
|
|
|
const config = await storage.get('preferences', 'photos.config');
|
|
assert.strictEqual(config.rotation, 'weekly');
|
|
assert.strictEqual(config.rotationVersion, 2);
|
|
});
|
|
|
|
test('honors an explicit off mode in a versioned config', async function (assert) {
|
|
const { photos, storage } = setup(this);
|
|
await storage.set('preferences', 'photos.config', {
|
|
enabledFeedIds: ['dt:nature'],
|
|
currentPhotoId: null,
|
|
rotation: 'off',
|
|
rotationVersion: 2,
|
|
rotationPeriodIndex: 42,
|
|
rotationQueue: ['stale'],
|
|
});
|
|
|
|
await photos.loadCached();
|
|
|
|
assert.strictEqual(photos.rotation, 'off');
|
|
assert.strictEqual(photos.rotationPeriodIndex, null);
|
|
assert.deepEqual(photos.rotationQueue, []);
|
|
});
|
|
|
|
test('rotates daily when the local day changes', async function (assert) {
|
|
const { photos } = setup(this);
|
|
const now = Date.now();
|
|
const nextId = photoByTitle('Awakening trees').id;
|
|
|
|
await photos.refresh();
|
|
photos.rotation = 'daily';
|
|
photos.rotationPeriodIndex = rotationIndex(now, 'daily') - 1;
|
|
photos.rotationQueue = [nextId];
|
|
photos.random = () => 0;
|
|
|
|
const changed = await photos.rotateIfDue(now);
|
|
|
|
assert.true(changed);
|
|
assert.strictEqual(photos.currentPhotoId, nextId);
|
|
assert.strictEqual(photos.rotationPeriodIndex, rotationIndex(now, 'daily'));
|
|
assert.notOk(photos.rotationQueue.includes(nextId));
|
|
});
|
|
|
|
test('rotates weekly when the UTC week changes', async function (assert) {
|
|
const { photos } = setup(this);
|
|
const now = Date.now();
|
|
const nextId = photoByTitle('Rolling sand dunes').id;
|
|
|
|
await photos.refresh();
|
|
photos.rotation = 'weekly';
|
|
photos.rotationPeriodIndex = rotationIndex(now, 'weekly') - 1;
|
|
photos.rotationQueue = [nextId];
|
|
photos.random = () => 0;
|
|
|
|
const changed = await photos.rotateIfDue(now);
|
|
|
|
assert.true(changed);
|
|
assert.strictEqual(photos.currentPhotoId, nextId);
|
|
assert.strictEqual(
|
|
photos.rotationPeriodIndex,
|
|
rotationIndex(now, 'weekly')
|
|
);
|
|
});
|
|
|
|
test('does not rotate twice in the same period', async function (assert) {
|
|
const { photos } = setup(this);
|
|
const now = Date.now();
|
|
|
|
await photos.refresh();
|
|
const currentId = photos.currentPhoto.id;
|
|
photos.rotation = 'daily';
|
|
photos.rotationPeriodIndex = rotationIndex(now, 'daily');
|
|
photos.currentPhotoId = currentId;
|
|
photos.rotationQueue = [photoByTitle('Awakening trees').id];
|
|
|
|
const changed = await photos.rotateIfDue(now);
|
|
|
|
assert.false(changed);
|
|
assert.strictEqual(photos.currentPhotoId, currentId);
|
|
assert.deepEqual(photos.rotationQueue, [
|
|
photoByTitle('Awakening trees').id,
|
|
]);
|
|
});
|
|
|
|
test('refills the queue without repeating the current photo', async function (assert) {
|
|
const { photos } = setup(this);
|
|
const now = Date.now();
|
|
|
|
await photos.refresh();
|
|
const currentId = photos.currentPhoto.id;
|
|
photos.rotation = 'daily';
|
|
photos.rotationPeriodIndex = rotationIndex(now, 'daily') - 1;
|
|
photos.currentPhotoId = currentId;
|
|
photos.rotationQueue = [];
|
|
photos.random = () => 0;
|
|
|
|
const changed = await photos.rotateIfDue(now);
|
|
|
|
assert.true(changed);
|
|
assert.notStrictEqual(photos.currentPhotoId, currentId);
|
|
assert.true(
|
|
photos.rotationPool
|
|
.map((photo) => photo.id)
|
|
.includes(photos.currentPhotoId)
|
|
);
|
|
});
|
|
|
|
test('keeps a single-photo pool', async function (assert) {
|
|
const { photos } = setup(this);
|
|
const now = Date.now();
|
|
const adrian = photoByTitle('Winding road through autumn forest');
|
|
|
|
await photos.refresh();
|
|
photos.photos = photos.photos.filter((photo) => photo.id === adrian.id);
|
|
photos.rotation = 'daily';
|
|
photos.rotationPeriodIndex = rotationIndex(now, 'daily') - 1;
|
|
photos.currentPhotoId = adrian.id;
|
|
photos.rotationQueue = [];
|
|
photos.random = () => 0;
|
|
|
|
const changed = await photos.rotateIfDue(now);
|
|
|
|
assert.false(changed);
|
|
assert.strictEqual(photos.currentPhotoId, adrian.id);
|
|
});
|
|
|
|
test('switching modes keeps the current photo and resets rotation state', async function (assert) {
|
|
const { photos } = setup(this);
|
|
|
|
await photos.refresh();
|
|
const currentId = photos.currentPhoto.id;
|
|
|
|
await photos.setRotation('daily');
|
|
|
|
assert.strictEqual(photos.rotation, 'daily');
|
|
assert.strictEqual(typeof photos.rotationPeriodIndex, 'number');
|
|
assert.strictEqual(photos.currentPhoto.id, currentId);
|
|
|
|
await photos.setRotation('off');
|
|
|
|
assert.strictEqual(photos.rotation, 'off');
|
|
assert.strictEqual(photos.rotationPeriodIndex, null);
|
|
assert.deepEqual(photos.rotationQueue, []);
|
|
});
|
|
|
|
test('manual selection removes the photo from the rotation queue', async function (assert) {
|
|
const { photos } = setup(this);
|
|
const selectedId = photoByTitle('Awakening trees').id;
|
|
|
|
await photos.refresh();
|
|
photos.rotation = 'weekly';
|
|
photos.rotationQueue = [selectedId, photoByTitle('Rolling sand dunes').id];
|
|
|
|
await photos.setCurrentPhoto(selectedId);
|
|
|
|
assert.strictEqual(photos.currentPhotoId, selectedId);
|
|
assert.notOk(photos.rotationQueue.includes(selectedId));
|
|
});
|
|
|
|
test('new feed photos enter the rotation queue', async function (assert) {
|
|
const { photos } = setup(this);
|
|
const cityId = photoByTitle('Train at night').id;
|
|
|
|
await photos.refresh();
|
|
photos.currentPhotoId = photoByTitle(
|
|
'Winding road through autumn forest'
|
|
).id;
|
|
photos.syncRotationPool();
|
|
|
|
assert.notOk(photos.rotationQueue.includes(cityId));
|
|
|
|
await photos.setFeedEnabled('dt:city-nights', true);
|
|
|
|
assert.ok(photos.rotationQueue.includes(cityId));
|
|
});
|
|
|
|
test('keeps the previous wallpaper while an uncached photo resolves', async function (assert) {
|
|
const { photos, photoCache } = setup(this);
|
|
|
|
await photos.refresh({ prefetch: false });
|
|
const previous = photos.displayUrl;
|
|
assert.ok(previous, 'a wallpaper is displayed');
|
|
|
|
const next = photos.photos.find(
|
|
(photo) => !photoCache.cached.has(photo.id)
|
|
);
|
|
assert.ok(next, 'there is an uncached photo');
|
|
|
|
const gate = photoCache.defer(next.id);
|
|
photos.currentPhotoId = next.id;
|
|
|
|
const pending = photos.resolveCurrentWallpaper();
|
|
await flush();
|
|
|
|
assert.strictEqual(
|
|
photos.displayUrl,
|
|
previous,
|
|
'the previous wallpaper stays while the new one is fetched'
|
|
);
|
|
|
|
gate.resolve(`blob:original:${next.id}`);
|
|
await pending;
|
|
|
|
assert.strictEqual(photos.displayUrl, `blob:original:${next.id}`);
|
|
});
|
|
|
|
test('keeps the last wallpaper when a fetch fails', async function (assert) {
|
|
const { photos, photoCache } = setup(this);
|
|
|
|
await photos.refresh({ prefetch: false });
|
|
const previous = photos.displayUrl;
|
|
|
|
const next = photos.photos.find(
|
|
(photo) => !photoCache.cached.has(photo.id)
|
|
);
|
|
photoCache.fail(next.id);
|
|
photos.currentPhotoId = next.id;
|
|
|
|
const originalError = console.error;
|
|
console.error = () => {};
|
|
try {
|
|
await photos.resolveCurrentWallpaper();
|
|
} finally {
|
|
console.error = originalError;
|
|
}
|
|
|
|
assert.strictEqual(photos.displayUrl, previous);
|
|
});
|
|
|
|
test('a scheduled rotation switches the wallpaper', async function (assert) {
|
|
const { photos } = setup(this);
|
|
const now = Date.now();
|
|
const nextId = photoByTitle('Awakening trees').id;
|
|
|
|
await photos.refresh();
|
|
photos.rotation = 'daily';
|
|
photos.rotationPeriodIndex = rotationIndex(now, 'daily') - 1;
|
|
photos.rotationQueue = [nextId];
|
|
photos.random = () => 0;
|
|
let rearmed = 0;
|
|
photos.scheduleRotation = () => {
|
|
rearmed += 1;
|
|
};
|
|
|
|
await photos.runScheduledRotation(now);
|
|
|
|
assert.strictEqual(photos.currentPhotoId, nextId);
|
|
assert.strictEqual(photos.displayUrl, `blob:original:${nextId}`);
|
|
assert.strictEqual(rearmed, 1, 're-arms the timer');
|
|
});
|
|
|
|
test('a scheduled rotation with nothing due leaves the wallpaper', async function (assert) {
|
|
const { photos, photoCache } = setup(this);
|
|
const now = Date.now();
|
|
|
|
await photos.refresh({ prefetch: false });
|
|
const currentId = photos.currentPhoto.id;
|
|
const url = photos.displayUrl;
|
|
const calls = photoCache.calls.length;
|
|
photos.scheduleRotation = () => {};
|
|
|
|
await photos.runScheduledRotation(now);
|
|
|
|
assert.strictEqual(photos.currentPhoto.id, currentId);
|
|
assert.strictEqual(photos.displayUrl, url);
|
|
assert.strictEqual(
|
|
photoCache.calls.length,
|
|
calls,
|
|
'does not resolve the wallpaper when nothing is due'
|
|
);
|
|
});
|
|
});
|