AI ad writer on the text-ad form (Marty 2026-11-17: "do it on the other sites too"): "Write my ad from my link" asks the network hub (hubadwriter.js, POST /v1/adwriter, 60/90 limits) for 3 ads from the member's own page; shown only when the hub says on; chatbot fact; my.js?v=9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// The network ad writer client (2026-11-17): node qa/hubadwriter.test.mjs
|
||||
// A stand-in hub checks the signature and answers like network-hub lib/adwriter.js; no real hub, no model, no member.
|
||||
import http from 'node:http';
|
||||
import crypto from 'node:crypto';
|
||||
import path from 'node:path';
|
||||
import { createRequire } from 'node:module';
|
||||
const require = createRequire(import.meta.url);
|
||||
const ROOT = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..');
|
||||
let pass = 0, fail = 0;
|
||||
const ok = (c, m, extra) => { if (c) pass++; else { fail++; console.log('FAIL', m, extra !== undefined ? JSON.stringify(extra).slice(0, 300) : ''); } };
|
||||
|
||||
const SECRET = 's'.repeat(40); let seen = [], mode = 'good';
|
||||
const hub = http.createServer((q, r) => { let b = ''; q.on('data', c => { b += c; }); q.on('end', () => {
|
||||
const sig = crypto.createHmac('sha256', SECRET).update(q.headers['x-hub-ts'] + '\n' + b).digest('hex');
|
||||
r.setHeader('Content-Type', 'application/json');
|
||||
if (sig !== q.headers['x-hub-sig']) { r.statusCode = 401; return r.end('{"error":"unauthorized"}'); }
|
||||
const j = JSON.parse(b); seen.push({ path: q.url, site: q.headers['x-hub-site'], body: j });
|
||||
if (q.url === '/v1/adwriter/status') return r.end(JSON.stringify(mode === 'off' ? { on: false, daily: 5 } : { on: true, daily: 5, left: j.member ? 3 : undefined }));
|
||||
if (mode === 'limit') { r.statusCode = 429; return r.end(JSON.stringify({ ok: false, error: 'member_limit', code: 'member_limit', message: 'You have used your 5 ad writes for today.', left: 0 })); }
|
||||
if (mode === 'slow') return;
|
||||
r.end(JSON.stringify({ ok: true, options: [{ headline: 'One link for all your pages', line: 'Share one link that rotates your pages.' }], url: 'https://linkspin.app/', cached: false, left: 4 }));
|
||||
}); });
|
||||
await new Promise(r => hub.listen(0, '127.0.0.1', r));
|
||||
process.env.HUB_URL = 'http://127.0.0.1:' + hub.address().port; process.env.HUB_SECRET = SECRET; delete process.env.HUB_SITE; delete process.env.AI_WRITER;
|
||||
const aw = require(path.join(ROOT, 'hubadwriter.js'));
|
||||
|
||||
ok(await aw.enabled(), 'on when the hub says on');
|
||||
ok(seen[0].path === '/v1/adwriter/status' && seen[0].site && seen[0].site !== '__SITE__', 'signed as this site', seen[0]);
|
||||
let r = await aw.write('member-1', 'https://linkspin.app', { headline: 60, line: 90 });
|
||||
ok(r.ok && r.options.length === 1 && r.left === 4, 'options passed on', r);
|
||||
ok(seen[1].body.member === 'member-1' && seen[1].body.limits.headline === 60 && seen[1].body.limits.line === 90 && seen[1].body.url === 'https://linkspin.app', 'member, url and this site\'s limits sent', seen[1].body);
|
||||
mode = 'limit'; r = await aw.write('member-1', 'https://linkspin.app', { headline: 60, line: 90 });
|
||||
ok(r.error === 'You have used your 5 ad writes for today.' && r.status === 429 && r.left === 0, 'the hub message is shown as is', r);
|
||||
const st = await aw.status('member-1'); ok(st.on && st.left === 3, 'status for a member (left today)', st);
|
||||
mode = 'off'; aw._reset(); ok(!(await aw.enabled()), 'off when the hub says off');
|
||||
process.env.HUB_SECRET = 'w'.repeat(40); mode = 'good'; r = await aw.write('member-1', 'https://linkspin.app', { headline: 60, line: 90 });
|
||||
ok(r.code === 'hub' && /busy/.test(r.error), 'a refused signature: a friendly busy message', r);
|
||||
aw._reset(); ok(!(await aw.enabled()), 'a refused signature hides the button');
|
||||
process.env.HUB_SECRET = SECRET; process.env.AI_WRITER = '0'; aw._reset(); const n0 = seen.length; ok(!(await aw.enabled()) && seen.length === n0, 'AI_WRITER=0 hides it without asking the hub', seen.length);
|
||||
delete process.env.AI_WRITER; process.env.HUB_URL = 'http://127.0.0.1:1'; aw._reset(); ok(!(await aw.enabled()), 'an unreachable hub hides the button');
|
||||
hub.close();
|
||||
console.log('hubadwriter: ' + pass + ' passed, ' + fail + ' failed');
|
||||
process.exit(fail ? 1 : 0);
|
||||
+2
-1
@@ -49,6 +49,7 @@ listen(HUB_PORT, async (q, r, b) => {
|
||||
else if (j.email) p = all.find(x => x.email === j.email);
|
||||
return r.end(JSON.stringify({ person: hubView(p) }));
|
||||
}
|
||||
if (String(q.url).startsWith('/v1/adwriter')) return r.end(JSON.stringify({ on: true, daily: 5, left: 5 })); // the ad writer (hubadwriter.js; its own test covers the calls)
|
||||
HUB.got.push({ route: q.url, body: j }); r.end('{"ok":true}');
|
||||
});
|
||||
// ---- stand-in InstantAdPay member lookup (only used while the hub is down) ----
|
||||
@@ -476,7 +477,7 @@ const mtop = await anon.json('/api/wire?page=member&u=topdog');
|
||||
pa = await M1.json('/api/my/ad', adBody(1, { link: 'https://member-1.example-shop.com/third', track: true }));
|
||||
my = await M1.json('/api/my'); ok(pa.ok && pa.linkspin === 'tracked' && my.ad.spin === false && my.linkspin.state === 'tracked', 'back to an ordinary link: tracked with LinkSpin again', [pa, my.linkspin]);
|
||||
const h = await (await anon.req('/my')).text(); const js = await (await anon.req('/assets/my.js?v=7')).text();
|
||||
ok(/id="spinBtn"[^>]*>Use one of my spin links</.test(h) && /id="trackNote"/.test(h) && /my\.js\?v=8/.test(h) && /\/api\/my\/spins/.test(js), 'the ad form has "Use one of my spin links" and the tracking note'); }
|
||||
ok(/id="spinBtn"[^>]*>Use one of my spin links</.test(h) && /id="trackNote"/.test(h) && /my\.js\?v=9/.test(h) && /id="aiWrite"/.test(h) && /\/api\/my\/spins/.test(js), 'the ad form has "Use one of my spin links" and the tracking note'); }
|
||||
|
||||
console.log('14c. PolHunter mission (front page, How it works)');
|
||||
{ const hr = await anon.req('/'); const hh = await hr.text(); const csp = hr.headers.get('content-security-policy') || '';
|
||||
|
||||
Reference in New Issue
Block a user