test: add parse_csv.test.ts and parse_file.test.ts

This commit is contained in:
Mara 2025-02-08 10:10:09 +01:00
parent 440d08b9ba
commit 54dc23840c
2 changed files with 145 additions and 0 deletions

85
tests/parse_csv.test.ts Normal file
View file

@ -0,0 +1,85 @@
import { describe, expect, it } from "bun:test";
import type { ColumnName, Separator, Translation } from "../src/interfaces";
import { getThesaurus } from "../src/utils";
import "uniformize";
const mockTranslation = ((key: string, options?: any) => {
const translations: { [key: string]: string } = {
"error.csv.columns": "Invalid column names",
"error.csv.separator": `Invalid separator: ${options?.sep}`,
"error.csv.header": `Invalid header length: ${options?.len}`,
"error.csv.malformed": `Malformed line with length: ${options?.len}`,
};
return translations[key] || key;
}) as Translation;
describe("getThesaurus", () => {
const columnNames: ColumnName = { term: "Term", synonyms: "Synonyms" };
it("should parse a valid CSV content", () => {
const csvContent = "Term,Synonyms\nword1,synonym1\nword2,synonym2";
const separator: Separator = ",";
const thesaurus = getThesaurus(csvContent, separator, mockTranslation, columnNames);
expect(thesaurus).toEqual({
word1: new Set(["synonym1"]),
word2: new Set(["synonym2"]),
});
});
it("should throw an error for invalid column names", () => {
const csvContent = "Invalid,Header\nword1,synonym1";
const separator: Separator = ",";
expect(() =>
getThesaurus(csvContent, separator, mockTranslation, columnNames)
).toThrow("Invalid column names");
});
it("should throw an error for invalid separator", () => {
const csvContent = "Term;Synonyms\nword1;synonym1";
const separator: Separator = ",";
expect(() =>
getThesaurus(csvContent, separator, mockTranslation, columnNames)
).toThrow("Invalid separator: ;");
});
it("should throw an error for invalid header length", () => {
const csvContent = "Term,Synonyms,Extra\nword1,synonym1";
const separator: Separator = ",";
expect(() =>
getThesaurus(csvContent, separator, mockTranslation, columnNames)
).toThrow("Invalid header length: 3");
});
it("should throw an error for malformed line", () => {
const csvContent = "Term,Synonyms\nword1,synonym1\nword2";
const separator: Separator = ",";
expect(() =>
getThesaurus(csvContent, separator, mockTranslation, columnNames)
).toThrow("Malformed line with length: 1");
});
it("should not have duplicate terms in the result", () => {
const csvContent = "Term,Synonyms\nword1,synonym1\nword1,synonym2";
const separator: Separator = ",";
const thesaurus = getThesaurus(csvContent, separator, mockTranslation, columnNames);
expect(Object.keys(thesaurus).length).toBe(1);
expect(thesaurus["word1"]).toEqual(new Set(["synonym1", "synonym2"]));
});
it("should not have duplicate synonyms in the result", () => {
const csvContent = "Term,Synonyms\nword1,synonym1\nword1,synonym1";
const separator: Separator = ",";
const thesaurus = getThesaurus(csvContent, separator, mockTranslation, columnNames);
expect(Object.keys(thesaurus).length).toBe(1);
expect(thesaurus["word1"]).toEqual(new Set(["synonym1"]));
});
it("should not have duplicate terms and synonyms in the result", () => {
const csvContent = "Term,Synonyms\nword1,synonym1\nword2,synonym1";
const separator: Separator = ",";
const thesaurus = getThesaurus(csvContent, separator, mockTranslation, columnNames);
expect(Object.keys(thesaurus).length).toBe(2);
expect(thesaurus["word1"]).toEqual(new Set(["synonym1"]));
expect(thesaurus["word2"]).toEqual(new Set(["synonym1"]));
});
});

60
tests/parse_file.test.ts Normal file
View file

@ -0,0 +1,60 @@
import { describe, expect, it } from "bun:test";
import type { Thesaurus } from "../src/interfaces";
import { getTags } from "../src/utils";
describe("getTags", () => {
it("should return an empty array if no tags are found", () => {
const content = "This is a test content without any tags.";
const thesaurus: Thesaurus = {
tag1: new Set(["synonym1", "synonym2"]),
tag2: new Set(["synonym3", "synonym4"]),
};
const result = getTags(content, thesaurus);
expect(result).toEqual([]);
});
it("should return tags if synonyms are found in the content", () => {
const content = "This content contains synonym1 and synonym4.";
const thesaurus: Thesaurus = {
tag1: new Set(["synonym1", "synonym2"]),
tag2: new Set(["synonym3", "synonym4"]),
};
const result = getTags(content, thesaurus);
expect(result).toEqual(["tag1", "tag2"]);
});
it("should handle case insensitive matches", () => {
const content = "This content contains Synonym1 and SYNONYM4.";
const thesaurus: Thesaurus = {
tag1: new Set(["synonym1", "synonym2"]),
tag2: new Set(["synonym3", "synonym4"]),
};
const result = getTags(content, thesaurus);
expect(result).toEqual(["tag1", "tag2"]);
});
it("should not return duplicate tags", () => {
const content = "This content contains synonym1 and synonym1 again.";
const thesaurus: Thesaurus = {
tag1: new Set(["synonym1", "synonym2"]),
tag2: new Set(["synonym3", "synonym4"]),
};
const result = getTags(content, thesaurus);
expect(result).toEqual(["tag1"]);
});
it("should return the two tags related to the synonyms", () => {
const content = "This content contains synonym4.";
const thesaurus: Thesaurus = {
tag1: new Set(["synonym1", "synonym4"]),
tag2: new Set(["synonym3", "synonym4"]),
};
const result = getTags(content, thesaurus);
expect(result).toEqual(["tag1", "tag2"]);
});
});