SmartfolioBackend / scripts /test-activity-logic.mjs
ibrahimgulbutt's picture
deploy: correct salary parsing + backfill endpoint for existing rows
88800df
Raw History Blame Contribute Delete
5.2 kB
// Pure-logic unit tests for activity-gated scraping and its supporting changes.
// Runs against the compiled output in dist/ (no DB, no network, no subprocess).
// Usage: npm run build && node scripts/test-activity-logic.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const norm = require('../dist/modules/scraper/normalize.js');
const ranking = require('../dist/modules/scraper/ranking.js');
let passed = 0;
const test = (name, fn) => {
fn();
passed++;
console.log(` ✓ ${name}`);
};
const job = (over) => norm.normalizeJob({
title: 'Backend Engineer',
company: 'Acme',
location: 'Lahore',
apply_url: 'https://example.com/j/1',
...over,
}, 'adzuna');
console.log('Salary parsing — filters compare these, so a wrong value hides real jobs');
test('a plain numeric string becomes a number', () => {
assert.equal(job({ salary_min: '85000' }).salaryMinValue, 85000);
});
test('a number passes through unchanged', () => {
assert.equal(job({ salary_min: 85000 }).salaryMinValue, 85000);
});
test('currency symbols and separators are stripped', () => {
assert.equal(job({ salary_min: '£45,000' }).salaryMinValue, 45000);
});
test('decimals survive', () => {
assert.equal(job({ salary_min: '1234.56' }).salaryMinValue, 1234.56);
});
test('"Not specified" is unknown, not zero', () => {
// Zero would make an unpaid-looking job satisfy every salary_max filter.
assert.equal(job({ salary_min: 'Not specified' }).salaryMinValue, null);
});
test('a missing field is unknown, not zero', () => {
assert.equal(job({}).salaryMinValue, null);
assert.equal(job({}).salaryMaxValue, null);
});
test('the display strings are left untouched for the API contract', () => {
const j = job({ salary_min: '£45,000' });
assert.equal(j.salaryMin, '£45,000');
assert.equal(j.salaryMinValue, 45000);
});
test('the legacy `salary` alias still feeds the numeric mirror', () => {
assert.equal(job({ salary: '70000' }).salaryMinValue, 70000);
});
console.log('\nUnreadable salaries must be null — a wrong number beats no number only for the board');
test('a range does not concatenate into a fabricated figure', () => {
// Stripping every non-digit turns this into 30130, which then satisfies
// salary filters the job has no business matching.
assert.equal(job({ salary_min: '$30 - $130/hour' }).salaryMinValue, null);
});
test('a "k" suffix is unknown, not its leading digits', () => {
// "USD 50k" naively reads as 50 — far worse than null, since 50 passes
// every "at least" filter a user could set.
assert.equal(job({ salary_min: 'USD 50k' }).salaryMinValue, null);
});
test('malformed decimals are unknown rather than a cast error', () => {
assert.equal(job({ salary_min: '1.2.3' }).salaryMinValue, null);
});
test('prose around a number is not mined for digits', () => {
assert.equal(job({ salary_min: 'competitive, up to 90000 DOE' }).salaryMinValue, null);
});
test('currency symbols and thousands separators still parse', () => {
assert.equal(job({ salary_min: '£45,000' }).salaryMinValue, 45000);
assert.equal(job({ salary_min: '$ 85 000' }).salaryMinValue, 85000);
});
console.log('\nRescoring — a profile edit must actually change the ranking');
const backendJob = job({
title: 'Senior Backend Engineer',
description: 'Node.js, PostgreSQL and Docker.',
scraped_at: new Date().toISOString().split('T')[0],
});
test('the same job scores differently for two different profiles', () => {
const backendProfile = {
targetRole: 'Backend Engineer',
skills: ['node.js', 'postgresql'],
location: 'Lahore',
};
const designProfile = {
targetRole: 'Graphic Designer',
skills: ['figma', 'illustrator'],
location: 'Lahore',
};
const forBackend = ranking.scoreJob(backendJob, backendProfile);
const forDesign = ranking.scoreJob(backendJob, designProfile);
assert.ok(
forBackend > forDesign,
`backend profile should outscore design profile (${forBackend} vs ${forDesign})`,
);
});
test('scoring is deterministic, so rescoring is a no-op when nothing changed', () => {
const profile = { targetRole: 'Backend Engineer', skills: ['node.js'], location: 'Lahore' };
assert.equal(ranking.scoreJob(backendJob, profile), ranking.scoreJob(backendJob, profile));
});
test('changing the target role changes the score — the bug this fixes', () => {
const before = ranking.scoreJob(backendJob, { targetRole: 'Backend Engineer', skills: [] });
const after = ranking.scoreJob(backendJob, { targetRole: 'Marketing Manager', skills: [] });
assert.notEqual(before, after);
});
test('no profile yields zero rather than throwing', () => {
assert.equal(ranking.scoreJob(backendJob, null), 0);
});
test('scores stay inside the documented 0–100 range', () => {
const maximal = ranking.scoreJob(backendJob, {
targetRole: 'Senior Backend Engineer',
skills: ['node.js', 'postgresql', 'docker'],
location: 'Lahore',
careerStage: 'senior',
openToRemote: true,
});
assert.ok(maximal >= 0 && maximal <= 100, `expected 0–100, got ${maximal}`);
});
console.log(`\n${passed} tests passed`);