From d1d654ebac2d51e3841675faeb56480e440f622f Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Tue, 5 Mar 2024 18:08:09 +0100 Subject: Initial commit --- frontend/tests/Reader.test.ts | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 frontend/tests/Reader.test.ts (limited to 'frontend/tests/Reader.test.ts') diff --git a/frontend/tests/Reader.test.ts b/frontend/tests/Reader.test.ts new file mode 100644 index 0000000..e12d69b --- /dev/null +++ b/frontend/tests/Reader.test.ts @@ -0,0 +1,45 @@ +import { Layout, type PageFragment } from '$gql/graphql'; +import { partition, type Chunk } from '$lib/Reader'; +import { expect, test } from 'vitest'; + +const normalAttrs = { aspectRatio: 0.7, width: 140, height: 200 }; +const wideAttrs = { aspectRatio: 1.4, width: 280, height: 200 }; + +const pages: PageFragment[] = [ + { id: 0, path: '000.png', image: { id: 0, hash: '0', ...normalAttrs } }, + { id: 1, path: '001.png', image: { id: 1, hash: '1', ...normalAttrs } }, + { id: 2, path: '002.png', image: { id: 2, hash: '2', ...normalAttrs } }, + { id: 3, path: '003.png', image: { id: 3, hash: '3', ...normalAttrs } }, + { id: 4, path: '004.png', image: { id: 4, hash: '4', ...wideAttrs } }, + { id: 5, path: '005.png', image: { id: 5, hash: '5', ...normalAttrs } }, + { id: 6, path: '006.png', image: { id: 6, hash: '6', ...normalAttrs } } +]; + +const ids = (chunks: Chunk[]) => + chunks.map((c) => (c.secondary ? [c.main.id, c.secondary.id] : [c.main.id])); + +const indices = (chunks: Chunk[]) => chunks.map((c) => c.index); + +test('partitions single layout', () => { + const [chunks, lookup] = partition(pages, Layout.Single); + + expect(ids(chunks)).toStrictEqual([[0], [1], [2], [3], [4], [5], [6]]); + expect(indices(chunks)).toStrictEqual([0, 1, 2, 3, 4, 5, 6]); + expect(lookup).toStrictEqual([0, 1, 2, 3, 4, 5, 6]); +}); + +test('partitions double layout', () => { + const [chunks, lookup] = partition(pages, Layout.Double); + + expect(ids(chunks)).toStrictEqual([[0, 1], [2, 3], [4], [5, 6]]); + expect(indices(chunks)).toStrictEqual([0, 2, 4, 5]); + expect(lookup).toStrictEqual([0, 0, 1, 1, 2, 3, 3]); +}); + +test('partitions double (offset) layout', () => { + const [chunks, lookup] = partition(pages, Layout.DoubleOffset); + + expect(ids(chunks)).toStrictEqual([[0], [1, 2], [3], [4], [5, 6]]); + expect(indices(chunks)).toStrictEqual([0, 1, 3, 4, 5]); + expect(lookup).toStrictEqual([0, 1, 1, 2, 3, 4, 4]); +}); -- cgit v1.2.3-2-gb3c3