|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { collectClipboardImages } from './clipboard-images' |
| 3 | +import { normalizePastedImageFile, validateImageFile } from './validate-image' |
| 4 | + |
| 5 | +function t(key: string): string { |
| 6 | + return key |
| 7 | +} |
| 8 | + |
| 9 | +function makeFile(name: string, type: string, size = 16): File { |
| 10 | + return new File([new Uint8Array(size)], name, { type }) |
| 11 | +} |
| 12 | + |
| 13 | +describe(`validateImageFile`, () => { |
| 14 | + it(`accepts a named PNG`, () => { |
| 15 | + expect(validateImageFile(makeFile(`shot.png`, `image/png`), t)).toEqual({ ok: true }) |
| 16 | + }) |
| 17 | + |
| 18 | + it(`accepts a clipboard screenshot with MIME type but no filename`, () => { |
| 19 | + expect(validateImageFile(makeFile(``, `image/png`), t)).toEqual({ ok: true }) |
| 20 | + }) |
| 21 | + |
| 22 | + it(`accepts a file with a valid suffix even when type is empty`, () => { |
| 23 | + expect(validateImageFile(makeFile(`photo.JPEG`, ``), t)).toEqual({ ok: true }) |
| 24 | + }) |
| 25 | + |
| 26 | + it(`rejects a non-image file`, () => { |
| 27 | + expect(validateImageFile(makeFile(`notes.pdf`, `application/pdf`), t)).toEqual({ |
| 28 | + ok: false, |
| 29 | + msg: `upload.errors.invalidFormat`, |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + it(`rejects oversized images`, () => { |
| 34 | + expect(validateImageFile(makeFile(`huge.png`, `image/png`, 11 * 1024 * 1024), t)).toEqual({ |
| 35 | + ok: false, |
| 36 | + msg: `upload.errors.tooLarge`, |
| 37 | + }) |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe(`normalizePastedImageFile`, () => { |
| 42 | + it(`keeps a file that already has an image suffix`, () => { |
| 43 | + const file = makeFile(`shot.png`, `image/png`) |
| 44 | + expect(normalizePastedImageFile(file)).toBe(file) |
| 45 | + }) |
| 46 | + |
| 47 | + it(`adds a filename when the clipboard image has no extension`, () => { |
| 48 | + const normalized = normalizePastedImageFile(makeFile(``, `image/png`)) |
| 49 | + expect(normalized.name).toBe(`pasted-image.png`) |
| 50 | + expect(normalized.type).toBe(`image/png`) |
| 51 | + }) |
| 52 | +}) |
| 53 | + |
| 54 | +describe(`collectClipboardImages`, () => { |
| 55 | + it(`returns an empty list when clipboard data is missing`, () => { |
| 56 | + expect(collectClipboardImages(null)).toEqual([]) |
| 57 | + }) |
| 58 | + |
| 59 | + it(`collects image files from clipboard items`, () => { |
| 60 | + const image = makeFile(`image.png`, `image/png`) |
| 61 | + const data = { |
| 62 | + items: [{ |
| 63 | + kind: `file`, |
| 64 | + type: `image/png`, |
| 65 | + getAsFile: () => image, |
| 66 | + }], |
| 67 | + files: [image], |
| 68 | + } as unknown as DataTransfer |
| 69 | + |
| 70 | + expect(collectClipboardImages(data)).toEqual([image]) |
| 71 | + }) |
| 72 | + |
| 73 | + it(`ignores files when items already contain the clipboard image`, () => { |
| 74 | + const fromItems = makeFile(`image.png`, `image/png`) |
| 75 | + const fromFiles = makeFile(`image.png`, `image/png`) |
| 76 | + const data = { |
| 77 | + items: [{ |
| 78 | + kind: `file`, |
| 79 | + type: `image/png`, |
| 80 | + getAsFile: () => fromItems, |
| 81 | + }], |
| 82 | + files: [fromFiles], |
| 83 | + } as unknown as DataTransfer |
| 84 | + |
| 85 | + expect(collectClipboardImages(data)).toEqual([fromItems]) |
| 86 | + }) |
| 87 | + |
| 88 | + it(`reads files when clipboard items have no image`, () => { |
| 89 | + const image = makeFile(`shot.png`, `image/png`) |
| 90 | + const data = { |
| 91 | + items: [{ |
| 92 | + kind: `string`, |
| 93 | + type: `text/plain`, |
| 94 | + getAsFile: () => null, |
| 95 | + }], |
| 96 | + files: [image], |
| 97 | + } as unknown as DataTransfer |
| 98 | + |
| 99 | + expect(collectClipboardImages(data)).toEqual([image]) |
| 100 | + }) |
| 101 | + |
| 102 | + it(`normalizes nameless clipboard screenshots`, () => { |
| 103 | + const image = makeFile(``, `image/webp`) |
| 104 | + const data = { |
| 105 | + items: [{ |
| 106 | + kind: `file`, |
| 107 | + type: `image/webp`, |
| 108 | + getAsFile: () => image, |
| 109 | + }], |
| 110 | + files: [], |
| 111 | + } as unknown as DataTransfer |
| 112 | + |
| 113 | + const collected = collectClipboardImages(data) |
| 114 | + expect(collected).toHaveLength(1) |
| 115 | + expect(collected[0].name).toBe(`pasted-image.webp`) |
| 116 | + }) |
| 117 | + |
| 118 | + it(`ignores non-image clipboard files`, () => { |
| 119 | + const pdf = makeFile(`doc.pdf`, `application/pdf`) |
| 120 | + const data = { |
| 121 | + items: [{ |
| 122 | + kind: `file`, |
| 123 | + type: `application/pdf`, |
| 124 | + getAsFile: () => pdf, |
| 125 | + }], |
| 126 | + files: [pdf], |
| 127 | + } as unknown as DataTransfer |
| 128 | + |
| 129 | + expect(collectClipboardImages(data)).toEqual([]) |
| 130 | + }) |
| 131 | +}) |
0 commit comments