summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/tests/Reader.test.ts
blob: e12d69b0c569f8725372a6ea19fd59add7c3b2a4 (plain) (blame)
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
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]);
});