import { describe, expect, it } from 'vitest'; import { normalizeEdits, withoutLensVignetting } from './edits'; import { defaultForMeta, type CameraLensDefault } from './lensDefaults'; import type { LensProfile, PhotoRecord } from '../types'; const profile = (model: string): LensProfile => ({ maker: 'Nikon', model, crop: 1, corrections: { distortion: true, tca: true, vignetting: true }, }); const savedDefault = ( camera: string, exifLens: string, model = 'Nikkor AF 50mm f/1.8D', ): CameraLensDefault => ({ camera, exifLens, profile: profile(model), updatedAt: Date.now(), }); describe('camera-lens default selection', () => { it('matches a camera and lens after normalizing both names', () => { const defaults = [ savedDefault('NIKON CORPORATION NIKON D700', 'NIKON AF NIKKOR 50MM F/1.8D'), ]; expect( defaultForMeta(defaults, { camera: 'NIKON D700', lens: 'Nikkor AF 50mm 1:1.8D', }), ).toBe(defaults[0]); }); it('does not apply a default to a different lens on the same body', () => { const defaults = [ savedDefault('NIKON CORPORATION NIKON D700', 'NIKON AF NIKKOR 50MM F/1.8D'), ]; expect( defaultForMeta(defaults, { camera: 'NIKON CORPORATION NIKON D700', lens: 'NIKON AF NIKKOR 85MM F/1.8G', }), ).toBeNull(); }); it('requires both camera and lens metadata', () => { const defaults = [ savedDefault('NIKON CORPORATION NIKON D700', 'NIKON AF NIKKOR 50MM F/1.8D'), ]; expect(defaultForMeta(defaults, { camera: 'NIKON D700', lens: undefined })).toBeNull(); expect(defaultForMeta(defaults, { camera: undefined, lens: 'NIKKOR 50mm f/1.8D' })).toBeNull(); }); it('uses the first row when more than one entry has the same key', () => { const older = { ...savedDefault('NIKON D700', 'NIKKOR 50mm f/1.8D'), updatedAt: 1 }; const newer = { ...savedDefault('NIKON D700', 'NIKON AF NIKKOR 50MM F/1.8D'), updatedAt: 2 }; expect( defaultForMeta([newer, older], { camera: 'NIKON CORPORATION NIKON D700', lens: 'Nikkor AF 50mm f/1.8D', }), ).toBe(newer); }); it('leaves the vignetting correction off unless the profile asked for it', () => { // Older saved profiles carry no corrections block at all; the flat-field // gain would brighten the corners past the frame centre if it defaulted on. const legacy = normalizeEdits({ lens: { maker: 'Nikon', model: 'Nikkor 50mm', crop: 1 } } as never); expect(legacy.lens?.corrections).toEqual({ distortion: true, tca: true, vignetting: false }); const optedIn = normalizeEdits({ lens: { maker: 'Nikon', model: 'Nikkor 50mm', crop: 1, corrections: { distortion: true, tca: true, vignetting: true }, }, } as never); expect(optedIn.lens?.corrections.vignetting).toBe(true); }); it('clears a stored vignetting flag once, in edits, snapshots and history', () => { const profile: LensProfile = { maker: 'Canon', model: 'EF 50mm', crop: 1, corrections: { distortion: true, tca: true, vignetting: true }, }; const photo = { id: 'p1', edits: { lens: profile }, snapshots: [{ id: 's1', name: 'Before', ts: 1, edits: { lens: profile } }], history: [ { label: 'Import', ts: 1, edits: { lens: profile } }, { label: 'Exposure', ts: 2, edits: {} }, ], } as unknown as PhotoRecord; const cleared = withoutLensVignetting(photo); expect(cleared?.edits.lens?.corrections.vignetting).toBe(false); expect(cleared?.snapshots?.[0].edits.lens?.corrections.vignetting).toBe(false); expect(cleared?.history?.[0].edits.lens?.corrections.vignetting).toBe(false); // Geometry and CA stay on: only the corner gain was the problem. expect(cleared?.edits.lens?.corrections.distortion).toBe(true); // Nothing left to do the second time, so the upgrade is idempotent. expect(withoutLensVignetting(cleared as PhotoRecord)).toBeNull(); }); });