import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { getShareByKey, utcBucketKey } from '../src/immich' import { KeyType } from '../src/types' // 3.0-shaped responses: album `/shared-links/me` returns an empty assets[], // and the album's assets are enumerated from the timeline API. This exercises // the regression fix end-to-end through getShareByKey -> fetchShareByKey. interface MockResponse { ok: boolean status: number headers: { get: (h: string) => string | null } json: () => Promise } function jsonResponse (body: unknown, status = 200): MockResponse { return { ok: status >= 200 && status < 300, status, headers: { get: (h: string) => (h.toLowerCase() === 'content-type' ? 'application/json' : null) }, json: async () => body } } const ALBUM_ID = '0aab733a-3cb4-416f-b4b9-f94906533aaa' const sharedLinkResponse = (order?: string) => ({ type: 'ALBUM', assets: [], allowDownload: true, expiresAt: null, showMetadata: true, key: 'real-key', album: { id: ALBUM_ID, albumName: 'Test album', albumThumbnailAssetId: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', order } }) const MOTION_CLIP_ID = 'dddddddd-dddd-dddd-dddd-dddddddddddd' const bucketsResponse = [{ timeBucket: '2026-06-01', count: 3 }] // Columnar (struct-of-arrays). Index 1 is trashed and must be filtered out. const bucketResponse = { id: [ 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'cccccccc-cccc-cccc-cccc-cccccccccccc' ], isImage: [true, true, false], ratio: [1.5, 0.5, 1], thumbhash: ['hashA', null, 'hashC'], isTrashed: [false, true, false], fileCreatedAt: ['2026-06-03T00:00:00.000Z', '2026-06-02T00:00:00.000Z', '2026-06-01T00:00:00.000Z'], localOffsetHours: [5.5, -8, 0], // Index 0 is a motion photo; the others are ordinary assets. livePhotoVideoId: [MOTION_CLIP_ID, null, null] } function routeFetch (sharedLink: unknown) { return vi.fn(async (url: string) => { if (url.includes('/shared-links/me')) return jsonResponse(sharedLink) if (url.includes('/timeline/buckets')) return jsonResponse(bucketsResponse) if (url.includes('/timeline/bucket')) return jsonResponse(bucketResponse) throw new Error('Unexpected fetch to ' + url) }) } let keyCounter = 0 function uniqueKey () { // getShareByKey caches by key; a fresh key per test avoids cross-test reuse. return 'album-key-' + (keyCounter++) } describe('album timeline enumeration (Immich 3.0)', () => { beforeEach(() => { process.env.IMMICH_URL = 'http://immich.test' }) afterEach(() => { vi.unstubAllGlobals() }) it('populates grid assets from the timeline and filters trashed', async () => { vi.stubGlobal('fetch', routeFetch(sharedLinkResponse())) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) expect(result.valid).toBe(true) const assets = result.link!.assets // 3 assets, 1 trashed -> 2 survive expect(assets.map(a => a.id)).toEqual([ 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'cccccccc-cccc-cccc-cccc-cccccccccccc' ]) // grid assets are flagged for lazy detail loading expect(assets.every(a => a.needsDetail)).toBe(true) // type derived from isImage expect(assets[0].type).toBe('IMAGE') expect(assets[1].type).toBe('VIDEO') // thumbhash carried through; null becomes undefined expect(assets[0].thumbhash).toBe('hashA') // dimensions derived from ratio (landscape 1.5 -> wider than tall) expect(assets[0].width!).toBeGreaterThan(assets[0].height!) }) it('requests each bucket with an explicit UTC key so non-UTC databases match (#260)', async () => { const fetchMock = routeFetch(sharedLinkResponse()) vi.stubGlobal('fetch', fetchMock) await getShareByKey(uniqueKey(), undefined, KeyType.key) const bucketCalls = fetchMock.mock.calls.map(c => String(c[0])).filter(u => u.includes('/timeline/bucket?')) expect(bucketCalls).toHaveLength(1) expect(new URL(bucketCalls[0]).searchParams.get('timeBucket')).toBe('2026-06-01T00:00:00.000Z') }) it('utcBucketKey pads bare dates and leaves anything else alone', () => { expect(utcBucketKey('2026-06-01')).toBe('2026-06-01T00:00:00.000Z') expect(utcBucketKey('2026-06-01T00:00:00.000Z')).toBe('2026-06-01T00:00:00.000Z') expect(utcBucketKey('')).toBe('') }) it('carries the motion photo clip id through from the bucket', async () => { // Album shares get livePhotoVideoId for free from the timeline response, // so motion photos cost no extra upstream requests here. vi.stubGlobal('fetch', routeFetch(sharedLinkResponse())) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) const assets = result.link!.assets expect(assets[0].livePhotoVideoId).toBe(MOTION_CLIP_ID) // null in the columnar response becomes undefined, as with thumbhash expect(assets[1].livePhotoVideoId).toBeUndefined() }) it('synthesises localDateTime from fileCreatedAt + localOffsetHours', async () => { vi.stubGlobal('fetch', routeFetch(sharedLinkResponse())) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) const assets = result.link!.assets // aaa: 2026-06-03T00:00Z + 5.5h -> 05:30 local expect(assets[0].localDateTime).toBe('2026-06-03T05:30:00.000Z') // ccc: 2026-06-01T00:00Z + 0h -> unchanged expect(assets[1].localDateTime).toBe('2026-06-01T00:00:00.000Z') }) it('stamps the share key/keyType onto every asset', async () => { vi.stubGlobal('fetch', routeFetch(sharedLinkResponse())) const key = uniqueKey() const result = await getShareByKey(key, undefined, KeyType.key) expect(result.link!.assets.every(a => a.key === key && a.keyType === KeyType.key)).toBe(true) }) it('respects album sort order asc', async () => { vi.stubGlobal('fetch', routeFetch(sharedLinkResponse('asc'))) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) const dates = result.link!.assets.map(a => a.fileCreatedAt) expect(dates).toEqual([...dates].sort()) }) it('respects album sort order desc', async () => { vi.stubGlobal('fetch', routeFetch(sharedLinkResponse('desc'))) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) const dates = result.link!.assets.map(a => a.fileCreatedAt) expect(dates).toEqual([...dates].sort().reverse()) }) it('keeps the album cover id from /shared-links/me (no extra album fetch)', async () => { vi.stubGlobal('fetch', routeFetch(sharedLinkResponse())) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) expect(result.link!.album!.albumThumbnailAssetId).toBe('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') }) it('is invalid when the album share has no album id', async () => { const noId = { ...sharedLinkResponse(), album: undefined } vi.stubGlobal('fetch', routeFetch(noId)) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) expect(result.valid).toBe(false) }) it('treats a share with no assets array as empty', async () => { // Defensive guard (see #267): if Immich returns a link whose `assets` // isn't an array, `.filter` of undefined would reject and, unhandled, // exit the whole process. Treat it as an empty share instead. const noAssets = { type: 'INDIVIDUAL', key: 'real-key', allowDownload: true } vi.stubGlobal('fetch', vi.fn(async (url: string) => { if (url.includes('/shared-links/me')) return jsonResponse(noAssets) throw new Error('Unexpected fetch to ' + url) })) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) expect(result.valid).toBe(true) expect(result.link!.assets).toEqual([]) }) it('is invalid (not cached empty) when timeline enumeration fails upstream', async () => { vi.stubGlobal('fetch', vi.fn(async (url: string) => { if (url.includes('/shared-links/me')) return jsonResponse(sharedLinkResponse()) if (url.includes('/timeline/buckets')) return jsonResponse({ message: 'boom' }, 500) throw new Error('Unexpected fetch to ' + url) })) const result = await getShareByKey(uniqueKey(), undefined, KeyType.key) expect(result.valid).toBe(false) }) })