// Unit checks for the pure rules: the link-check colours (linkcheck.verdict), planting validation, ID cleaning and filling. // node qa/unit.test.cjs 'use strict'; const path = require('path'); const os = require('os'); const fs = require('fs'); process.env.DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'ypb-unit-')); const lc = require('../linkcheck'); const sb = require('../seedbed'); let pass = 0, fail = 0; const ok = (c, what, x) => { if (c) { pass++; console.log(' ok ' + what); } else { fail++; console.log(' FAIL ' + what + (x !== undefined ? ' -> ' + JSON.stringify(x) : '')); } }; console.log('-- link check verdicts'); ok(lc.verdict([], { ok: true, status: 200, host: 'a.com', moved: false }).state === 'green', '200 on the same site, no flags: green'); ok(lc.verdict(['spam words'], { ok: true, status: 200 }).state === 'yellow', 'flags: yellow'); ok(lc.verdict([], { ok: true, status: 200, moved: true, host: 'b.com' }).state === 'yellow', 'redirect to another site: yellow'); ok(lc.verdict([], { ok: true, http: true }).state === 'yellow', 'plain http: yellow'); ok(lc.verdict([], { ok: true, status: 403, wall: true }).state === 'yellow', '403 bot wall: yellow'); ok(lc.verdict([], { ok: false, why: 'answered 404', hard: true }).state === 'red', '404 on planting: red'); ok(lc.verdict([], { ok: false, why: 'could not be reached (timeout)' }, null).state === 'red', 'unreachable on planting: red'); { const v1 = lc.verdict([], { ok: false, why: 'timeout' }, { state: 'green', misses: 0 }); const v2 = lc.verdict([], { ok: false, why: 'timeout' }, v1); ok(v1.state === 'yellow' && v1.misses === 1 && v2.state === 'red', 'a green packet: one failed recheck yellow, the second red', [v1, v2]); } ok(lc.verdict([], { ok: false, why: 'answered 410', hard: true }, { state: 'green' }).state === 'red', 'a gone page on recheck: red at once'); ok(lc.verdict([], { skipped: true }).state === 'green' && lc.verdict(['x'], { skipped: true }).state === 'yellow', 'probe off: flags only'); ok(lc.sameSite('www.a.com'.replace(/^www\./, ''), 'a.com') && lc.sameSite('go.a.com', 'a.com') && !lc.sameSite('a.com', 'b.com'), 'same-site rule'); ok(lc.sample('https://x.com/?r={id}') === 'https://x.com/?r=ypbcheck', 'the probe uses a sample ID'); console.log('-- planting validation'); const base = { name: 'Good Program', url: 'https://good.example.net/?ref={id}', myId: 'me1', desc: 'A program that does a useful thing.', cost: 'free' }; let v = sb.validatePlant(base); ok(v.ok && v.fields.url === 'https://good.example.net/?ref={id}' && v.fields.ownerId === 'me1' && v.flags.length === 0, 'a good packet', v); v = sb.validatePlant(Object.assign({}, base, { url: 'https://good.example.net/?ref={affiliateid}' })); ok(v.ok && v.fields.url.endsWith('{id}'), '{affiliateid} is accepted and normalised to {id}', v); v = sb.validatePlant(Object.assign({}, base, { url: 'https://{id}.good.example.net/' })); ok(v.error && /domain/.test(v.error), '{id} in the domain refused', v); v = sb.validatePlant(Object.assign({}, base, { desc: 'short' })); ok(v.field === 'desc', 'too short a description'); v = sb.validatePlant(Object.assign({}, base, { cost: 'maybe' })); ok(v.field === 'cost', 'cost must be free or paid'); v = sb.validatePlant(Object.assign({}, base, { desc: 'Earn passive income with guaranteed returns daily.' })); ok(v.ok && v.flags.includes('income claim words'), 'income words flagged', v.flags); v = sb.validatePlant(Object.assign({}, base, { desc: 'See www.other.com/page for more about it.' })); ok(v.ok && v.flags.includes('link in description'), 'a link in the description flagged'); v = sb.validatePlant(Object.assign({}, base, { url: 'http://good.example.net/?ref={id}' })); ok(v.ok && v.flags.includes('not https'), 'http flagged'); v = sb.validatePlant(Object.assign({}, base, { name: 'X' })); ok(v.field === 'name', 'name too short'); v = sb.validatePlant(Object.assign({}, base, { myId: '' })); ok(v.field === 'myId', 'the planter ID is required'); v = sb.validatePlant(Object.assign({}, base, { url: 'https://stackpol.com/r/{id}' })); ok(v.error && /network site/.test(v.error), 'a network site refused'); v = sb.validatePlant(Object.assign({}, base, { url: 'https://rmcircle.team/{id}' })); ok(!!v.error, 'RM Circle refused (never in the network rows)'); v = sb.validatePlant(Object.assign({}, base, { contract: '0x' + 'A'.repeat(40) })); ok(v.ok && v.fields.contract === '0x' + 'a'.repeat(40), 'contract address lowercased'); ok(sb.cats().length >= 8 && !sb.cats().some(c => c.id === '12'), 'categories from the builder (empty faucets category left out)'); console.log('-- IDs'); const p = { url: 'https://good.example.net/?ref={id}' }; ok(sb.cleanId(p, ' jane ') === 'jane', 'an ID is trimmed'); ok(sb.cleanId(p, 'https://www.good.example.net/?ref=jane99') === 'jane99', 'a pasted whole link gives the ID'); ok(sb.cleanId(p, '') === '', 'empty = remove'); ok(sb.cleanId(p, 'bad id!') === null, 'spaces and ! refused'); ok(sb.fill(p, 'a b') === 'https://good.example.net/?ref=a%20b', 'fill encodes the ID'); ok(sb.homeUrl(p) === 'https://good.example.net/', 'the plain home page'); ok(sb.textHash({ name: 'a', url: 'b' }) !== sb.textHash({ name: 'a', url: 'c' }), 'text hash changes with the text'); console.log('\n' + pass + ' passed, ' + fail + ' failed'); process.exit(fail ? 1 : 0);