|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { chunkify_stylesheet, type ChunkedCoverage } from './chunkify' |
| 3 | + |
| 4 | +test('creates chunks with outer chunks covered', () => { |
| 5 | + let coverage = { |
| 6 | + text: 'a { color: red; } b { color: green; } c { color: blue; }', |
| 7 | + ranges: [ |
| 8 | + { start: 0, end: 17 }, |
| 9 | + { start: 38, end: 56 }, |
| 10 | + ], |
| 11 | + url: 'https://example.com', |
| 12 | + } |
| 13 | + let result = chunkify_stylesheet(coverage) |
| 14 | + expect(result).toEqual({ |
| 15 | + ...coverage, |
| 16 | + chunks: [ |
| 17 | + { |
| 18 | + start_offset: 0, |
| 19 | + end_offset: 17, |
| 20 | + is_covered: true, |
| 21 | + }, |
| 22 | + { |
| 23 | + start_offset: 17, |
| 24 | + end_offset: 38, |
| 25 | + is_covered: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + start_offset: 38, |
| 29 | + end_offset: 56, |
| 30 | + is_covered: true, |
| 31 | + }, |
| 32 | + ], |
| 33 | + } satisfies ChunkedCoverage) |
| 34 | +}) |
| 35 | + |
| 36 | +test('creates chunks with only middle chunk covered', () => { |
| 37 | + let coverage = { |
| 38 | + text: 'a { color: red; } b { color: green; } c { color: blue; }', |
| 39 | + ranges: [{ start: 17, end: 38 }], |
| 40 | + url: 'https://example.com', |
| 41 | + } |
| 42 | + let result = chunkify_stylesheet(coverage) |
| 43 | + expect(result).toEqual({ |
| 44 | + ...coverage, |
| 45 | + chunks: [ |
| 46 | + { |
| 47 | + start_offset: 0, |
| 48 | + end_offset: 17, |
| 49 | + is_covered: false, |
| 50 | + }, |
| 51 | + { |
| 52 | + start_offset: 17, |
| 53 | + end_offset: 38, |
| 54 | + is_covered: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + start_offset: 38, |
| 58 | + end_offset: 56, |
| 59 | + is_covered: false, |
| 60 | + }, |
| 61 | + ], |
| 62 | + } satisfies ChunkedCoverage) |
| 63 | +}) |
| 64 | + |
| 65 | +test('creates a single chunk when all is covered', () => { |
| 66 | + let coverage = { |
| 67 | + text: 'a { color: red; } b { color: green; } c { color: blue; }', |
| 68 | + ranges: [{ start: 0, end: 56 }], |
| 69 | + url: 'https://example.com', |
| 70 | + } |
| 71 | + let result = chunkify_stylesheet(coverage) |
| 72 | + expect(result).toEqual({ |
| 73 | + ...coverage, |
| 74 | + chunks: [ |
| 75 | + { |
| 76 | + start_offset: 0, |
| 77 | + end_offset: 56, |
| 78 | + is_covered: true, |
| 79 | + }, |
| 80 | + ], |
| 81 | + } satisfies ChunkedCoverage) |
| 82 | +}) |
| 83 | + |
| 84 | +test('creates a single chunk when none is covered', () => { |
| 85 | + let coverage = { |
| 86 | + text: 'a { color: red; } b { color: green; } c { color: blue; }', |
| 87 | + ranges: [], |
| 88 | + url: 'https://example.com', |
| 89 | + } |
| 90 | + let result = chunkify_stylesheet(coverage) |
| 91 | + expect(result).toEqual({ |
| 92 | + ...coverage, |
| 93 | + chunks: [ |
| 94 | + { |
| 95 | + start_offset: 0, |
| 96 | + end_offset: 56, |
| 97 | + is_covered: false, |
| 98 | + }, |
| 99 | + ], |
| 100 | + } satisfies ChunkedCoverage) |
| 101 | +}) |
0 commit comments