Require Immich 3.x; code cleanup

This commit is contained in:
Alan Grainger
2026-10-06 09:23:57 +13:00
parent d03a77f691
commit 255260ccb3
33 changed files with 347 additions and 800 deletions
+51
View File
@@ -105,6 +105,57 @@ describe('album timeline enumeration (Immich 3.0)', () => {
expect(assets[0].width!).toBeGreaterThan(assets[0].height!)
})
it('withholds assets Immich has not finished generating thumbnails for', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const pending = {
...bucketResponse,
id: bucketResponse.id.map(id => id.replace(/^a/, 'e')),
thumbhash: [null, null, 'hashC'],
isTrashed: [false, true, false]
}
vi.stubGlobal('fetch', vi.fn(async (url: string) => {
if (url.includes('/shared-links/me')) return jsonResponse(sharedLinkResponse())
if (url.includes('/timeline/buckets')) return jsonResponse(bucketsResponse)
if (url.includes('/timeline/bucket')) return jsonResponse(pending)
throw new Error('Unexpected fetch to ' + url)
}))
const first = await getShareByKey(uniqueKey(), undefined, KeyType.key)
// A second share key stands in for the next cache refresh.
await getShareByKey(uniqueKey(), undefined, KeyType.key)
expect(first.valid).toBe(true)
expect(first.link!.assets.map(a => a.id)).toEqual(['cccccccc-cccc-cccc-cccc-cccccccccccc'])
const warnings = warn.mock.calls.filter(c => String(c[0]).includes('eaaaaaaa-'))
expect(warnings).toHaveLength(1)
warn.mockRestore()
})
it('withholds unprocessed assets on an individual share too', async () => {
// Individual shares carry their assets inline on /shared-links/me rather
// than via the timeline, so the filter has to sit downstream of both.
vi.spyOn(console, 'warn').mockImplementation(() => {})
const individual = {
type: 'INDIVIDUAL',
key: 'real-key',
allowDownload: true,
expiresAt: null,
showMetadata: true,
assets: [
{ id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', type: 'IMAGE', isTrashed: false, thumbhash: 'hashA' },
{ id: 'ffffffff-ffff-ffff-ffff-ffffffffffff', type: 'IMAGE', isTrashed: false, thumbhash: null }
]
}
vi.stubGlobal('fetch', vi.fn(async (url: string) => {
if (url.includes('/shared-links/me')) return jsonResponse(individual)
throw new Error('Unexpected fetch to ' + url)
}))
const result = await getShareByKey(uniqueKey(), undefined, KeyType.key)
expect(result.valid).toBe(true)
expect(result.link!.assets.map(a => a.id)).toEqual(['aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'])
vi.mocked(console.warn).mockRestore()
})
it('requests each bucket with an explicit UTC key so non-UTC databases match (#260)', async () => {
const fetchMock = routeFetch(sharedLinkResponse())
vi.stubGlobal('fetch', fetchMock)
+11 -24
View File
@@ -1,6 +1,7 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { Asset, AssetType, KeyType, loadConfig, SharedLink } from '@ipp/core'
import { buildAssetMetadata } from '../src/gallery/metadata'
import { removedMetadataSwitches } from '../src/gallery/exif'
// buildAssetMetadata is the lazy-flow counterpart to the gallery builder's
// per-item baking; it must apply the same showMetadata kill-switch and
@@ -56,17 +57,11 @@ describe('buildAssetMetadata', () => {
expect(meta.exif?.city).toBeUndefined()
})
// Immich 3.x applies EXIF orientation before storing asset.width / height,
// so a rotated photo reports portrait dimensions there while the exifImage*
// pair stays landscape. The asset-level pair must win.
it('reports dimensions in display orientation', () => {
it('reports the asset-level dimensions', () => {
setConfig({ ipp: { showMetadata: { exif: { dimensions: true } } } })
const photo = asset()
photo.width = 3000
photo.height = 4000
photo.exifInfo!.exifImageWidth = 4000
photo.exifInfo!.exifImageHeight = 3000
photo.exifInfo!.orientation = '6'
expect(buildAssetMetadata(photo, share()).exif).toMatchObject({
width: 3000,
@@ -74,25 +69,17 @@ describe('buildAssetMetadata', () => {
})
})
// Immich 2.x has no asset-level width / height, so the raw EXIF pair is
// used, swapped when the orientation marks the image as stored rotated.
const exifOrientationCases: Array<[string | null, number, number]> = [
['6', 3000, 4000],
['8', 3000, 4000],
['90', 3000, 4000],
['-90', 3000, 4000],
['1', 4000, 3000],
[null, 4000, 3000]
]
it('finds the removed metadata master switches, whatever their value', () => {
setConfig({ ipp: { showMetadata: { exif: { enabled: false, gps: true }, location: { enabled: true } } } })
expect(removedMetadataSwitches()).toEqual(['ipp.showMetadata.exif.enabled', 'ipp.showMetadata.location.enabled'])
it.each(exifOrientationCases)('falls back to swapped exif dimensions without asset dimensions (orientation %s)', (orientation, width, height) => {
setConfig({ ipp: { showMetadata: { exif: { make: true } } } })
expect(removedMetadataSwitches()).toEqual([])
})
it('omits dimensions when Immich has no asset-level width / height', () => {
setConfig({ ipp: { showMetadata: { exif: { dimensions: true } } } })
const photo = asset()
photo.exifInfo!.exifImageWidth = 4000
photo.exifInfo!.exifImageHeight = 3000
photo.exifInfo!.orientation = orientation
expect(buildAssetMetadata(photo, share()).exif).toMatchObject({ width, height })
expect(buildAssetMetadata(asset(), share()).exif?.width).toBeUndefined()
})
it('returns the photo timezone only when both date and timezone are enabled', () => {
+35
View File
@@ -211,4 +211,39 @@ describe('assetBuffer streaming', () => {
expect(res.headers['content-disposition']).toContain('a1.webp')
expect(res.headers['content-type']).toBe('image/webp')
})
describe('original tier on an album asset', () => {
// Album assets come from the timeline with no originalFileName or mime.
const albumAsset: Asset = { id: 'a2', key: 'testkey', keyType: KeyType.key, type: AssetType.image, isTrashed: false }
function svgFetch (headers: Record<string, string>) {
return vi.fn(async () => new globalThis.Response('<svg xmlns="http://www.w3.org/2000/svg"/>', {
status: 200,
headers: { 'content-type': 'image/svg+xml', ...headers }
}))
}
it('sends an attachment named from Immich\'s header, with nosniff', async () => {
vi.stubGlobal('fetch', svgFetch({ 'content-disposition': 'attachment; filename="drawing.svg"' }))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), albumAsset, ImageSize.original)
expect(res.headers['content-disposition']).toBe("attachment; filename*=UTF-8''drawing.svg")
expect(res.headers['x-content-type-options']).toBe('nosniff')
})
it('falls back to the asset id when Immich sends no filename', async () => {
vi.stubGlobal('fetch', svgFetch({}))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), albumAsset, ImageSize.original)
expect(res.headers['content-disposition']).toBe("attachment; filename*=UTF-8''a2.svg")
})
})
it('serves display sizes inline, still with nosniff', async () => {
vi.stubGlobal('fetch', pullFetch(CHUNK, newSource()))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.preview)
expect(res.headers['content-disposition']).toBeUndefined()
expect(res.headers['x-content-type-options']).toBe('nosniff')
})
})
+47
View File
@@ -0,0 +1,47 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { KeyType, loadConfig, SharedLink } from '@ipp/core'
import { canDownload } from '../src/share'
// canDownload gates the download UI on ipp.allowDownload: 0 off, 1 follow the
// Immich share's toggle, 2 always. Anything else fails closed.
function setConfig (config: unknown) {
process.env.CONFIG = JSON.stringify(config)
loadConfig()
}
const share = (allowDownload: boolean): SharedLink => ({
key: 'k',
keyType: KeyType.key,
type: 'ALBUM',
assets: [],
allowDownload
})
describe('canDownload', () => {
beforeEach(() => {
delete process.env.CONFIG
loadConfig()
vi.spyOn(console, 'warn').mockImplementation(() => {})
})
it('is off by default', () => {
expect(canDownload(share(true))).toBe(false)
})
it('follows the Immich toggle with 1', () => {
setConfig({ ipp: { allowDownload: 1 } })
expect(canDownload(share(true))).toBe(true)
expect(canDownload(share(false))).toBe(false)
})
it('is always on with 2', () => {
setConfig({ ipp: { allowDownload: 2 } })
expect(canDownload(share(false))).toBe(true)
})
it.each([true, '1', '2', 3, -1])('fails closed for %j', value => {
setConfig({ ipp: { allowDownload: value } })
expect(canDownload(share(true))).toBe(false)
})
})
+1 -2
View File
@@ -1,6 +1,5 @@
import { describe, it, expect } from 'vitest'
import { filenameFromContentDisposition } from '../src/stream/download'
import { attachmentDisposition } from '../src/gallery/filename'
import { attachmentDisposition, filenameFromContentDisposition } from '../src/gallery/filename'
// Used by the zip download path to recover the real filename for album grid
// assets (which arrive without originalFileName) from the /original response.
+2
View File
@@ -66,6 +66,7 @@ class FakeRes extends Writable {
}
setHeader (name: string, value: string) { this.headers[name] = value; return this }
set (name: string, value: string) { return this.setHeader(name, value) }
status (code: number) { this.statusCode = code; return this }
send () { this.end(); return this }
@@ -245,6 +246,7 @@ describe('downloadAssets', () => {
expect(res.writableEnded).toBe(true)
expect(res.received).toBe(0)
expect(res.headers['Content-Type']).toBeUndefined()
expect(res.headers['Cache-Control']).toContain('no-store')
}, 10_000)
it('destroys the response when a later asset keeps failing', async () => {
-364
View File
@@ -1,364 +0,0 @@
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
import { applyMigrations } from '../src/config/migrations'
/*
The migrations module mutates the passed-in config object in place and
logs a one-line deprecation notice when it makes a change. These tests
cover all four shims registered in `SHIMS`. Console output is silenced
per-test so the suite stays quiet and so we can assert on it where the
deprecation message is part of the contract.
*/
let logSpy: ReturnType<typeof vi.spyOn>
beforeEach(() => {
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
logSpy.mockRestore()
})
describe('applyMigrations', () => {
it('is a no-op for a fresh config that has no legacy keys', () => {
const config = {
ipp: {
showMetadata: {
exif: { dateTimeOriginal: true },
location: { city: true }
},
gallery: { showTitle: true },
lightbox: { showArrows: true }
}
}
const before = JSON.stringify(config)
applyMigrations(config)
expect(JSON.stringify(config)).toBe(before)
expect(logSpy).not.toHaveBeenCalled()
})
it('is a no-op for a completely empty config', () => {
const config = {}
applyMigrations(config)
// applyMigrations seeds an `ipp` object on its way through, so empty in
// means `{ ipp: {} }` out - that's fine, just check no crash + no logs.
expect(logSpy).not.toHaveBeenCalled()
})
})
describe('lightGallery shim', () => {
it('maps lightGallery.controls / download / mobileSettings to ipp.lightbox.*', () => {
const config = {
lightGallery: {
controls: false,
download: false,
mobileSettings: { controls: true }
}
} as Record<string, unknown>
applyMigrations(config)
const ipp = config.ipp as Record<string, unknown>
const lightbox = ipp.lightbox as Record<string, unknown>
expect(lightbox.showArrows).toBe(false)
expect(lightbox.showDownload).toBe(false)
expect(lightbox.mobileArrows).toBe(true)
expect(logSpy).toHaveBeenCalledOnce()
})
it('does not overwrite values the user has already set on the new path', () => {
const config = {
lightGallery: { controls: false, download: false },
ipp: {
lightbox: { showArrows: true, showDownload: true }
}
} as Record<string, unknown>
applyMigrations(config)
const lightbox = (config.ipp as Record<string, unknown>).lightbox as Record<string, unknown>
expect(lightbox.showArrows).toBe(true)
expect(lightbox.showDownload).toBe(true)
})
it('does nothing when lightGallery is absent', () => {
const config = { ipp: { lightbox: { showArrows: true } } } as Record<string, unknown>
applyMigrations(config)
expect(logSpy).not.toHaveBeenCalled()
})
})
describe('topLevelGallery shim', () => {
it('moves legacy top-level keys under ipp.gallery.*', () => {
const config = {
ipp: {
singleImageGallery: true,
showGalleryTitle: false,
groupGalleryByDate: true
}
} as Record<string, unknown>
applyMigrations(config)
const gallery = (config.ipp as Record<string, unknown>).gallery as Record<string, unknown>
expect(gallery.singleImage).toBe(true)
expect(gallery.showTitle).toBe(false)
expect(gallery.groupByDate).toBe(true)
expect(logSpy).toHaveBeenCalledOnce()
})
it('keeps the user-set value on the new path when both forms are present', () => {
const config = {
ipp: {
singleImageGallery: false,
gallery: { singleImage: true }
}
} as Record<string, unknown>
applyMigrations(config)
const gallery = (config.ipp as Record<string, unknown>).gallery as Record<string, unknown>
expect(gallery.singleImage).toBe(true)
})
it('does nothing when no legacy keys are present', () => {
const config = {
ipp: { gallery: { singleImage: true } }
} as Record<string, unknown>
applyMigrations(config)
expect(logSpy).not.toHaveBeenCalled()
})
})
describe('descriptionSplit shim', () => {
it('expands boolean true to { caption: true, sidebar: true }', () => {
const config = {
ipp: { showMetadata: { description: true } }
} as Record<string, unknown>
applyMigrations(config)
const description = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).description
expect(description).toEqual({ caption: true, sidebar: true })
expect(logSpy).toHaveBeenCalledOnce()
})
it('expands boolean false to { caption: false, sidebar: false }', () => {
const config = {
ipp: { showMetadata: { description: false } }
} as Record<string, unknown>
applyMigrations(config)
const description = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).description
expect(description).toEqual({ caption: false, sidebar: false })
})
it('leaves an existing object form alone', () => {
const config = {
ipp: {
showMetadata: {
description: { caption: false, sidebar: true }
}
}
} as Record<string, unknown>
applyMigrations(config)
const description = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).description
expect(description).toEqual({ caption: false, sidebar: true })
expect(logSpy).not.toHaveBeenCalled()
})
})
describe('metadataEnabled shim', () => {
it('legacy enabled=true defaults every unset per-field flag to true', () => {
const config = {
ipp: {
showMetadata: {
exif: { enabled: true }
}
}
} as Record<string, unknown>
applyMigrations(config)
const exif = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).exif as Record<string, unknown>
expect(exif.enabled).toBeUndefined()
expect(exif.dateTimeOriginal).toBe(true)
expect(exif.timeZone).toBeUndefined()
expect(exif.fileName).toBe(true)
expect(exif.dimensions).toBe(true)
expect(exif.fileSize).toBe(true)
expect(exif.make).toBe(true)
expect(exif.model).toBe(true)
expect(exif.lensModel).toBe(true)
expect(exif.exposureTime).toBe(true)
expect(exif.iso).toBe(true)
expect(exif.fNumber).toBe(true)
expect(exif.focalLength).toBe(true)
expect(logSpy).toHaveBeenCalledOnce()
})
it('legacy enabled=true preserves explicit per-field false ("all except X")', () => {
const config = {
ipp: {
showMetadata: {
location: { enabled: true, gps: false }
}
}
} as Record<string, unknown>
applyMigrations(config)
const location = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).location as Record<string, unknown>
expect(location.enabled).toBeUndefined()
expect(location.city).toBe(true)
expect(location.state).toBe(true)
expect(location.country).toBe(true)
expect(location.gps).toBe(false)
})
it('legacy enabled=false clears any per-field flags that were true', () => {
// This is the shipped 2.3.0 default shape: enabled=false + every per-
// field=true as documentation. Under the new semantic those `true`s
// would otherwise become live opt-ins; the shim zeroes them.
const config = {
ipp: {
showMetadata: {
exif: {
enabled: false,
dateTimeOriginal: true,
fileName: true,
make: true,
iso: true
}
}
}
} as Record<string, unknown>
applyMigrations(config)
const exif = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).exif as Record<string, unknown>
expect(exif.enabled).toBeUndefined()
expect(exif.dateTimeOriginal).toBe(false)
expect(exif.fileName).toBe(false)
expect(exif.make).toBe(false)
expect(exif.iso).toBe(false)
expect(logSpy).toHaveBeenCalledOnce()
})
it('legacy enabled=false leaves explicit per-field false alone', () => {
const config = {
ipp: {
showMetadata: {
location: { enabled: false, gps: false }
}
}
} as Record<string, unknown>
applyMigrations(config)
const location = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).location as Record<string, unknown>
expect(location.gps).toBe(false)
})
it('handles both exif and location groups in one pass', () => {
const config = {
ipp: {
showMetadata: {
exif: { enabled: true },
location: { enabled: false, gps: true }
}
}
} as Record<string, unknown>
applyMigrations(config)
const showMetadata = (config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>
const exif = showMetadata.exif as Record<string, unknown>
const location = showMetadata.location as Record<string, unknown>
expect(exif.dateTimeOriginal).toBe(true)
expect(location.gps).toBe(false)
// One deprecation log call regardless of how many groups were migrated
expect(logSpy).toHaveBeenCalledOnce()
})
it('does nothing when no legacy enabled key is present', () => {
const config = {
ipp: {
showMetadata: {
exif: { dateTimeOriginal: true }
}
}
} as Record<string, unknown>
applyMigrations(config)
const exif = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).exif as Record<string, unknown>
expect(exif.dateTimeOriginal).toBe(true)
expect(logSpy).not.toHaveBeenCalled()
})
it('does nothing when showMetadata is absent', () => {
const config = { ipp: {} } as Record<string, unknown>
applyMigrations(config)
expect(logSpy).not.toHaveBeenCalled()
})
})
describe('downloadOriginalPhoto shim', () => {
it('maps true -> maxDownloadQuality: original', () => {
const config = { ipp: { downloadOriginalPhoto: true } } as Record<string, unknown>
applyMigrations(config)
expect((config.ipp as Record<string, unknown>).maxDownloadQuality).toBe('original')
expect(logSpy).toHaveBeenCalledOnce()
})
it('maps false -> maxDownloadQuality: preview', () => {
const config = { ipp: { downloadOriginalPhoto: false } } as Record<string, unknown>
applyMigrations(config)
expect((config.ipp as Record<string, unknown>).maxDownloadQuality).toBe('preview')
})
it('does not overwrite an explicitly-set maxDownloadQuality', () => {
const config = { ipp: { downloadOriginalPhoto: false, maxDownloadQuality: 'fullsize' } } as Record<string, unknown>
applyMigrations(config)
expect((config.ipp as Record<string, unknown>).maxDownloadQuality).toBe('fullsize')
expect(logSpy).not.toHaveBeenCalled()
})
it('does nothing when downloadOriginalPhoto is absent', () => {
const config = { ipp: { maxDownloadQuality: 'original' } } as Record<string, unknown>
applyMigrations(config)
expect(logSpy).not.toHaveBeenCalled()
})
})
describe('allowDownloadAll rename shim', () => {
it('maps allowDownloadAll -> allowDownload, preserving the int value', () => {
const config = { ipp: { allowDownloadAll: 2 } } as Record<string, unknown>
applyMigrations(config)
expect((config.ipp as Record<string, unknown>).allowDownload).toBe(2)
expect(logSpy).toHaveBeenCalledOnce()
})
it('maps the falsy 0 value through (not skipped as "absent")', () => {
const config = { ipp: { allowDownloadAll: 0 } } as Record<string, unknown>
applyMigrations(config)
expect((config.ipp as Record<string, unknown>).allowDownload).toBe(0)
})
it('does not overwrite an explicitly-set allowDownload', () => {
const config = { ipp: { allowDownloadAll: 2, allowDownload: 1 } } as Record<string, unknown>
applyMigrations(config)
expect((config.ipp as Record<string, unknown>).allowDownload).toBe(1)
expect(logSpy).not.toHaveBeenCalled()
})
it('does nothing when allowDownloadAll is absent', () => {
const config = { ipp: { allowDownload: 1 } } as Record<string, unknown>
applyMigrations(config)
expect(logSpy).not.toHaveBeenCalled()
})
})