import { describe, expect, test } from 'bun:test' import { looksLikeGeodata, looksLikeMmdb } from './geo.ts' describe('GEO 文件的形状检查', () => { test('末尾带 MaxMind 标记的通过,纯垃圾不通过', () => { const marker = new Uint8Array([ 0xab, 0xcd, 0xef, ...Array.from('MaxMind.com', (c) => c.charCodeAt(0)), ]) const good = new Uint8Array(4096) good.set(marker, 4000) expect(looksLikeMmdb(good)).toBe(true) expect(looksLikeMmdb(new Uint8Array(4096))).toBe(false) }) test('标记只认末尾 128KB,太靠前算无效', () => { const marker = new Uint8Array([ 0xab, 0xcd, 0xef, ...Array.from('MaxMind.com', (c) => c.charCodeAt(0)), ]) const bytes = new Uint8Array(300 * 1024) bytes.set(marker, 100) expect(looksLikeMmdb(bytes)).toBe(false) }) test('geodata 认首字节 0x0a,HTML 错误页和小文件被挡下', () => { const good = new Uint8Array(2048) good[0] = 0x0a expect(looksLikeGeodata(good)).toBe(true) const html = new Uint8Array(2048) html[0] = '<'.charCodeAt(0) expect(looksLikeGeodata(html)).toBe(false) const tiny = new Uint8Array(10) tiny[0] = 0x0a expect(looksLikeGeodata(tiny)).toBe(false) }) })