openvibers/openvibe.markets testapp.test.js

app.test.js

main 7.8 KB · 118 lines · javascript

1'use strict';
2/**
3 * Markets (plan §L.1): the search over Deals (each price "as of" its time, its store and source, a was-price, "Can I
4 * afford it?" to Finance), the marketplaces opened with the words filled in (new tab, nothing fetched), Deals down or
5 * empty said plainly with the links still there, the JSON API, the home page, noindex on searches, robots and llms.txt.
6 */
7const assert = require('assert');
8const http = require('node:http');
9const { createApp } = require('../server/app');
10const { createDeals } = require('../server/deals');
11const { linksFor, PLACES } = require('../server/elsewhere');
12
13const NOW = Date.parse('2026-11-18T12:00:00Z');
14const OFFERS = [
15 { title: 'Noise cancelling headphones, over-ear', url: 'https://openvibe.deals/d/nc-1', latest_observation: { observed_at: '2026-11-18T09:00:00Z', currency: 'USD', condition: 'new' },
16 feed: { price: { amount: 149.99, currency: 'USD' }, was: 199.99, store: 'Shop Example', media: { thumb: 'https://img.example/nc.jpg' } } },
17 { title: 'Refurbished studio headphones', url: 'https://openvibe.deals/d/st-2', latest_observation: { observed_at: '2026-11-10T09:00:00Z', currency: 'USD', condition: 'refurbished' },
18 feed: { price: { amount: 59, currency: 'USD' }, store: 'Refurb Co' } },
19 { title: 'A deal with no price', url: 'https://openvibe.deals/d/np-3', latest_observation: null, feed: { price: null, store: null } },
20 { title: 'Not a safe link', url: 'javascript:alert(1)', feed: {} },
21];
22
23(async () => {
24 // A stand-in Deals: /api/v1/search answers OFFERS for "headphones", nothing for anything else; "boom" is a 500.
25 let asked = [];
26 const fake = http.createServer((req, res) => {
27 const u = new URL(req.url, 'http://x');
28 asked.push(u.pathname + u.search);
29 const q = u.searchParams.get('q');
30 if (u.pathname !== '/api/v1/search') { res.writeHead(404); return res.end(); }
31 if (q === 'boom') { res.writeHead(500, { 'content-type': 'application/json' }); return res.end('{}'); }
32 const offers = q === 'headphones' ? OFFERS : [];
33 res.writeHead(200, { 'content-type': 'application/json' });
34 res.end(JSON.stringify({ q, total: q === 'headphones' ? 31 : 0, offers }));
35 });
36 await new Promise((r) => fake.listen(0, '127.0.0.1', r));
37 const deals = createDeals({ baseUrl: `http://127.0.0.1:${fake.address().port}` });
38 const server = createApp({ deals, now: () => NOW }).listen(0, '127.0.0.1');
39 await new Promise((r) => server.once('listening', r));
40 const base = `http://127.0.0.1:${server.address().port}`;
41 const get = (p) => fetch(base + p, { redirect: 'manual' }).then(async (r) => ({ status: r.status, headers: r.headers, location: r.headers.get('location'), text: await r.text() }));
42 try {
43 // The marketplaces, with the words in, encoded.
44 const links = linksFor('used road bike & helmet');
45 assert.ok(PLACES.length >= 12, 'at least ten ways in besides Deals');
46 assert.strictEqual(links.find((l) => l.id === 'ebay').url, 'https://www.ebay.com/sch/i.html?_nkw=used%20road%20bike%20%26%20helmet');
47 assert.ok(links.every((l) => /^https:\/\//.test(l.url) && l.url.includes('used%20road%20bike')));
48
49 let r = await get('/');
50 assert.strictEqual(r.status, 200);
51 assert.match(r.text, /Every marketplace, one search/);
52 assert.match(r.text, /<form class="mk-search" method="get" action="\/search"/);
53 assert.match(r.text, /Facebook Marketplace/);
54 assert.match(r.text, /data-ov-trending="own"/, 'a product search keeps its own examples, never the page\'s headings');
55 assert.match(r.headers.get('cache-control'), /public/);
56 assert.match(r.text, /href="\/markets\.css\?v=[0-9a-f]{10}"/, 'our stylesheet by its content: a release is a new address');
57
58 r = await get('/search?q=headphones');
59 assert.strictEqual(r.status, 200);
60 assert.strictEqual(r.headers.get('x-robots-tag'), 'noindex, follow');
61 assert.match(r.text, /31 deals on OpenVibe Deals, the newest 3 here/);
62 assert.match(r.text, /\$149\.99<\/b><s>\$199\.99<\/s><small>as of 3 hours ago<\/small>/, 'a price with its time and the price before');
63 assert.match(r.text, /as of Nov 10, 2026/);
64 assert.match(r.text, /price not stated/);
65 assert.match(r.text, /<li class="mk-card mk-card--text">/, 'a deal without a picture has no empty square');
66 assert.match(r.text, /<img class="mk-card-img" src="https:\/\/img\.example\/nc\.jpg"/);
67 assert.match(r.text, /Shop Example · OpenVibe Deals/);
68 assert.match(r.text, /Refurb Co · OpenVibe Deals · refurbished/);
69 assert.match(r.text, /href="https:\/\/openvibe\.finance\/ahead\?cost=149\.99">Can I afford it\?/);
70 assert.doesNotMatch(r.text, /javascript:alert/, 'only https links');
71 assert.match(r.text, /<a href="https:\/\/www\.ebay\.com\/sch\/i\.html\?_nkw=headphones" target="_blank" rel="noopener noreferrer">/);
72 assert.strictEqual(asked.pop(), '/api/v1/search?q=headphones&limit=24');
73 assert.match(r.text, /href="https:\/\/openvibe\.deals\/watches\?query=headphones">Alert me about new deals for “headphones”/, 'alerts are Deals\' watches');
74
75 r = await get('/search?q=a%20sofa');
76 assert.match(r.text, /No deal on OpenVibe Deals matches these words yet/);
77 assert.match(r.text, /amazon\.com\/s\?k=a%20sofa/, 'the links are there anyway');
78 r = await get('/search?q=boom');
79 assert.match(r.text, /Deals answered 500\. The marketplaces below still work/);
80
81 r = await get('/?q=headphones');
82 assert.deepStrictEqual([r.status, r.location], [302, '/search?q=headphones']);
83 r = await get('/search?q=%20%20');
84 assert.deepStrictEqual([r.status, r.location], [302, '/']);
85
86 r = await get('/api/v1/search?q=headphones');
87 const j = JSON.parse(r.text);
88 assert.strictEqual(j.deals.ok, true);
89 assert.deepStrictEqual(j.deals.results.map((x) => [x.price, x.store]), [[149.99, 'Shop Example'], [59, 'Refurb Co'], [null, null]]);
90 assert.strictEqual(j.elsewhere.length, PLACES.length);
91 assert.strictEqual((await get('/api/v1/search')).status, 422);
92
93 // 2026-11-21: every page crawlable (a search page stays noindex, follow); a sitemap for the home.
94 r = await get('/robots.txt');
95 assert.ok(!/Disallow: \/search/.test(r.text) && /Sitemap: https:\/\/openvibe\.markets\/sitemap\.xml/.test(r.text), r.text);
96 assert.match((await get('/sitemap.xml')).text, /<loc>https:\/\/openvibe\.markets\/<\/loc>/);
97 assert.match((await get('/llms.txt')).text, /OpenVibe\.Markets/);
98 // llms-full.txt (11-28: a 404 to agents): the home page as words, linked from llms.txt.
99 const full = await get('/llms-full.txt');
100 assert.strictEqual(full.status, 200);
101 assert.ok(full.text.startsWith('# OpenVibe.Markets') && full.text.length > 300 && !/<(?:script|style)\b/i.test(full.text), 'words, not markup');
102 assert.match((await get('/llms.txt')).text, /\(https:\/\/openvibe\.markets\/llms-full\.txt\)/);
103 assert.strictEqual((await get('/nope')).status, 404);
104
105 // Deals unreachable: said plainly, the links still work.
106 const down = createApp({ deals: createDeals({ baseUrl: 'http://127.0.0.1:1', timeoutMs: 500 }), now: () => NOW }).listen(0, '127.0.0.1');
107 await new Promise((x) => down.once('listening', x));
108 const d = await fetch(`http://127.0.0.1:${down.address().port}/search?q=headphones`).then((x) => x.text());
109 assert.match(d, /Deals could not be reached\. The marketplaces below still work/);
110 assert.match(d, /ebay\.com\/sch/);
111 down.close();
112 } finally {
113 server.close();
114 fake.close();
115 }
116 console.log('markets: Deals with prices as of their time, every marketplace with the words in, failures said plainly');
117})().catch((err) => { console.error(err); process.exit(1); });
118