|
| 1 | +import { afterEach, beforeEach, describe, expect, it, test as baseTest, vi } from 'vitest'; |
| 2 | +import { cleanup, render, type RenderResult } from 'vitest-browser-react'; |
| 3 | +import * as funcs from '../functions'; |
| 4 | +import type { Pixel, VideoElement } from '../types'; |
| 5 | + |
| 6 | +interface Fixtures { |
| 7 | + canvas: HTMLCanvasElement; |
| 8 | + image: HTMLImageElement; |
| 9 | + video: VideoElement; |
| 10 | + reinit: () => ReturnType<ReturnType<typeof funcs.start>['init']>; |
| 11 | + errLogger: () => void; |
| 12 | +} |
| 13 | + |
| 14 | +const test = baseTest.extend<{ |
| 15 | + screen: RenderResult; |
| 16 | + fixtures: Fixtures; |
| 17 | +}>({ |
| 18 | + screen: async ({}, fixture) => { |
| 19 | + await fixture( |
| 20 | + render(<div> |
| 21 | + <canvas title="canvas" /> |
| 22 | + <video title="vid" /> |
| 23 | + <img title='img' /> |
| 24 | + </div>) |
| 25 | + ); |
| 26 | + cleanup(); |
| 27 | + }, |
| 28 | + fixtures: async ({ screen }, fixture) => { |
| 29 | + const { init, deinit } = funcs.start(); |
| 30 | + const errLogger = vi.fn(); |
| 31 | + const canvas = screen |
| 32 | + .getByTitle('canvas') |
| 33 | + .element() as HTMLCanvasElement; |
| 34 | + |
| 35 | + const image = screen |
| 36 | + .getByTitle('img') |
| 37 | + .element() as HTMLImageElement; |
| 38 | + |
| 39 | + const video = screen |
| 40 | + .getByTitle('vid') |
| 41 | + .element() as VideoElement; |
| 42 | + |
| 43 | + const reinit = () => init(image, video, canvas, errLogger, { onClickStill: () => {} }); |
| 44 | + reinit(); |
| 45 | + await fixture({ |
| 46 | + image, |
| 47 | + canvas, |
| 48 | + video, |
| 49 | + errLogger, |
| 50 | + reinit |
| 51 | + }); |
| 52 | + deinit(); |
| 53 | + } |
| 54 | +}); |
| 55 | + |
| 56 | +const height = funcs.image_height(); |
| 57 | +const width = funcs.image_width(); |
| 58 | + |
| 59 | +beforeEach(() => { |
| 60 | + vi.useFakeTimers(); |
| 61 | +}); |
| 62 | + |
| 63 | +afterEach(() => { |
| 64 | + vi.runOnlyPendingTimers(); |
| 65 | + vi.useRealTimers(); |
| 66 | +}); |
| 67 | + |
| 68 | +describe('pixel manipulation functions', () => { |
| 69 | + describe(funcs.alpha_of, () => { |
| 70 | + it('works', () => { |
| 71 | + expect(funcs.alpha_of([0, 0, 0, 255])).toEqual(255); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe(funcs.red_of, () => { |
| 76 | + it('works', () => { |
| 77 | + expect(funcs.red_of([255, 0, 0, 0])).toEqual(255); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe(funcs.green_of, () => { |
| 82 | + it('works', () => { |
| 83 | + expect(funcs.green_of([0, 255, 0, 0])).toEqual(255); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe(funcs.blue_of, () => { |
| 88 | + it('works', () => { |
| 89 | + expect(funcs.blue_of([0, 0, 255, 0])).toEqual(255); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe(funcs.set_rgba, () => { |
| 94 | + it('works', () => { |
| 95 | + const pixel: Pixel = [0, 0, 0, 0]; |
| 96 | + expect(funcs.set_rgba(pixel, 1, 2, 3, 4)).toBeUndefined(); |
| 97 | + for (let i = 0; i < 4; i++) { |
| 98 | + expect(pixel[i]).toEqual(i + 1); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe(funcs.isPixelValid, () => { |
| 105 | + it('returns true if the pixel is valid', () => { |
| 106 | + const pixel: Pixel = [0, 1, 2, 3]; |
| 107 | + expect(funcs.isPixelValid(pixel)).toEqual(true); |
| 108 | + for (let i = 0; i < 4; i++) { |
| 109 | + expect(pixel[i]).toEqual(i); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns false and resets the pixel if it is invalid', () => { |
| 114 | + const pixel: Pixel = [-1, 0, 0, 0]; |
| 115 | + expect(funcs.isPixelValid(pixel)).toEqual(false); |
| 116 | + for (let i = 0; i < 4; i++) { |
| 117 | + expect(pixel[i]).toEqual(0); |
| 118 | + } |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe(funcs.writeToBuffer, () => { |
| 123 | + test('with valid data', ({ fixtures: { errLogger } }) => { |
| 124 | + const img = funcs.new_image(); |
| 125 | + funcs.set_rgba(img[0][0], 0, 1, 2, 3); |
| 126 | + |
| 127 | + const imageData = new ImageData(width, height); |
| 128 | + const buffer = imageData.data; |
| 129 | + funcs.writeToBuffer(buffer, img); |
| 130 | + |
| 131 | + expect(errLogger).not.toHaveBeenCalled(); |
| 132 | + expect(buffer.length).toBeGreaterThan(0); |
| 133 | + |
| 134 | + for (let i = 0; i < 4; i++) { |
| 135 | + expect(buffer[i]).toEqual(i); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + test('with invalid data', ({ fixtures: { errLogger } }) => { |
| 140 | + const img = funcs.new_image(); |
| 141 | + funcs.set_rgba(img[0][0], 999, 999, 999, 999); |
| 142 | + |
| 143 | + const imageData = new ImageData(width, height); |
| 144 | + const buffer = imageData.data; |
| 145 | + funcs.writeToBuffer(buffer, img); |
| 146 | + |
| 147 | + expect(errLogger).toHaveBeenCalled(); |
| 148 | + expect(buffer.length).toBeGreaterThan(0); |
| 149 | + |
| 150 | + for (let i = 0; i < 4; i++) { |
| 151 | + expect(buffer[i]).toEqual(0); |
| 152 | + } |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +describe('video functions', () => { |
| 157 | + test('startVideo and stopVideo', ({ fixtures: { errLogger } }) => { |
| 158 | + const filter = vi.fn(funcs.copy_image); |
| 159 | + funcs.install_filter(filter); |
| 160 | + funcs.startVideo(); |
| 161 | + |
| 162 | + for (let i = 0; i < 67; i++) { |
| 163 | + vi.advanceTimersToNextFrame(); |
| 164 | + } |
| 165 | + |
| 166 | + expect(filter).toHaveBeenCalledTimes(9); |
| 167 | + expect(errLogger).not.toHaveBeenCalled(); |
| 168 | + |
| 169 | + funcs.stopVideo(); |
| 170 | + |
| 171 | + for (let i = 0; i < 67; i++) { |
| 172 | + vi.advanceTimersToNextFrame(); |
| 173 | + } |
| 174 | + |
| 175 | + // Filter should not have been called again after stopVideo was called |
| 176 | + expect(filter).toHaveBeenCalledTimes(9); |
| 177 | + expect(errLogger).not.toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + |
| 180 | + // Test just doesn't work properly |
| 181 | + describe.skip(funcs.set_fps, () => { |
| 182 | + test('Setting FPS works', ({ fixtures: { reinit } }) => { |
| 183 | + expect(() => funcs.set_fps(20)).not.toThrow(); |
| 184 | + const { FPS } = reinit(); |
| 185 | + expect(FPS).toEqual(20); |
| 186 | + }); |
| 187 | + |
| 188 | + test('Lowest FPS is 1', ({ fixtures: { reinit } }) => { |
| 189 | + expect(() => funcs.set_fps(0)).not.toThrow(); |
| 190 | + const { FPS } = reinit(); |
| 191 | + expect(FPS).toEqual(1); |
| 192 | + }); |
| 193 | + |
| 194 | + test('Highest FPS is 60', ({ fixtures: { reinit } }) => { |
| 195 | + expect(() => funcs.set_fps(999)).not.toThrow(); |
| 196 | + const { FPS } = reinit(); |
| 197 | + expect(FPS).toEqual(60); |
| 198 | + }); |
| 199 | + }); |
| 200 | +}); |
0 commit comments