fix(journey): read GPS and the EXIF time offset from uploaded photos (#2512)

The capture backfill for local Journey uploads asked exifr for
'latitude' and 'longitude'. Those are not tags: exifr derives them from
the GPS tags, and a pick of only the derived names reads no GPS block at
all, so every uploaded photo lost its location and never showed on the
journey map. DateTimeOriginal was revived by exifr as a Date in the
server's time zone and OffsetTimeOriginal was never read, so a photo
taken at 15:52 in France was stored as 20:52 UTC on a server running in
Chicago. Impossible stamps were rolled over into real dates as well
(0000:00:00 became 1899, 30 February became 2 March).

The reader now picks the raw GPS tags and reads the stamps unrevived.
exifCaptureInstant validates the stamp and applies the first well formed
offset, preferring the tag that belongs to the stamp (OffsetTimeOriginal
for DateTimeOriginal, OffsetTimeDigitized for CreateDate). A zone written
onto the stamp itself (' UTC', 'GMT', 'Z', '+02:00') wins over the tags.
Without a usable offset it keeps the old reading in the server zone. A
stamp that is not a real date is dropped, CreateDate stands in when
DateTimeOriginal is unusable, and coordinates are kept only as a valid
pair. A GPS block of zeros, which a receiver without a fix writes, counts
as no location instead of a pin at 0,0.

Photos uploaded before this fix keep their stored time and location for
now; correcting them is left for a later release.

StorageService.withLocalFile now also removes its temp file when the
download itself fails, not only when the consumer throws.

The stamp is text any upload sets, so exifCaptureInstant turns down one
longer than 64 characters before any pattern runs, and the pattern has
no greedy tail in front of an end anchor, which cost quadratic time on a
long run of digits.

The REST uploads (entry, gallery) and the plugin journal.addEntryPhoto call
share this one reader, so both are fixed. The MCP journey tools only
attach provider photos, which never reach it.
This commit is contained in:
Maurice
2026-09-26 11:10:12 +02:00
parent 44a12e21fc
commit 626adead3f
10 changed files with 738 additions and 28 deletions
@@ -131,9 +131,10 @@ describe('JourneyPhotoCaptureService', () => {
});
const recordCaptureMetadata = vi.fn(() => true);
// Ids from 30 up are the uploaded files; their EXIF says when they were taken.
// The storage stub answers with what the EXIF reader makes of such a file.
const resolve = (id: number) =>
id >= 30 ? { id, provider: 'local', file_path: `journey/${id}.jpg` } : { id, provider: 'immich' };
const withLocalFile = vi.fn(async () => ({ DateTimeOriginal: new Date('2026-03-15T09:00:00Z') }));
const withLocalFile = vi.fn(async () => ({ takenAt: '2026-03-15T09:00:00.000Z', lat: null, lng: null }));
const backfill = new PhotoCaptureBackfillService(
{ getPhotoInfo } as unknown as PhotoResolverService,
{ resolve, recordCaptureMetadata } as unknown as TrekPhotosRepository,
+148 -1
View File
@@ -7,10 +7,11 @@
* headings in the picker mean something, so it is pinned here rather than
* inside either provider suite.
*/
import { describe, it, expect } from 'vitest';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import {
dayStartEpochSeconds,
describeFetchFailure,
exifCaptureInstant,
isWithinLocalDayRange,
shiftCalendarDay,
sortAssetsByTakenAtDesc,
@@ -181,3 +182,149 @@ describe('describeFetchFailure', () => {
expect(describeFetchFailure(new TypeError('fetch failed', { cause }))).toBe('fetch failed (self-signed certificate)');
});
});
describe('exifCaptureInstant (#2512)', () => {
// Pinned to a zone that is neither UTC nor the photographer's, because that
// is the only setup where reading the stamp in the server's zone shows.
let prevTz: string | undefined;
beforeEach(() => {
prevTz = process.env.TZ;
process.env.TZ = 'America/Chicago';
});
afterEach(() => {
if (prevTz === undefined) delete process.env.TZ;
else process.env.TZ = prevTz;
});
it('MEM-EXIF-001: applies the offset, so the stamp becomes the instant it names', () => {
expect(exifCaptureInstant('2026:05:30 15:52:19', ['+02:00'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19', ['-07:00'])).toBe('2026-05-30T22:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19', ['+05:45'])).toBe('2026-05-30T10:07:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19', ['+00:00'])).toBe('2026-05-30T15:52:19.000Z');
});
it('MEM-EXIF-002: an evening photo east of UTC stays on its own day', () => {
expect(exifCaptureInstant('2026:05:30 22:30:00', ['+02:00'])).toBe('2026-05-30T20:30:00.000Z');
});
it('MEM-EXIF-003: takes the first offset that is well formed', () => {
expect(exifCaptureInstant('2026:05:30 15:52:19', [undefined, ' : ', '+2:00', '+09:00', '+02:00']))
.toBe('2026-05-30T06:52:19.000Z');
});
it('MEM-EXIF-004: accepts the widest real zones and refuses what is past them', () => {
expect(exifCaptureInstant('2026:05:30 12:00:00', ['+14:00'])).toBe('2026-05-29T22:00:00.000Z');
expect(exifCaptureInstant('2026:05:30 12:00:00', ['-12:00'])).toBe('2026-05-31T00:00:00.000Z');
// Out of range falls through to the server zone, like no offset at all.
const local = exifCaptureInstant('2026:05:30 12:00:00', []);
expect(exifCaptureInstant('2026:05:30 12:00:00', ['+15:00'])).toBe(local);
expect(exifCaptureInstant('2026:05:30 12:00:00', ['+02:60'])).toBe(local);
});
it('MEM-EXIF-005: without an offset the stamp is read in the server zone, as before', () => {
// 15:52 CDT is 20:52 UTC.
expect(exifCaptureInstant('2026:05:30 15:52:19', [])).toBe('2026-05-30T20:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19', [null, 42, '0200', '+02:00:00', '+2', 'CEST'])).toBe('2026-05-30T20:52:19.000Z');
});
it('MEM-EXIF-006: a wall clock inside a DST gap is still a date, not garbage', () => {
// 02:30 on 8 March 2026 does not exist in Chicago; the old reader moved it an
// hour on and so does this one. What matters is that it is not dropped.
expect(exifCaptureInstant('2026:03:08 02:30:00', [])).toBe(new Date(2026, 2, 8, 2, 30, 0).toISOString());
expect(exifCaptureInstant('2026:03:08 02:30:00', ['+01:00'])).toBe('2026-03-08T01:30:00.000Z');
});
it('MEM-EXIF-007: tolerates dashes, a T and fractional seconds', () => {
expect(exifCaptureInstant('2026-05-30 15:52:19', ['+02:00'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026:05:30T15:52:19', ['+02:00'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19.123', ['+02:00'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant(' 2026:05:30 15:52:19 ', ['+02:00'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2028:02:29 08:00:00', ['+00:00'])).toBe('2028-02-29T08:00:00.000Z');
});
it('MEM-EXIF-008: a stamp that is not a real moment is null, not a rolled over date', () => {
for (const stamp of [
'0000:00:00 00:00:00', // camera without a clock
' : : : : ', // blanked as the spec allows
'2026:02:30 10:00:00',
'2026:02:29 10:00:00',
'2026:13:01 10:00:00',
'2026:05:30 24:00:00',
'2026:05:30 15:60:00',
'2026:05:30 15:52:60',
'0099:05:30 15:52:19', // Date would read this as 1999
'2026:05:30',
'2026:05:30 15:52',
'yesterday',
'',
]) {
expect(exifCaptureInstant(stamp, ['+02:00']), stamp).toBeNull();
expect(exifCaptureInstant(stamp, []), stamp).toBeNull();
}
});
it('MEM-EXIF-009: anything but a string is null', () => {
expect(exifCaptureInstant(undefined, ['+02:00'])).toBeNull();
expect(exifCaptureInstant(null, [])).toBeNull();
expect(exifCaptureInstant(new Date('2026-05-30T13:52:19Z'), [])).toBeNull();
expect(exifCaptureInstant(1780149139, [])).toBeNull();
});
it('MEM-EXIF-010: an offset tag written without the colon, or as Z or UTC, says the same thing', () => {
expect(exifCaptureInstant('2026:05:30 15:52:19', ['+0200'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19', ['-0330'])).toBe('2026-05-30T19:22:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19', ['Z'])).toBe('2026-05-30T15:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19', ['utc'])).toBe('2026-05-30T15:52:19.000Z');
});
it('MEM-EXIF-011: a zone written onto the stamp itself is honoured, and wins over the offset tags', () => {
// exifr's reviver names this form, and read it as Chicago time: 20:52:19Z.
expect(exifCaptureInstant('2026:05:30 15:52:19 UTC', [])).toBe('2026-05-30T15:52:19.000Z');
expect(exifCaptureInstant('2009-09-23 17:40:52 UTC', [])).toBe('2009-09-23T17:40:52.000Z');
// The reviver turned these into local midnight of the day.
expect(exifCaptureInstant('2026:05:30 15:52:19Z', [])).toBe('2026-05-30T15:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19+02:00', [])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026-05-30T15:52:19.250-07:00', [])).toBe('2026-05-30T22:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19 +0200', [])).toBe('2026-05-30T13:52:19.000Z');
// The stamp's own zone is the more specific answer.
expect(exifCaptureInstant('2026:05:30 15:52:19+02:00', ['+09:00'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19 UTC', ['+02:00'])).toBe('2026-05-30T15:52:19.000Z');
});
it('MEM-EXIF-012: a zone on the stamp that is out of range leaves the tags to decide', () => {
expect(exifCaptureInstant('2026:05:30 15:52:19+15:00', ['+02:00'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19+15:00', [])).toBe('2026-05-30T20:52:19.000Z');
});
it('MEM-EXIF-014: a field written with one digit, and a GMT zone, still read as the old reviver read them', () => {
expect(exifCaptureInstant('2026:5:30 15:52:19', ['+02:00'])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 5:52:19', ['+02:00'])).toBe('2026-05-30T03:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19 GMT', ['+02:00'])).toBe('2026-05-30T15:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19', ['GMT'])).toBe('2026-05-30T15:52:19.000Z');
// One digit is not a licence for three.
expect(exifCaptureInstant('2026:005:30 15:52:19', ['+02:00'])).toBeNull();
expect(exifCaptureInstant('2026:05:30 15:52:190', ['+02:00'])).toBeNull();
});
it('MEM-EXIF-013: anything else after the time is still not a stamp', () => {
for (const stamp of ['2026:05:30 15:52:19 CEST', '2026:05:30 15:52:19 +2', '2026:05:30 15:52:19 UTC+2', '2026:05:30 15:52:19 later']) {
expect(exifCaptureInstant(stamp, ['+02:00']), stamp).toBeNull();
}
});
it('MEM-EXIF-015: a huge stamp from a hostile upload is turned down at once, not backtracked over', () => {
// A run of digits after the seconds, then a line break: a greedy tail in
// front of an end anchor tries every split of it. 200k of them took 20 s.
const hostile = '2026:05:30 15:52:19.' + '1'.repeat(200_000) + '\nx';
const started = performance.now();
const read = exifCaptureInstant(hostile, ['+02:00']);
const withHostileOffset = exifCaptureInstant('2026:05:30 15:52:19', [hostile]);
expect(performance.now() - started).toBeLessThan(250);
expect(read).toBeNull();
expect(withHostileOffset).toBe('2026-05-30T20:52:19.000Z');
// Up to the limit a stamp still reads, and a line break is no zone.
expect(exifCaptureInstant('2026:05:30 15:52:19.' + '1'.repeat(38) + '+02:00', [])).toBe('2026-05-30T13:52:19.000Z');
expect(exifCaptureInstant('2026:05:30 15:52:19.' + '1'.repeat(39) + '+02:00', [])).toBeNull();
expect(exifCaptureInstant('2026:05:30 15:52:19\nx', ['+02:00'])).toBeNull();
});
});
@@ -0,0 +1,177 @@
/**
* PhotoCaptureBackfillService against real files (#2512).
*
* The sibling suite mocks exifr, which is how a pick that could never return GPS
* and a date read in the wrong zone both passed it: the mock handed back exactly
* what the code hoped exifr would. Here exifr reads real JPEG bytes carrying the
* tags a phone writes, through a real StorageService, with the process in a zone
* that is neither UTC nor the photographer's.
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import { Readable } from 'node:stream';
import { PhotoCaptureBackfillService } from '../../../src/nest/memories/photo-capture-backfill.service';
import type { PhotoResolverService } from '../../../src/nest/memories/photo-resolver.service';
import type { TrekPhotosRepository } from '../../../src/nest/photos/trek-photos.repository';
import { jpegWithExif, type ExifJpegTags } from '../../helpers/exif-jpeg';
import { makeStorageFixture, type StorageFixture } from '../../helpers/storage-fixture';
// Normandy, west of Greenwich, so a dropped sign would show.
const NORMANDY = { lat: 49.274523, lng: -0.703421 };
let fx: StorageFixture;
let prevTz: string | undefined;
let seq = 0;
beforeAll(() => { fx = makeStorageFixture('journey/'); });
afterAll(() => fx.cleanup());
beforeEach(() => {
prevTz = process.env.TZ;
process.env.TZ = 'America/Chicago';
});
afterEach(() => {
if (prevTz === undefined) delete process.env.TZ;
else process.env.TZ = prevTz;
});
/** Stores `bytes` as a local journey upload, runs the backfill on it, returns what was recorded. */
async function backfill(bytes: Buffer): Promise<unknown[][]> {
const name = `upload-${++seq}.jpg`;
await fx.storage.put('journey', name, Readable.from(bytes), { contentType: 'image/jpeg' });
const recordCaptureMetadata = vi.fn();
const photos = {
resolve: (id: number) => ({ id, provider: 'local', file_path: `journey/${name}`, taken_at: null, lat: null, lng: null }),
recordCaptureMetadata,
} as unknown as TrekPhotosRepository;
const svc = new PhotoCaptureBackfillService({} as PhotoResolverService, photos, fx.storage);
await svc.run([7], 1);
return recordCaptureMetadata.mock.calls;
}
const photo = (tags: ExifJpegTags) => backfill(jpegWithExif(tags));
describe('PhotoCaptureBackfillService reading real EXIF', () => {
it('EXIF-001: the reported phone photo gets its true instant and its GPS', async () => {
const calls = await photo({
DateTimeOriginal: '2026:05:30 15:52:19',
CreateDate: '2026:05:30 15:52:19',
OffsetTime: '+02:00',
OffsetTimeOriginal: '+02:00',
OffsetTimeDigitized: '+02:00',
gps: NORMANDY,
});
expect(calls).toHaveLength(1);
const [id, meta] = calls[0] as [number, { takenAt: string; lat: number; lng: number }];
expect(id).toBe(7);
// Not 20:52:19Z, which is 15:52 read as Chicago time.
expect(meta.takenAt).toBe('2026-05-30T13:52:19.000Z');
expect(meta.lat).toBeCloseTo(NORMANDY.lat, 5);
expect(meta.lng).toBeCloseTo(NORMANDY.lng, 5);
});
it('EXIF-002: the offset is found in OffsetTimeDigitized when it is the only one', async () => {
const calls = await photo({ DateTimeOriginal: '2026:05:30 15:52:19', OffsetTimeDigitized: '+02:00' });
expect(calls).toEqual([[7, { takenAt: '2026-05-30T13:52:19.000Z', lat: null, lng: null }]]);
});
it('EXIF-003: the offset is found in OffsetTime when it is the only one', async () => {
const calls = await photo({ DateTimeOriginal: '2026:05:30 15:52:19', OffsetTime: '+09:00' });
expect(calls).toEqual([[7, { takenAt: '2026-05-30T06:52:19.000Z', lat: null, lng: null }]]);
});
it('EXIF-004: DateTimeOriginal goes with its own offset when the tags disagree', async () => {
const calls = await photo({
DateTimeOriginal: '2026:05:30 15:52:19',
OffsetTimeOriginal: '+02:00',
OffsetTimeDigitized: '+05:00',
OffsetTime: '-03:00',
});
expect(calls).toEqual([[7, { takenAt: '2026-05-30T13:52:19.000Z', lat: null, lng: null }]]);
});
it('EXIF-005: CreateDate stands in for a missing DateTimeOriginal, with its own offset first', async () => {
const calls = await photo({
CreateDate: '2026:05:30 21:10:00',
OffsetTimeDigitized: '+01:00',
OffsetTimeOriginal: '+02:00',
});
expect(calls).toEqual([[7, { takenAt: '2026-05-30T20:10:00.000Z', lat: null, lng: null }]]);
});
it('EXIF-006: CreateDate also stands in when DateTimeOriginal is garbage', async () => {
const calls = await photo({
DateTimeOriginal: '0000:00:00 00:00:00',
CreateDate: '2026:05:30 15:52:19',
OffsetTimeOriginal: '+02:00',
});
expect(calls).toEqual([[7, { takenAt: '2026-05-30T13:52:19.000Z', lat: null, lng: null }]]);
});
it('EXIF-007: with no offset the stamp is read in the server zone, exactly as before', async () => {
const calls = await photo({ DateTimeOriginal: '2026:05:30 15:52:19' });
// 15:52 CDT.
expect(calls).toEqual([[7, { takenAt: '2026-05-30T20:52:19.000Z', lat: null, lng: null }]]);
});
it('EXIF-008: a malformed offset is ignored like a missing one', async () => {
for (const bad of ['+2:00', ' : ', '+99:00', 'CEST']) {
const calls = await photo({ DateTimeOriginal: '2026:05:30 15:52:19', OffsetTimeOriginal: bad });
expect(calls, bad).toEqual([[7, { takenAt: '2026-05-30T20:52:19.000Z', lat: null, lng: null }]]);
}
});
it('EXIF-009: an impossible or garbage date is not stored, and does not cost the location', async () => {
for (const bad of ['0000:00:00 00:00:00', '2026:02:30 25:61:00', ' : : : : ', 'not a date']) {
const calls = await photo({ DateTimeOriginal: bad, OffsetTimeOriginal: '+02:00', gps: NORMANDY });
expect(calls, bad).toHaveLength(1);
const [, meta] = calls[0] as [number, { takenAt: string | null; lat: number; lng: number }];
expect(meta.takenAt, bad).toBeNull();
expect(meta.lat, bad).toBeCloseTo(NORMANDY.lat, 5);
expect(meta.lng, bad).toBeCloseTo(NORMANDY.lng, 5);
}
});
it('EXIF-010: an impossible date with nothing else to go on records nothing', async () => {
expect(await photo({ DateTimeOriginal: '2026:02:30 10:00:00' })).toEqual([]);
});
it('EXIF-011: GPS without any date still puts the photo on the map', async () => {
const calls = await photo({ gps: { lat: -33.856784, lng: 151.215297 } });
expect(calls).toHaveLength(1);
const [, meta] = calls[0] as [number, { takenAt: string | null; lat: number; lng: number }];
expect(meta.takenAt).toBeNull();
expect(meta.lat).toBeCloseTo(-33.856784, 5);
expect(meta.lng).toBeCloseTo(151.215297, 5);
});
it('EXIF-012: a JPEG without EXIF and a file that is not an image both record nothing', async () => {
const plain = fs.readFileSync(path.join(__dirname, '../../fixtures/small-image.jpg'));
expect(await backfill(plain)).toEqual([]);
expect(await backfill(Buffer.from('definitely not a jpeg'))).toEqual([]);
});
it('EXIF-013: a GPS block of zeros, as a receiver without a fix writes it, is no location', async () => {
const zeros = { lat: 0, lng: 0 };
expect(await photo({ DateTimeOriginal: '2026:05:30 15:52:19', OffsetTimeOriginal: '+02:00', gps: zeros }))
.toEqual([[7, { takenAt: '2026-05-30T13:52:19.000Z', lat: null, lng: null }]]);
// Nothing else to go on: nothing is recorded, rather than a pin in the Gulf of Guinea.
expect(await photo({ gps: zeros })).toEqual([]);
});
it('EXIF-014: a real place on the equator or the prime meridian keeps its location', async () => {
for (const gps of [{ lat: 0, lng: 32.58 }, { lat: 51.4779, lng: 0 }]) {
const calls = await photo({ gps });
expect(calls, JSON.stringify(gps)).toHaveLength(1);
const [, meta] = calls[0] as [number, { lat: number; lng: number }];
expect(meta.lat).toBeCloseTo(gps.lat, 5);
expect(meta.lng).toBeCloseTo(gps.lng, 5);
}
});
it('EXIF-015: a stamp that carries its own zone is read in that zone', async () => {
const calls = await photo({ DateTimeOriginal: '2026:05:30 15:52:19 UTC', OffsetTimeOriginal: '+02:00' });
expect(calls).toEqual([[7, { takenAt: '2026-05-30T15:52:19.000Z', lat: null, lng: null }]]);
});
});
@@ -330,8 +330,10 @@ describe('PhotoCaptureBackfillService — local files', () => {
}
it('CAPTURE-007: reads a local file rather than asking a provider', async () => {
// Raw values, as exifr hands them over with reviveValues off (#2512).
vi.mocked(exifr.parse).mockResolvedValue({
DateTimeOriginal: new Date('2026-03-15T10:20:00Z'),
DateTimeOriginal: '2026:03:15 11:20:00',
OffsetTimeOriginal: '+01:00',
latitude: 48.8584,
longitude: 2.2945,
});
@@ -348,7 +350,7 @@ describe('PhotoCaptureBackfillService — local files', () => {
});
it('CAPTURE-008: falls back to CreateDate when the original timestamp is missing', async () => {
vi.mocked(exifr.parse).mockResolvedValue({ CreateDate: new Date('2026-03-16T08:00:00Z') });
vi.mocked(exifr.parse).mockResolvedValue({ CreateDate: '2026:03:16 08:00:00', OffsetTimeDigitized: '+00:00' });
const { svc, recordCaptureMetadata } = localBuild([
{ id: 7, provider: 'local', file_path: 'journey/a.jpg' },
]);
@@ -360,6 +362,35 @@ describe('PhotoCaptureBackfillService — local files', () => {
});
});
it('CAPTURE-016: a coordinate that is not a real one drops the pair, not the capture time', async () => {
// 0,0 is what a receiver without a fix writes, not a place (#2512).
for (const [latitude, longitude] of [[Number.NaN, 2.2945], [48.8584, 200], [91, 2.2945], [48.8584, undefined], [0, 0]]) {
vi.mocked(exifr.parse).mockResolvedValue({
DateTimeOriginal: '2026:03:15 11:20:00', OffsetTimeOriginal: '+01:00', latitude, longitude,
});
const { svc, recordCaptureMetadata } = localBuild([
{ id: 7, provider: 'local', file_path: 'journey/a.jpg' },
]);
await svc.run([7], 1);
expect(recordCaptureMetadata).toHaveBeenCalledWith(7, {
takenAt: '2026-03-15T10:20:00.000Z', lat: null, lng: null,
});
}
});
it('CAPTURE-017: a bad coordinate with no usable date records nothing', async () => {
vi.mocked(exifr.parse).mockResolvedValue({ DateTimeOriginal: 'garbage', latitude: 48.8584 });
const { svc, recordCaptureMetadata } = localBuild([
{ id: 7, provider: 'local', file_path: 'journey/a.jpg' },
]);
await svc.run([7], 1);
expect(recordCaptureMetadata).not.toHaveBeenCalled();
});
it('CAPTURE-009: a file with nothing readable is left alone', async () => {
vi.mocked(exifr.parse).mockResolvedValue({});
const { svc, recordCaptureMetadata } = localBuild([
@@ -423,7 +454,7 @@ describe('PhotoCaptureBackfillService — local files', () => {
});
it('CAPTURE-015: schedule kicks the run off for a non-empty batch', async () => {
vi.mocked(exifr.parse).mockResolvedValue({ DateTimeOriginal: new Date('2026-03-15T10:20:00Z') });
vi.mocked(exifr.parse).mockResolvedValue({ DateTimeOriginal: '2026:03:15 10:20:00', OffsetTimeOriginal: '+00:00' });
const { svc, recordCaptureMetadata } = localBuild([
{ id: 7, provider: 'local', file_path: 'journey/a.jpg' },
]);
@@ -433,7 +464,7 @@ describe('PhotoCaptureBackfillService — local files', () => {
});
it('CAPTURE-025: a device upload reads its EXIF while the same user\'s provider lookups hang', async () => {
vi.mocked(exifr.parse).mockResolvedValue({ DateTimeOriginal: new Date('2026-03-15T10:20:00Z') });
vi.mocked(exifr.parse).mockResolvedValue({ DateTimeOriginal: '2026:03:15 10:20:00', OffsetTimeOriginal: '+00:00' });
const { svc, getPhotoInfo, recordCaptureMetadata, recover } = buildHung([
{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 },
{ id: 5, provider: 'local', file_path: 'journey/upload.jpg' },
@@ -51,7 +51,7 @@ function makeFixture(keyPrefix = 'files/'): Fixture {
}
/** A driver with no getLocalPath — the remote-driver branch every helper must handle. */
function makeStreamOnlyFixture(contents: string): Fixture & { driverCalls: string[] } {
function makeStreamOnlyFixture(contents: string, body = (bytes: Buffer): Readable => Readable.from(bytes)): Fixture & { driverCalls: string[] } {
const fx = makeFixture('');
const driverCalls: string[] = [];
const bytes = Buffer.from(contents);
@@ -61,7 +61,7 @@ function makeStreamOnlyFixture(contents: string): Fixture & { driverCalls: strin
put: async () => undefined,
getStream: async (key: string) => {
driverCalls.push(`getStream:${key}`);
return { stream: Readable.from(bytes), stat: { ...stat, key } };
return { stream: body(bytes), stat: { ...stat, key } };
},
stat: async (key: string) => ({ ...stat, key }),
delete: async () => undefined,
@@ -216,6 +216,22 @@ describe('StorageService withLocalFile', () => {
expect(fs.existsSync(seenPath)).toBe(false); // cleaned up on throw too
});
it('leaves nothing in tempDir when the download itself fails midway', async () => {
const cut = new Error('socket hang up');
const remote = makeStreamOnlyFixture('first half of the bytes', bytes =>
Readable.from((async function* () {
yield bytes;
throw cut;
})()),
);
let called = false;
await expect(remote.storage.withLocalFile('files', 'remote.bin', async () => { called = true; })).rejects.toBe(cut);
expect(called).toBe(false);
expect(fs.readdirSync(remote.tempDir)).toEqual([]);
});
it('falls through to the stream branch when getLocalPath resolves but the file is not actually on disk', async () => {
// Composite-driver stand-in (task C9-i): the façade must fs-check the
// local path itself rather than trust driver.stat() as "the file is at