105 lines
3.5 KiB
JavaScript
105 lines
3.5 KiB
JavaScript
import { test, expect } from 'bun:test';
|
|
import { readFileSync } from 'fs';
|
|
import { join } from 'path';
|
|
import { DOMParser } from '../setup.js';
|
|
import {
|
|
hasFlightData,
|
|
getRawFlightData,
|
|
decodeRawFlightData,
|
|
parseDecodedRawFlightData,
|
|
getFlightData
|
|
} from '../../parser/flight_data.js';
|
|
import { RSCPayload, Text, Module } from '../../parser/types.js';
|
|
|
|
function loadTestHTML(filename) {
|
|
const path = join(process.cwd(), '..', 'test', 'src', filename);
|
|
return readFileSync(path, 'utf-8');
|
|
}
|
|
|
|
test('hasFlightData detects flight data in nextjs.org.html', () => {
|
|
const html = loadTestHTML('nextjs.org.html');
|
|
expect(hasFlightData(html, DOMParser)).toBe(true);
|
|
});
|
|
|
|
test('hasFlightData returns false for x.com.html (no NextJS)', () => {
|
|
const html = loadTestHTML('x.com.html');
|
|
expect(hasFlightData(html, DOMParser)).toBe(false);
|
|
});
|
|
|
|
test('hasFlightData returns false for empty HTML', () => {
|
|
const html = '<html><body>No flight data</body></html>';
|
|
expect(hasFlightData(html, DOMParser)).toBe(false);
|
|
});
|
|
|
|
test('getRawFlightData extracts data from nextjs.org.html', () => {
|
|
const html = loadTestHTML('nextjs.org.html');
|
|
const raw = getRawFlightData(html, DOMParser);
|
|
expect(raw).not.toBeNull();
|
|
expect(Array.isArray(raw)).toBe(true);
|
|
expect(raw.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
test('getRawFlightData returns null for no flight data', () => {
|
|
const html = '<html><body>No flight data</body></html>';
|
|
const raw = getRawFlightData(html, DOMParser);
|
|
expect(raw).toBeNull();
|
|
});
|
|
|
|
test('decodeRawFlightData handles bootstrap segment', () => {
|
|
const raw = [[0], [1, 'test data']];
|
|
const decoded = decodeRawFlightData(raw);
|
|
expect(decoded).toEqual(['test data']);
|
|
});
|
|
|
|
test('decodeRawFlightData handles binary segment', () => {
|
|
const raw = [[0], [3, btoa('binary data')]];
|
|
const decoded = decodeRawFlightData(raw);
|
|
expect(decoded).toEqual(['binary data']);
|
|
});
|
|
|
|
test('decodeRawFlightData throws on unknown segment type', () => {
|
|
const raw = [[0], [99, 'data']];
|
|
expect(() => decodeRawFlightData(raw)).toThrow('Unknown segment type');
|
|
});
|
|
|
|
test('decodeRawFlightData throws on non-bootstrap first', () => {
|
|
const raw = [[1, 'data']];
|
|
expect(() => decodeRawFlightData(raw)).toThrow('initialServerDataBuffer');
|
|
});
|
|
|
|
test('parseDecodedRawFlightData parses simple data', () => {
|
|
const decoded = ['2:{"test":"value"}'];
|
|
const parsed = parseDecodedRawFlightData(decoded);
|
|
expect(parsed[2]).toBeDefined();
|
|
});
|
|
|
|
test('parseDecodedRawFlightData parses text element', () => {
|
|
const decoded = ['1:T5,hello'];
|
|
const parsed = parseDecodedRawFlightData(decoded);
|
|
expect(parsed[1]).toBeInstanceOf(Text);
|
|
expect(parsed[1].text).toBe('hello');
|
|
});
|
|
|
|
test('getFlightData returns parsed data from nextjs.org.html', () => {
|
|
const html = loadTestHTML('nextjs.org.html');
|
|
const flightData = getFlightData(html, DOMParser);
|
|
expect(flightData).not.toBeNull();
|
|
expect(typeof flightData).toBe('object');
|
|
|
|
// Check for RSCPayload at index 0
|
|
expect(flightData[0]).toBeInstanceOf(RSCPayload);
|
|
expect(typeof flightData[0].build_id).toBe('string');
|
|
});
|
|
|
|
test('getFlightData returns null for no flight data', () => {
|
|
const html = '<html><body>No flight data</body></html>';
|
|
const flightData = getFlightData(html, DOMParser);
|
|
expect(flightData).toBeNull();
|
|
});
|
|
|
|
test('getFlightData parses mintstars.com.html', () => {
|
|
const html = loadTestHTML('mintstars.com.html');
|
|
const flightData = getFlightData(html, DOMParser);
|
|
expect(flightData).not.toBeNull();
|
|
expect(typeof flightData).toBe('object');
|
|
});
|