|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { collectAssetSources, mapAssetSources } from './asset-sources' |
| 3 | +import type { Slide, UploadAsset } from './types' |
| 4 | + |
| 5 | +const makeSlide = (overrides: Partial<Slide> = {}): Slide => ({ |
| 6 | + id: 'slide-1', |
| 7 | + name: 'Screen 1', |
| 8 | + background: { type: 'solid', color1: '#000', color2: '#000', angle: 0 }, |
| 9 | + elements: [], |
| 10 | + ...overrides, |
| 11 | +}) |
| 12 | + |
| 13 | +const makeUpload = (src: string): UploadAsset => ({ id: 'upload-1', name: 'shot.png', src }) |
| 14 | + |
| 15 | +const projectFixture = () => ({ |
| 16 | + uploads: [makeUpload('data:image/png;base64,AAA')], |
| 17 | + slides: [ |
| 18 | + makeSlide({ |
| 19 | + background: { type: 'image', color1: '#000', color2: '#000', angle: 0, image: 'data:image/png;base64,BBB' }, |
| 20 | + elements: [ |
| 21 | + { |
| 22 | + id: 'image-1', type: 'image' as const, x: 0, y: 0, width: 10, rotation: 0, opacity: 1, |
| 23 | + src: 'data:image/png;base64,AAA', borderRadius: 0, |
| 24 | + }, |
| 25 | + { |
| 26 | + id: 'device-1', type: 'device' as const, x: 0, y: 0, width: 10, rotation: 0, opacity: 1, |
| 27 | + deviceStyle: 'iphone-17-a' as const, screenTheme: 'coral' as const, tiltX: 0, tiltY: 0, shadow: 0, |
| 28 | + screenshot: 'data:image/png;base64,CCC', |
| 29 | + }, |
| 30 | + { |
| 31 | + id: 'shape-1', type: 'shape' as const, x: 0, y: 0, width: 10, rotation: 0, opacity: 1, |
| 32 | + shape: 'circle' as const, color: '#fff', strokeColor: '#000', strokeWidth: 0, shadow: 0, |
| 33 | + }, |
| 34 | + ], |
| 35 | + }), |
| 36 | + ], |
| 37 | +}) |
| 38 | + |
| 39 | +describe('collectAssetSources', () => { |
| 40 | + it('collects uploads, background images, image elements, and device screenshots once each', () => { |
| 41 | + expect(collectAssetSources(projectFixture())).toEqual(new Set([ |
| 42 | + 'data:image/png;base64,AAA', |
| 43 | + 'data:image/png;base64,BBB', |
| 44 | + 'data:image/png;base64,CCC', |
| 45 | + ])) |
| 46 | + }) |
| 47 | + |
| 48 | + it('skips absent optional sources and empty projects', () => { |
| 49 | + expect(collectAssetSources({ uploads: [], slides: [makeSlide()] })).toEqual(new Set()) |
| 50 | + expect(collectAssetSources({ uploads: [], slides: [] })).toEqual(new Set()) |
| 51 | + }) |
| 52 | +}) |
| 53 | + |
| 54 | +describe('mapAssetSources', () => { |
| 55 | + it('rewrites every source field through the mapper', () => { |
| 56 | + const mapped = mapAssetSources(projectFixture(), (src) => `mapped:${src.slice(-3)}`) |
| 57 | + expect(mapped.uploads[0]?.src).toBe('mapped:AAA') |
| 58 | + expect(mapped.slides[0]?.background.image).toBe('mapped:BBB') |
| 59 | + expect(mapped.slides[0]?.elements[0]).toMatchObject({ src: 'mapped:AAA' }) |
| 60 | + expect(mapped.slides[0]?.elements[1]).toMatchObject({ screenshot: 'mapped:CCC' }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('preserves untouched objects by identity so undo history keeps sharing structure', () => { |
| 64 | + const project = projectFixture() |
| 65 | + const identical = mapAssetSources(project, (src) => src) |
| 66 | + expect(identical.slides[0]).toBe(project.slides[0]) |
| 67 | + expect(identical.uploads[0]).toBe(project.uploads[0]) |
| 68 | + |
| 69 | + const partial = mapAssetSources(project, (src) => src.endsWith('AAA') ? 'mapped:AAA' : src) |
| 70 | + expect(partial.slides[0]).not.toBe(project.slides[0]) |
| 71 | + expect(partial.slides[0]?.background).toBe(project.slides[0]?.background) |
| 72 | + expect(partial.slides[0]?.elements[1]).toBe(project.slides[0]?.elements[1]) |
| 73 | + expect(partial.slides[0]?.elements[2]).toBe(project.slides[0]?.elements[2]) |
| 74 | + }) |
| 75 | +}) |
0 commit comments