File size: 13,355 Bytes
79ed62f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';

// Module-level types for dynamic imports
type PhotoServiceModule = typeof import('../../../src/services/photoService');
type ApiClientModule = typeof import('../../../src/api/client');

let svc: PhotoServiceModule;
let mockPlacePhoto: ReturnType<typeof vi.fn>;

// ── Canvas mock helpers ────────────────────────────────────────────────────────

function setupCanvasMock(dataUrl = 'data:image/webp;base64,mock') {
  vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
    beginPath: vi.fn(),
    arc: vi.fn(),
    clip: vi.fn(),
    drawImage: vi.fn(),
  } as unknown as CanvasRenderingContext2D);
  vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue(dataUrl);
}

// ── Image src interceptor ──────────────────────────────────────────────────────
// jsdom doesn't load images; we override the src setter so onload/onerror fire.

function setupImageAutoLoad(succeed = true) {
  Object.defineProperty(HTMLImageElement.prototype, 'src', {
    configurable: true,
    set(url: string) {
      (this as HTMLImageElement & { _src: string })._src = url;
      // Fire asynchronously so assignment completes before handler runs
      Promise.resolve().then(() => {
        if (succeed && typeof this.onload === 'function') {
          this.onload(new Event('load'));
        } else if (!succeed && typeof this.onerror === 'function') {
          this.onerror(new Event('error'));
        }
      });
    },
    get() {
      return (this as HTMLImageElement & { _src: string })._src ?? '';
    },
  });
}

function restoreImageSrc() {
  // Remove override β€” jsdom's descriptor is on the prototype, restoring
  // configurable property to original (no-op src) is sufficient for test isolation.
  Object.defineProperty(HTMLImageElement.prototype, 'src', {
    configurable: true,
    set(_url: string) {},
    get() { return ''; },
  });
}

// ── Module reset helpers ───────────────────────────────────────────────────────

async function freshImports() {
  vi.resetModules();
  vi.doMock('../../../src/api/client', () => ({
    mapsApi: { placePhoto: vi.fn() },
  }));
  svc = await import('../../../src/services/photoService');
  const apiClient = await import('../../../src/api/client') as ApiClientModule;
  mockPlacePhoto = vi.mocked(apiClient.mapsApi.placePhoto);
}

// ── Flush all pending microtasks + macrotasks ──────────────────────────────────
const flush = () => new Promise<void>(r => setTimeout(r, 0));

// ==============================================================================

beforeEach(async () => {
  await freshImports();
  setupCanvasMock();
  setupImageAutoLoad(true); // default: image loads succeed so urlToBase64 resolves and .finally() runs
});

afterEach(() => {
  vi.restoreAllMocks();
  restoreImageSrc();
  vi.clearAllMocks();
});

// ==============================================================================
// getCached / isLoading
// ==============================================================================

describe('getCached', () => {
  it('FE-COMP-PHOTO-001: returns undefined for an unknown key', () => {
    expect(svc.getCached('missing')).toBeUndefined();
  });
});

describe('isLoading', () => {
  it('FE-COMP-PHOTO-002: returns false before any fetch', () => {
    expect(svc.isLoading('key')).toBe(false);
  });
});

// ==============================================================================
// fetchPhoto β€” cache hit
// ==============================================================================

describe('fetchPhoto β€” cache hit', () => {
  it('FE-COMP-PHOTO-003: callback fires immediately on second call; API called only once', async () => {
    mockPlacePhoto.mockResolvedValue({ photoUrl: 'https://example.com/photo.jpg' });

    const cb1 = vi.fn();
    svc.fetchPhoto('k', 'pid', undefined, undefined, undefined, cb1);
    await flush();

    expect(mockPlacePhoto).toHaveBeenCalledTimes(1);
    expect(cb1).toHaveBeenCalledWith(expect.objectContaining({ photoUrl: 'https://example.com/photo.jpg' }));

    const cb2 = vi.fn();
    svc.fetchPhoto('k', 'pid', undefined, undefined, undefined, cb2);
    // Cache hit β†’ synchronous call, no additional API request
    expect(cb2).toHaveBeenCalledWith(expect.objectContaining({ photoUrl: 'https://example.com/photo.jpg' }));
    expect(mockPlacePhoto).toHaveBeenCalledTimes(1);
  });
});

// ==============================================================================
// fetchPhoto β€” in-flight deduplication
// ==============================================================================

describe('fetchPhoto β€” in-flight deduplication', () => {
  it('FE-COMP-PHOTO-004: concurrent calls make only one API request; both callbacks receive result', async () => {
    let resolve!: (v: { photoUrl: string }) => void;
    mockPlacePhoto.mockReturnValue(new Promise<{ photoUrl: string }>(r => { resolve = r; }));

    const cb1 = vi.fn();
    const cb2 = vi.fn();
    svc.fetchPhoto('k', 'pid', undefined, undefined, undefined, cb1);
    svc.fetchPhoto('k', 'pid', undefined, undefined, undefined, cb2);

    // acquireRequestSlot() is async (Promise.resolve), so flush microtasks before asserting
    await flush();
    expect(mockPlacePhoto).toHaveBeenCalledTimes(1);

    resolve({ photoUrl: 'https://example.com/photo.jpg' });
    await flush();

    expect(cb1).toHaveBeenCalledWith(expect.objectContaining({ photoUrl: 'https://example.com/photo.jpg' }));
    expect(cb2).toHaveBeenCalledWith(expect.objectContaining({ photoUrl: 'https://example.com/photo.jpg' }));
  });
});

// ==============================================================================
// fetchPhoto β€” photoUrl present
// ==============================================================================

describe('fetchPhoto β€” photoUrl present', () => {
  it('FE-COMP-PHOTO-005: callback receives entry with photoUrl set and thumbDataUrl null at call time', async () => {
    mockPlacePhoto.mockResolvedValue({ photoUrl: 'https://example.com/photo.jpg' });

    // Capture a shallow clone at the moment of the call, before the entry is mutated by thumb generation
    const snapshots: { photoUrl: string | null; thumbDataUrl: string | null }[] = [];
    const cb = vi.fn((entry: { photoUrl: string | null; thumbDataUrl: string | null }) => {
      snapshots.push({ ...entry });
    });

    svc.fetchPhoto('k', 'pid', undefined, undefined, undefined, cb);
    await flush();

    expect(cb).toHaveBeenCalledTimes(1);
    expect(snapshots[0]).toEqual({ photoUrl: 'https://example.com/photo.jpg', thumbDataUrl: null });
  });

  it('FE-COMP-PHOTO-006: getCached returns the entry after fetch resolves', async () => {
    mockPlacePhoto.mockResolvedValue({ photoUrl: 'https://example.com/photo.jpg' });

    svc.fetchPhoto('k', 'pid');
    await flush();

    const entry = svc.getCached('k');
    expect(entry).toBeDefined();
    expect(entry!.photoUrl).toBe('https://example.com/photo.jpg');
  });

  it('FE-COMP-PHOTO-007: isLoading returns false after fetch completes', async () => {
    mockPlacePhoto.mockResolvedValue({ photoUrl: 'https://example.com/photo.jpg' });

    svc.fetchPhoto('k', 'pid');
    await flush();

    expect(svc.isLoading('k')).toBe(false);
  });
});

// ==============================================================================
// fetchPhoto β€” photoUrl null
// ==============================================================================

describe('fetchPhoto β€” photoUrl null', () => {
  it('FE-COMP-PHOTO-008: callback receives null entry when API returns no photoUrl', async () => {
    mockPlacePhoto.mockResolvedValue({});

    const cb = vi.fn();
    svc.fetchPhoto('k', 'pid', undefined, undefined, undefined, cb);
    await flush();

    expect(cb).toHaveBeenCalledWith({ photoUrl: null, thumbDataUrl: null });
    expect(svc.getCached('k')).toEqual({ photoUrl: null, thumbDataUrl: null });
  });
});

// ==============================================================================
// fetchPhoto β€” API error
// ==============================================================================

describe('fetchPhoto β€” API error', () => {
  it('FE-COMP-PHOTO-009: callback receives null entry on API rejection', async () => {
    mockPlacePhoto.mockRejectedValue(new Error('Network error'));

    const cb = vi.fn();
    svc.fetchPhoto('k', 'pid', undefined, undefined, undefined, cb);
    await flush();

    expect(cb).toHaveBeenCalledWith({ photoUrl: null, thumbDataUrl: null });
    expect(svc.getCached('k')).toEqual({ photoUrl: null, thumbDataUrl: null });
  });
});

// ==============================================================================
// onPhotoLoaded
// ==============================================================================

describe('onPhotoLoaded', () => {
  it('FE-COMP-PHOTO-010: listener fires once when photo is fetched', async () => {
    mockPlacePhoto.mockResolvedValue({ photoUrl: 'https://example.com/photo.jpg' });

    const fn = vi.fn();
    svc.onPhotoLoaded('k', fn);
    svc.fetchPhoto('k', 'pid');
    await flush();

    expect(fn).toHaveBeenCalledTimes(1);
    expect(fn).toHaveBeenCalledWith(expect.objectContaining({ photoUrl: 'https://example.com/photo.jpg' }));
  });

  it('FE-COMP-PHOTO-011: unsubscribe prevents callback from being called', async () => {
    mockPlacePhoto.mockResolvedValue({ photoUrl: 'https://example.com/photo.jpg' });

    const fn = vi.fn();
    const unsub = svc.onPhotoLoaded('k', fn);
    unsub();
    svc.fetchPhoto('k', 'pid');
    await flush();

    expect(fn).not.toHaveBeenCalled();
  });
});

// ==============================================================================
// onThumbReady
// ==============================================================================

describe('onThumbReady', () => {
  it('FE-COMP-PHOTO-012: fires when urlToBase64 produces a thumb', async () => {
    mockPlacePhoto.mockResolvedValue({ photoUrl: 'https://example.com/img.jpg' });
    setupImageAutoLoad(true); // trigger img.onload β†’ canvas path runs
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/webp;base64,thumb');

    const fn = vi.fn();
    svc.onThumbReady('k', fn);
    svc.fetchPhoto('k', 'pid');

    // flush microtasks + macrotasks to let urlToBase64 complete
    await flush();
    await flush();

    expect(fn).toHaveBeenCalledWith('data:image/webp;base64,thumb');
    expect(svc.getCached('k')?.thumbDataUrl).toBe('data:image/webp;base64,thumb');
  });

  it('FE-COMP-PHOTO-013: unsubscribe prevents thumb callback', async () => {
    mockPlacePhoto.mockResolvedValue({ photoUrl: 'https://example.com/img.jpg' });
    setupImageAutoLoad(true);

    const fn = vi.fn();
    const unsub = svc.onThumbReady('k', fn);
    unsub();
    svc.fetchPhoto('k', 'pid');

    await flush();
    await flush();

    expect(fn).not.toHaveBeenCalled();
  });
});

// ==============================================================================
// urlToBase64
// ==============================================================================

describe('urlToBase64', () => {
  it('FE-COMP-PHOTO-014: returns null when image fails to load', async () => {
    setupImageAutoLoad(false); // triggers onerror
    const result = await svc.urlToBase64('https://bad-url.jpg');
    expect(result).toBeNull();
  });

  it('FE-COMP-PHOTO-015: returns a data URL string on successful load', async () => {
    setupImageAutoLoad(true);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/webp;base64,abc123');

    const result = await svc.urlToBase64('https://example.com/img.jpg', 48);
    expect(result).toBe('data:image/webp;base64,abc123');
  });

  it('FE-COMP-PHOTO-016: canvas clip/draw path does not throw', async () => {
    setupImageAutoLoad(true);
    await expect(svc.urlToBase64('https://example.com/img.jpg')).resolves.not.toThrow();
  });
});

// ==============================================================================
// getAllThumbs
// ==============================================================================

describe('getAllThumbs', () => {
  it('FE-COMP-PHOTO-017: returns only entries with a non-null thumbDataUrl', async () => {
    // key1: photo with thumb
    mockPlacePhoto.mockResolvedValueOnce({ photoUrl: 'https://example.com/img1.jpg' });
    // key2: no photo, no thumb
    mockPlacePhoto.mockResolvedValueOnce({});

    setupImageAutoLoad(true);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/webp;base64,thumb1');

    svc.fetchPhoto('key1', 'pid1');
    svc.fetchPhoto('key2', 'pid2');

    await flush();
    await flush();

    const thumbs = svc.getAllThumbs();
    expect(Object.keys(thumbs)).toContain('key1');
    expect(thumbs['key1']).toBe('data:image/webp;base64,thumb1');
    expect(Object.keys(thumbs)).not.toContain('key2');
  });
});