Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.
TypeScript ai-agentai-resume-analyzerapplication-trackerapplication-tracking-systematscareerjob-applicationjob-automationjob-huntingjob-searchjob-seeker-toolsjob-trackerjob-trackingjobsearchjobseekersmcpollamaresumeresume-reviewself-hosted
Add more unit tests for utils
Khuram Niaz66b10562bcd8
diff --git a/__tests__/lib-utils.spec.ts b/__tests__/lib-utils.spec.tsnew file mode 100644index 0000000..ffd7cae--- /dev/null+++ b/__tests__/lib-utils.spec.ts@@ -0,0 +1,249 @@+import {+ formatUrl,+ formatElapsedTime,+ combineDateAndTime,+ calculatePercentageDifference,+ getLast7Days,+ getTimestampedFileName,+} from "@/lib/utils";++describe("formatUrl", () => {+ it("prepends https:// to bare domain", () => {+ expect(formatUrl("example.com")).toBe("https://example.com");+ });++ it("leaves http:// URL unchanged", () => {+ expect(formatUrl("http://example.com")).toBe("http://example.com");+ });++ it("leaves https:// URL unchanged", () => {+ expect(formatUrl("https://example.com")).toBe("https://example.com");+ });++ it("handles URL with path", () => {+ expect(formatUrl("example.com/jobs/123")).toBe(+ "https://example.com/jobs/123"+ );+ });++ it("handles URL with query parameters", () => {+ expect(formatUrl("example.com?q=test")).toBe("https://example.com?q=test");+ });++ it("handles URL with subdomain", () => {+ expect(formatUrl("www.example.com")).toBe("https://www.example.com");+ });++ it("handles URL with port", () => {+ expect(formatUrl("example.com:3000")).toBe("https://example.com:3000");+ });+});++describe("formatElapsedTime", () => {+ it("formats seconds only", () => {+ expect(formatElapsedTime(30000)).toBe("0m 30s");+ });++ it("formats minutes and seconds", () => {+ expect(formatElapsedTime(90000)).toBe("1m 30s");+ });++ it("formats hours, minutes, and seconds", () => {+ expect(formatElapsedTime(3661000)).toBe("1h 1m 1s");+ });++ it("formats zero milliseconds", () => {+ expect(formatElapsedTime(0)).toBe("0m 0s");+ });++ it("omits hours when zero", () => {+ expect(formatElapsedTime(60000)).toBe("1m 0s");+ expect(formatElapsedTime(60000)).not.toContain("h");+ });++ it("includes hours when non-zero", () => {+ expect(formatElapsedTime(3600000)).toBe("1h 0m 0s");+ });++ it("formats multiple hours correctly", () => {+ expect(formatElapsedTime(7200000)).toBe("2h 0m 0s");+ });++ it("handles large durations (8 hours)", () => {+ expect(formatElapsedTime(28800000)).toBe("8h 0m 0s");+ });++ it("floors partial seconds", () => {+ expect(formatElapsedTime(1500)).toBe("0m 1s");+ });+});++describe("combineDateAndTime", () => {+ it("combines date and morning time", () => {+ const date = new Date(2025, 0, 15);+ const result = combineDateAndTime(date, "09:30 AM");+ expect(result.getFullYear()).toBe(2025);+ expect(result.getMonth()).toBe(0);+ expect(result.getDate()).toBe(15);+ expect(result.getHours()).toBe(9);+ expect(result.getMinutes()).toBe(30);+ });++ it("combines date and afternoon time", () => {+ const date = new Date(2025, 5, 20);+ const result = combineDateAndTime(date, "02:45 PM");+ expect(result.getHours()).toBe(14);+ expect(result.getMinutes()).toBe(45);+ });++ it("handles 12:00 AM (midnight)", () => {+ const date = new Date(2025, 0, 1);+ const result = combineDateAndTime(date, "12:00 AM");+ expect(result.getHours()).toBe(0);+ expect(result.getMinutes()).toBe(0);+ });++ it("handles 12:00 PM (noon)", () => {+ const date = new Date(2025, 0, 1);+ const result = combineDateAndTime(date, "12:00 PM");+ expect(result.getHours()).toBe(12);+ expect(result.getMinutes()).toBe(0);+ });++ it("preserves the date portion", () => {+ const date = new Date(2025, 11, 31);+ const result = combineDateAndTime(date, "11:59 PM");+ expect(result.getFullYear()).toBe(2025);+ expect(result.getMonth()).toBe(11);+ expect(result.getDate()).toBe(31);+ });+});++describe("calculatePercentageDifference", () => {+ it("returns 0 when both values are 0", () => {+ expect(calculatePercentageDifference(0, 0)).toBe(0);+ });++ it("returns 100 when first value is 0 and second is positive", () => {+ expect(calculatePercentageDifference(0, 5)).toBe(100);+ });++ it("returns 100 when first value is 0 and second is negative", () => {+ expect(calculatePercentageDifference(0, -5)).toBe(100);+ });++ it("calculates positive percentage increase", () => {+ expect(calculatePercentageDifference(10, 15)).toBe(50);+ });++ it("calculates negative percentage decrease", () => {+ expect(calculatePercentageDifference(10, 5)).toBe(-50);+ });++ it("returns 0 when both values are equal", () => {+ expect(calculatePercentageDifference(10, 10)).toBe(0);+ });++ it("returns 100 for doubling", () => {+ expect(calculatePercentageDifference(5, 10)).toBe(100);+ });++ it("returns -100 for going to zero", () => {+ expect(calculatePercentageDifference(10, 0)).toBe(-100);+ });++ it("rounds to nearest integer", () => {+ expect(calculatePercentageDifference(3, 4)).toBe(33);+ });++ it("handles negative base value", () => {+ const result = calculatePercentageDifference(-10, -5);+ expect(result).toBe(50);+ });+});++describe("getLast7Days", () => {+ it("returns 7 dates", () => {+ const result = getLast7Days();+ expect(result).toHaveLength(7);+ });++ it("returns dates in chronological order using a base date", () => {+ const baseDate = new Date(2025, 0, 10);+ const result = getLast7Days("yyyy-MM-dd", baseDate);+ expect(result).toEqual([+ "2025-01-04",+ "2025-01-05",+ "2025-01-06",+ "2025-01-07",+ "2025-01-08",+ "2025-01-09",+ "2025-01-10",+ ]);+ });++ it("last date is the base date", () => {+ const baseDate = new Date(2025, 0, 15);+ const result = getLast7Days("yyyy-MM-dd", baseDate);+ expect(result[6]).toBe("2025-01-15");+ });++ it("first date is 6 days before the base date", () => {+ const baseDate = new Date(2025, 0, 15);+ const result = getLast7Days("yyyy-MM-dd", baseDate);+ expect(result[0]).toBe("2025-01-09");+ });++ it("handles month boundary", () => {+ const baseDate = new Date(2025, 1, 2);+ const result = getLast7Days("yyyy-MM-dd", baseDate);+ expect(result[0]).toBe("2025-01-27");+ expect(result[6]).toBe("2025-02-02");+ });++ it("handles year boundary", () => {+ const baseDate = new Date(2025, 0, 3);+ const result = getLast7Days("yyyy-MM-dd", baseDate);+ expect(result[0]).toBe("2024-12-28");+ expect(result[6]).toBe("2025-01-03");+ });++ it("uses custom date format", () => {+ const baseDate = new Date(2025, 0, 10);+ const result = getLast7Days("MMM d", baseDate);+ expect(result[6]).toBe("Jan 10");+ });+});++describe("getTimestampedFileName", () => {+ it("appends timestamp to filename", () => {+ const result = getTimestampedFileName("resume.pdf");+ expect(result).toMatch(/^resume_\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}\.pdf$/);+ });++ it("preserves the file extension", () => {+ const result = getTimestampedFileName("document.docx");+ expect(result).toMatch(/\.docx$/);+ });++ it("preserves the base name", () => {+ const result = getTimestampedFileName("my-resume.pdf");+ expect(result).toMatch(/^my-resume_/);+ });++ it("handles files with multiple dots", () => {+ const result = getTimestampedFileName("file.name.txt");+ expect(result).toMatch(/\.txt$/);+ });++ it("replaces colons with dashes in timestamp for file system compatibility", () => {+ const result = getTimestampedFileName("test.pdf");+ expect(result).not.toContain(":");+ });++ it("removes milliseconds from timestamp", () => {+ const result = getTimestampedFileName("test.pdf");+ const timestampPart = result.replace("test_", "").replace(".pdf", "");+ expect(timestampPart).not.toContain(".");+ });+});diff --git a/__tests__/scraper-utils.spec.ts b/__tests__/scraper-utils.spec.tsnew file mode 100644index 0000000..ef21243--- /dev/null+++ b/__tests__/scraper-utils.spec.ts@@ -0,0 +1,221 @@+import {+ normalizeJobUrl,+ normalizeForSearch,+ extractKeywords,+ extractCityName,+} from "@/lib/scraper/utils";++describe("normalizeJobUrl", () => {+ it("removes utm_source parameter", () => {+ const url = "https://example.com/job?utm_source=google&id=123";+ expect(normalizeJobUrl(url)).toBe("https://example.com/job?id=123");+ });++ it("removes all UTM parameters", () => {+ const url =+ "https://example.com/job?utm_source=x&utm_medium=y&utm_campaign=z&utm_term=a&utm_content=b&id=1";+ expect(normalizeJobUrl(url)).toBe("https://example.com/job?id=1");+ });++ it("removes fbclid parameter", () => {+ const url = "https://example.com/job?fbclid=abc123&title=dev";+ expect(normalizeJobUrl(url)).toBe("https://example.com/job?title=dev");+ });++ it("removes gclid parameter", () => {+ const url = "https://example.com/job?gclid=xyz&role=swe";+ expect(normalizeJobUrl(url)).toBe("https://example.com/job?role=swe");+ });++ it("removes msclkid parameter", () => {+ const url = "https://example.com/job?msclkid=abc&id=5";+ expect(normalizeJobUrl(url)).toBe("https://example.com/job?id=5");+ });++ it("removes ref, source, tk, from, vjk parameters", () => {+ const url =+ "https://example.com/job?ref=email&source=board&tk=abc&from=search&vjk=xyz&id=1";+ expect(normalizeJobUrl(url)).toBe("https://example.com/job?id=1");+ });++ it("returns clean URL when all params are tracking", () => {+ const url = "https://example.com/job?utm_source=google&fbclid=abc";+ expect(normalizeJobUrl(url)).toBe("https://example.com/job");+ });++ it("preserves non-tracking parameters", () => {+ const url = "https://example.com/job?id=123&position=dev";+ expect(normalizeJobUrl(url)).toBe(+ "https://example.com/job?id=123&position=dev"+ );+ });++ it("preserves URL path and hash", () => {+ const url = "https://example.com/jobs/123#details?utm_source=x";+ const result = normalizeJobUrl(url);+ expect(result).toContain("/jobs/123");+ });++ it("returns original string for invalid URLs", () => {+ const invalid = "not-a-valid-url";+ expect(normalizeJobUrl(invalid)).toBe(invalid);+ });++ it("handles URL with no query parameters", () => {+ const url = "https://example.com/job/123";+ expect(normalizeJobUrl(url)).toBe("https://example.com/job/123");+ });+});++describe("normalizeForSearch", () => {+ it("converts to lowercase", () => {+ expect(normalizeForSearch("Software Engineer")).toBe("software-engineer");+ });++ it("trims whitespace", () => {+ expect(normalizeForSearch(" hello ")).toBe("hello");+ });++ it("replaces spaces with dashes", () => {+ expect(normalizeForSearch("full stack developer")).toBe(+ "full-stack-developer"+ );+ });++ it("removes special characters", () => {+ expect(normalizeForSearch("C++ Developer (Senior)")).toBe(+ "c-developer-senior"+ );+ });++ it("collapses multiple spaces into single dash", () => {+ expect(normalizeForSearch("word1 word2")).toBe("word1-word2");+ });++ it("handles empty string", () => {+ expect(normalizeForSearch("")).toBe("");+ });++ it("preserves numbers", () => {+ expect(normalizeForSearch("Level 3 Support")).toBe("level-3-support");+ });++ it("removes punctuation", () => {+ expect(normalizeForSearch("Sr. Engineer, Backend")).toBe(+ "sr-engineer-backend"+ );+ });+});++describe("extractKeywords", () => {+ it("extracts words longer than 2 characters", () => {+ const result = extractKeywords("Software Developer");+ expect(result).toEqual(["software", "developer"]);+ });++ it("filters out words with 2 or fewer characters", () => {+ const result = extractKeywords("I am a dev");+ expect(result).toEqual(["dev"]);+ });++ it("filters out stop words", () => {+ const result = extractKeywords("Senior Software Engineer at the Company");+ expect(result).toEqual(["software", "engineer"]);+ });++ it("splits on spaces", () => {+ expect(extractKeywords("react node python")).toEqual([+ "react",+ "node",+ "python",+ ]);+ });++ it("splits on dashes", () => {+ expect(extractKeywords("full-stack-developer")).toEqual([+ "full",+ "stack",+ "developer",+ ]);+ });++ it("splits on underscores", () => {+ expect(extractKeywords("data_science_role")).toEqual([+ "data",+ "science",+ "role",+ ]);+ });++ it("splits on commas", () => {+ expect(extractKeywords("react,node,python")).toEqual([+ "react",+ "node",+ "python",+ ]);+ });++ it("splits on dots", () => {+ expect(extractKeywords("google.amazon.meta")).toEqual([+ "google",+ "amazon",+ "meta",+ ]);+ });++ it("converts to lowercase", () => {+ expect(extractKeywords("React Node")).toEqual(["react", "node"]);+ });++ it("filters all stop words", () => {+ const stopWords = [+ "senior",+ "jr",+ "junior",+ "sr",+ "inc",+ "llc",+ "ltd",+ "corp",+ "corporation",+ "the",+ "a",+ "an",+ "co",+ "company",+ ];+ stopWords.forEach((word) => {+ expect(extractKeywords(word)).toEqual([]);+ });+ });++ it("returns empty array for empty string", () => {+ expect(extractKeywords("")).toEqual([]);+ });+});++describe("extractCityName", () => {+ it("extracts city from city, state format", () => {+ expect(extractCityName("Calgary, AB")).toBe("calgary");+ });++ it("extracts city from city, state, country format", () => {+ expect(extractCityName("New York, NY, USA")).toBe("new york");+ });++ it("returns lowercase city name", () => {+ expect(extractCityName("TORONTO, ON")).toBe("toronto");+ });++ it("trims whitespace from city name", () => {+ expect(extractCityName(" Vancouver , BC")).toBe("vancouver");+ });++ it("returns the full string (lowercase) when no comma", () => {+ expect(extractCityName("Remote")).toBe("remote");+ });++ it("handles empty string", () => {+ expect(extractCityName("")).toBe("");+ });+});diff --git a/__tests__/text-processing.spec.ts b/__tests__/text-processing.spec.tsnew file mode 100644index 0000000..835fce0--- /dev/null+++ b/__tests__/text-processing.spec.ts@@ -0,0 +1,307 @@+import {+ removeHtmlTags,+ normalizeWhitespace,+ normalizeBullets,+ normalizeHeadings,+ extractMetadata,+ validateText,+} from "@/lib/ai/tools/text-processing";++describe("removeHtmlTags", () => {+ it("returns empty string for undefined input", () => {+ expect(removeHtmlTags(undefined)).toBe("");+ });++ it("returns empty string for empty string input", () => {+ expect(removeHtmlTags("")).toBe("");+ });++ it("converts <li> tags to bullet points", () => {+ const html = "<ul><li>Item 1</li><li>Item 2</li></ul>";+ const result = removeHtmlTags(html);+ expect(result).toContain("• Item 1");+ expect(result).toContain("• Item 2");+ });++ it("converts closing block tags to newlines", () => {+ const html = "<p>Paragraph 1</p><p>Paragraph 2</p>";+ const result = removeHtmlTags(html);+ expect(result).toContain("Paragraph 1");+ expect(result).toContain("Paragraph 2");+ });++ it("converts <br> tags to newlines", () => {+ const html = "Line 1<br>Line 2<br/>Line 3<br />Line 4";+ const result = removeHtmlTags(html);+ expect(result).toContain("Line 1");+ expect(result).toContain("Line 2");+ expect(result).toContain("Line 3");+ expect(result).toContain("Line 4");+ });++ it("strips remaining HTML tags", () => {+ const html = "<strong>Bold</strong> and <em>italic</em> text";+ const result = removeHtmlTags(html);+ expect(result).toBe("Bold and italic text");+ });++ it("collapses multiple whitespace into single space", () => {+ const html = "<div> lots of spaces </div>";+ const result = removeHtmlTags(html);+ expect(result).not.toContain(" ");+ });++ it("handles nested HTML tags", () => {+ const html =+ '<div class="job"><p><strong>Title:</strong> Engineer</p></div>';+ const result = removeHtmlTags(html);+ expect(result).toContain("Title:");+ expect(result).toContain("Engineer");+ expect(result).not.toContain("<");+ });++ it("handles li tags with attributes", () => {+ const html = '<li class="item" id="1">Content</li>';+ const result = removeHtmlTags(html);+ expect(result).toContain("• Content");+ });+});++describe("normalizeWhitespace", () => {+ it("converts Windows line endings to Unix", () => {+ expect(normalizeWhitespace("line1\r\nline2")).toBe("line1\nline2");+ });++ it("converts old Mac line endings to Unix", () => {+ expect(normalizeWhitespace("line1\rline2")).toBe("line1\nline2");+ });++ it("collapses multiple spaces and tabs to single space", () => {+ expect(normalizeWhitespace("word1 \t word2")).toBe("word1 word2");+ });++ it("collapses 3+ consecutive newlines to double newline", () => {+ expect(normalizeWhitespace("a\n\n\n\nb")).toBe("a\n\nb");+ });++ it("preserves double newlines", () => {+ expect(normalizeWhitespace("a\n\nb")).toBe("a\n\nb");+ });++ it("trims leading and trailing whitespace", () => {+ expect(normalizeWhitespace(" hello ")).toBe("hello");+ });++ it("handles mixed line endings", () => {+ const input = "line1\r\nline2\rline3\nline4";+ const result = normalizeWhitespace(input);+ expect(result).toBe("line1\nline2\nline3\nline4");+ });+});++describe("normalizeBullets", () => {+ it("converts various bullet characters to standard bullet", () => {+ const bullets = ["●", "○", "◦", "▪", "▸", "►", "◆", "★", "✦", "✓", "✔", "→", "‣", "⁃"];+ bullets.forEach((bullet) => {+ expect(normalizeBullets(`${bullet} item`)).toBe("• item");+ });+ });++ it("converts dash bullets at start of line", () => {+ expect(normalizeBullets("- item 1\n- item 2")).toBe(+ "• item 1\n• item 2"+ );+ });++ it("converts en-dash and em-dash bullets at start of line", () => {+ expect(normalizeBullets("– item")).toBe("• item");+ expect(normalizeBullets("— item")).toBe("• item");+ });++ it("converts asterisk bullets at start of line", () => {+ expect(normalizeBullets("* item 1\n* item 2")).toBe(+ "• item 1\n• item 2"+ );+ });++ it("does not convert dashes in the middle of text", () => {+ expect(normalizeBullets("well-known framework")).toBe(+ "well-known framework"+ );+ });++ it("does not convert asterisks in the middle of text", () => {+ expect(normalizeBullets("5*3=15")).toBe("5*3=15");+ });+});++describe("normalizeHeadings", () => {+ it("normalizes all-caps headings with surrounding newlines", () => {+ const result = normalizeHeadings("WORK EXPERIENCE");+ expect(result).toContain("\nWORK EXPERIENCE\n");+ });++ it("normalizes headings with trailing colon", () => {+ const result = normalizeHeadings("EDUCATION:");+ expect(result).toContain("\nEDUCATION\n");+ });++ it("normalizes headings with ampersand", () => {+ const result = normalizeHeadings("SKILLS & TOOLS");+ expect(result).toContain("\nSKILLS & TOOLS\n");+ });++ it("does not affect mixed-case text", () => {+ const input = "This is a regular sentence";+ expect(normalizeHeadings(input)).toBe(input);+ });++ it("does not affect single lowercase word", () => {+ const input = "hello";+ expect(normalizeHeadings(input)).toBe(input);+ });++ it("collapses excessive newlines after normalization", () => {+ const input = "SECTION ONE\n\n\n\nSECTION TWO";+ const result = normalizeHeadings(input);+ expect(result).not.toContain("\n\n\n");+ });+});++describe("extractMetadata", () => {+ it("returns correct character count", () => {+ const result = extractMetadata("hello");+ expect(result.characterCount).toBe(5);+ });++ it("returns correct word count", () => {+ const result = extractMetadata("one two three four");+ expect(result.wordCount).toBe(4);+ });++ it("returns correct line count", () => {+ const result = extractMetadata("line1\nline2\nline3");+ expect(result.lineCount).toBe(3);+ });++ it("filters out empty words from count", () => {+ const result = extractMetadata(" spaced out ");+ expect(result.wordCount).toBe(2);+ });++ it("detects email as contact info", () => {+ const result = extractMetadata("Contact me at [email protected]");+ expect(result.hasContactInfo).toBe(true);+ });++ it("detects phone number as contact info", () => {+ const result = extractMetadata("Call me at (403) 555-1234");+ expect(result.hasContactInfo).toBe(true);+ });++ it("detects phone number without parens", () => {+ const result = extractMetadata("Phone: 403-555-1234");+ expect(result.hasContactInfo).toBe(true);+ });++ it("detects phone number with dots", () => {+ const result = extractMetadata("Phone: 403.555.1234");+ expect(result.hasContactInfo).toBe(true);+ });++ it("returns false for no contact info", () => {+ const result = extractMetadata("Just a regular sentence with no contacts");+ expect(result.hasContactInfo).toBe(false);+ });++ it("handles empty string", () => {+ const result = extractMetadata("");+ expect(result.characterCount).toBe(0);+ expect(result.wordCount).toBe(0);+ expect(result.lineCount).toBe(1);+ expect(result.hasContactInfo).toBe(false);+ });+});++describe("validateText", () => {+ const validText = "a".repeat(300);++ it("returns valid for text within bounds", () => {+ const result = validateText(validText);+ expect(result.isValid).toBe(true);+ expect(result.error).toBeUndefined();+ });++ it("returns NO_CONTENT for empty string", () => {+ const result = validateText("");+ expect(result.isValid).toBe(false);+ expect(result.error?.code).toBe("NO_CONTENT");+ });++ it("returns NO_CONTENT for whitespace-only string", () => {+ const result = validateText(" \n\t ");+ expect(result.isValid).toBe(false);+ expect(result.error?.code).toBe("NO_CONTENT");+ });++ it("returns TOO_SHORT when below minimum", () => {+ const result = validateText("short", 200);+ expect(result.isValid).toBe(false);+ expect(result.error?.code).toBe("TOO_SHORT");+ expect(result.error?.details).toEqual({+ characterCount: 5,+ minCharCount: 200,+ });+ });++ it("returns TOO_LONG when above maximum", () => {+ const longText = "a".repeat(60000);+ const result = validateText(longText, 200, 50000);+ expect(result.isValid).toBe(false);+ expect(result.error?.code).toBe("TOO_LONG");+ expect(result.error?.details).toEqual({+ characterCount: 60000,+ maxCharCount: 50000,+ });+ });++ it("returns CORRUPTED for excessive consecutive special characters", () => {+ const corrupted = "normal text " + "!@#$%^&*()".repeat(5) + " more text";+ const result = validateText(corrupted, 10, 50000);+ expect(result.isValid).toBe(false);+ expect(result.error?.code).toBe("CORRUPTED");+ });++ it("allows special characters under the threshold", () => {+ const text = "normal text !@#$% more text " + "a".repeat(200);+ const result = validateText(text, 10, 50000);+ expect(result.isValid).toBe(true);+ });++ it("uses custom context label in error messages", () => {+ const result = validateText("", 200, 50000, "Resume");+ expect(result.error?.message).toContain("Resume");+ });++ it("uses default context label when not provided", () => {+ const result = validateText("");+ expect(result.error?.message).toContain("Content");+ });++ it("respects custom min and max values", () => {+ const result = validateText("hello", 3, 10);+ expect(result.isValid).toBe(true);+ });++ it("exactly at minimum length is valid", () => {+ const text = "a".repeat(200);+ const result = validateText(text, 200);+ expect(result.isValid).toBe(true);+ });++ it("exactly at maximum length is valid", () => {+ const text = "a".repeat(50000);+ const result = validateText(text, 200, 50000);+ expect(result.isValid).toBe(true);+ });+});