|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { buildAlignUrl, parseAlignBody } from './align.js'; |
| 3 | +import { decodeState } from '$lib/serialization/decode.js'; |
| 4 | + |
| 5 | +const ORIGIN = 'https://example.com'; |
| 6 | + |
| 7 | +// --- parseAlignBody --- |
| 8 | + |
| 9 | +describe('parseAlignBody', () => { |
| 10 | + it('rejects non-object body', () => { |
| 11 | + expect(parseAlignBody(null)).toMatchObject({ err: expect.any(String) }); |
| 12 | + expect(parseAlignBody('string')).toMatchObject({ err: expect.any(String) }); |
| 13 | + }); |
| 14 | + |
| 15 | + it('rejects missing lines', () => { |
| 16 | + expect(parseAlignBody({})).toMatchObject({ err: expect.stringContaining('"lines"') }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('rejects empty lines array', () => { |
| 20 | + expect(parseAlignBody({ lines: [] })).toMatchObject({ err: expect.any(String) }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('rejects non-string line', () => { |
| 24 | + expect(parseAlignBody({ lines: [1, 2] })).toMatchObject({ err: expect.any(String) }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('rejects more than 8 lines', () => { |
| 28 | + expect(parseAlignBody({ lines: Array(9).fill('x') })).toMatchObject({ err: expect.any(String) }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('accepts lines without alignments', () => { |
| 32 | + const result = parseAlignBody({ lines: ['Hello', 'Bonjour'] }); |
| 33 | + expect(result).toMatchObject({ ok: { lines: ['Hello', 'Bonjour'], alignments: [] } }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('accepts lines with alignments', () => { |
| 37 | + const result = parseAlignBody({ lines: ['Hello', 'Bonjour'], alignments: [[0, 0, 1, 0]] }); |
| 38 | + expect(result).toMatchObject({ ok: { lines: ['Hello', 'Bonjour'], alignments: [[0, 0, 1, 0]] } }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('rejects alignment tuples that are not length-4 integer arrays', () => { |
| 42 | + expect(parseAlignBody({ lines: ['a', 'b'], alignments: [[0, 0, 1]] })).toMatchObject({ |
| 43 | + err: expect.any(String) |
| 44 | + }); |
| 45 | + expect(parseAlignBody({ lines: ['a', 'b'], alignments: [[0, 0, 1, 0.5]] })).toMatchObject({ |
| 46 | + err: expect.any(String) |
| 47 | + }); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +// --- buildAlignUrl --- |
| 52 | + |
| 53 | +describe('buildAlignUrl', () => { |
| 54 | + it('returns a URL with ?data= for two lines', () => { |
| 55 | + const result = buildAlignUrl(ORIGIN, { lines: ['Hello world', 'Bonjour le monde'] }); |
| 56 | + expect(result).not.toHaveProperty('err'); |
| 57 | + if ('url' in result) { |
| 58 | + expect(result.url).toMatch(/^https:\/\/example\.com\/\?data=/); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it('the encoded URL round-trips: decoding it yields the original lines', () => { |
| 63 | + const lines = ['Hello world', 'Bonjour le monde']; |
| 64 | + const result = buildAlignUrl(ORIGIN, { lines }); |
| 65 | + expect(result).not.toHaveProperty('err'); |
| 66 | + if (!('url' in result)) return; |
| 67 | + |
| 68 | + const dataParam = new URL(result.url).searchParams.get('data'); |
| 69 | + expect(dataParam).toBeTruthy(); |
| 70 | + const state = decodeState(dataParam); |
| 71 | + expect(state.project.lines.map((l) => l.rawText)).toEqual(lines); |
| 72 | + }); |
| 73 | + |
| 74 | + it('encodes alignments correctly: connections appear in decoded state', () => { |
| 75 | + const result = buildAlignUrl(ORIGIN, { |
| 76 | + lines: ['Hello world', 'Bonjour le monde'], |
| 77 | + alignments: [[0, 0, 1, 0]] |
| 78 | + }); |
| 79 | + expect(result).not.toHaveProperty('err'); |
| 80 | + if (!('url' in result)) return; |
| 81 | + |
| 82 | + const dataParam = new URL(result.url).searchParams.get('data'); |
| 83 | + const state = decodeState(dataParam); |
| 84 | + expect(state.project.connections).toHaveLength(1); |
| 85 | + expect(state.project.connections[0]!.upperTokenId).toBe('l0-0'); |
| 86 | + expect(state.project.connections[0]!.lowerTokenId).toBe('l1-0'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('maps word indices to correct token IDs for multi-word line', () => { |
| 90 | + // "Bonjour le monde": word 2 → "monde" → token id l1-2 |
| 91 | + const result = buildAlignUrl(ORIGIN, { |
| 92 | + lines: ['Hello world', 'Bonjour le monde'], |
| 93 | + alignments: [[0, 1, 1, 2]] |
| 94 | + }); |
| 95 | + if (!('url' in result)) throw new Error('expected url'); |
| 96 | + const state = decodeState(new URL(result.url).searchParams.get('data')); |
| 97 | + expect(state.project.connections[0]!.upperTokenId).toBe('l0-1'); // "world" |
| 98 | + expect(state.project.connections[0]!.lowerTokenId).toBe('l1-2'); // "monde" |
| 99 | + }); |
| 100 | + |
| 101 | + it('handles 3 lines with alignments between each adjacent pair', () => { |
| 102 | + const result = buildAlignUrl(ORIGIN, { |
| 103 | + lines: ['A B', 'C D', 'E F'], |
| 104 | + alignments: [ |
| 105 | + [0, 0, 1, 0], |
| 106 | + [1, 1, 2, 1] |
| 107 | + ] |
| 108 | + }); |
| 109 | + expect(result).not.toHaveProperty('err'); |
| 110 | + if (!('url' in result)) return; |
| 111 | + const state = decodeState(new URL(result.url).searchParams.get('data')); |
| 112 | + expect(state.project.connections).toHaveLength(2); |
| 113 | + }); |
| 114 | + |
| 115 | + it('rejects line index out of range', () => { |
| 116 | + const result = buildAlignUrl(ORIGIN, { |
| 117 | + lines: ['a', 'b'], |
| 118 | + alignments: [[0, 0, 5, 0]] |
| 119 | + }); |
| 120 | + expect(result).toMatchObject({ err: expect.stringContaining('lineB=5') }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('rejects non-adjacent lines', () => { |
| 124 | + const result = buildAlignUrl(ORIGIN, { |
| 125 | + lines: ['a', 'b', 'c'], |
| 126 | + alignments: [[0, 0, 2, 0]] |
| 127 | + }); |
| 128 | + expect(result).toMatchObject({ err: expect.stringContaining('not adjacent') }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('rejects word index out of range with helpful message', () => { |
| 132 | + const result = buildAlignUrl(ORIGIN, { |
| 133 | + lines: ['Hello', 'World'], |
| 134 | + alignments: [[0, 5, 1, 0]] |
| 135 | + }); |
| 136 | + expect(result).toMatchObject({ err: expect.stringContaining('word 5 out of range') }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('works with a single line and no alignments', () => { |
| 140 | + const result = buildAlignUrl(ORIGIN, { lines: ['Solo line'] }); |
| 141 | + expect(result).not.toHaveProperty('err'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('handles 8 lines (max)', () => { |
| 145 | + const lines = Array.from({ length: 8 }, (_, i) => `line ${i}`); |
| 146 | + const result = buildAlignUrl(ORIGIN, { lines }); |
| 147 | + expect(result).not.toHaveProperty('err'); |
| 148 | + }); |
| 149 | +}); |
0 commit comments