test(encodings.spec): add encoding tests and helper function

This commit is contained in:
k-g-a 2025-10-14 14:41:40 +05:00
parent 0733b3ed54
commit 69fd1e6c35
2 changed files with 39 additions and 2 deletions

31
tests/encodings.spec.ts Normal file
View file

@ -0,0 +1,31 @@
import {beforeAll, describe, expect, it} from 'vitest';
import {parseTextFile} from "../src/file/file";
import {readVcfFixture, readVcfFixtureAsBlob} from "./fixtures/fixtures";
describe('parseDifferentEncodings', () => {
let expectedData: string;
beforeAll(() => {
expectedData = readVcfFixture('encoding-utf8.vcf');
})
it('should parse utf-8 vCards as it used to', async () => {
const data = await readVcfFixtureAsBlob('encoding-utf8.vcf');
const result = await parseTextFile(data)
expect(result).toBe(expectedData);
})
it('should parse windows-1251 vCards correctly', async () => {
const data = await readVcfFixtureAsBlob('encoding-win1251.vcf');
const result = await parseTextFile(data)
expect(result).toBe(expectedData);
})
})

View file

@ -1,11 +1,17 @@
import { readFileSync } from 'fs';
import { join, resolve } from 'path';
import {openAsBlob, readFileSync} from 'fs';
import {join, resolve} from 'path';
export function readVcfFixture(fileName: string): string {
const filePath = join(__dirname, fileName);
return readFileSync(filePath, 'utf8');
}
export async function readVcfFixtureAsBlob(fileName: string): Promise<Blob> {
const filePath = join(__dirname, fileName);
return openAsBlob(filePath);
}
export function readFrontmatterFixture(fileName: string): Record<string, any>|undefined {
const fullPath = resolve(__dirname, fileName + '.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires