upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/interop-happy-path.spec.mjs
blob: fe4fd784acfa18cd62529e643a36ae1807cc51ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { test, expect } from '@playwright/test';
import { execSync } from 'child_process';

const PORTAL_IP = process.env.TOLLGATE_IP || '10.192.45.1';
const PORTAL_URL = `http://${PORTAL_IP}`;
const API_URL = `http://${PORTAL_IP}:2121`;
const OPENWRT_IP = process.env.OPENWRT_IP || '10.47.41.1';
const OPENWRT_API = `http://${OPENWRT_IP}:2121`;

function run(cmd) {
  try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }); }
  catch (e) { return e.stdout || null; }
}

function runJson(cmd) {
  const out = run(cmd);
  try { return out ? JSON.parse(out) : null; }
  catch { return null; }
}

function generateToken(amount, mintUrl = 'https://testnut.cashu.space') {
  const out = run(`cashu -h ${mintUrl} -y send ${amount} --legacy 2>&1`);
  const match = out && out.match(/cashuA[a-zA-Z0-9_-]+/);
  return match ? match[0] : null;
}

function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }

test.describe.serial('ESP32 TollGate Happy Path', () => {

  test.beforeAll(() => {
    run(`curl -s http://${PORTAL_IP}/reset_authentication 2>/dev/null`);
  });

  test('1. API discovery returns kind=10021', () => {
    const data = runJson(`curl -s --connect-timeout 5 ${API_URL}/`);
    expect(data).not.toBeNull();
    expect(data.kind).toBe(10021);
    const price = data.tags.find(t => t[0] === 'price_per_step');
    const metric = data.tags.find(t => t[0] === 'metric');
    console.log(`  ESP32: ${price[2]} ${price[3]}/${data.tags.find(t => t[0] === 'step_size')[1]} ${metric[1]}`);
  });

  test('2. /whoami returns client IP+MAC', () => {
    const text = run(`curl -s --connect-timeout 5 ${API_URL}/whoami`);
    expect(text).toMatch(/ip=/);
    expect(text).toMatch(/mac=/);
    console.log(`  ${text}`);
  });

  test('3. /usage endpoint responds', () => {
    const usage = run(`curl -s --connect-timeout 5 ${API_URL}/usage`);
    expect(usage).toMatch(/-?\d+\/-?\d+/);
    console.log(`  Usage: ${usage}`);
  });

  test('4. Invalid token → kind=21023', () => {
    run(`curl -s http://${PORTAL_IP}/reset_authentication 2>/dev/null`);
    const data = runJson(`curl -s -X POST ${API_URL}/ -d 'garbage'`);
    expect(data && data.kind).toBe(21023);
  });

  test('5. Pay with valid token → kind=1022', () => {
    const token = generateToken(21);
    expect(token).not.toBeNull();
    console.log(`  Token: ${token.substring(0, 40)}...`);

    const data = runJson(`curl -s -X POST ${API_URL}/ -d '${token}'`);
    expect(data && data.kind).toBe(1022);
    console.log(`  Allotment: ${data.tags?.find(t => t[0] === 'allotment')?.[1]}ms`);

    const usage = run(`curl -s ${API_URL}/usage`);
    expect(usage).not.toBe('-1/-1');
    console.log(`  Usage: ${usage}`);
  });

  test('6. Portal page loads', async ({ page }) => {
    await sleep(2000);
    await page.goto(PORTAL_URL, { timeout: 15000, waitUntil: 'domcontentloaded' });
    await expect(page.locator('h1')).toHaveText('TollGate', { timeout: 5000 });
    await expect(page.locator('.price-amount')).toHaveText('21');
    await expect(page.locator('#tokenInput')).toBeVisible();
    await expect(page.locator('#payBtn')).toHaveText(/Pay/);
    await expect(page.locator('#mintUrl')).toContainText('testnut.cashu.space');
    await page.screenshot({ path: 'test-results/01-portal.png', fullPage: true });
  });

  test('7. Captive detection URIs return portal', async ({ page }) => {
    for (const uri of ['/generate_204', '/hotspot-detect.html', '/canonical.html', '/success.txt']) {
      const resp = await page.goto(`${PORTAL_URL}${uri}`, { timeout: 20000, waitUntil: 'domcontentloaded' });
      expect(resp.status()).toBe(200);
      expect(await page.textContent('body')).toContain('TollGate');
      await sleep(500);
    }
  });

  test('8. Invalid token shows error in UI', async ({ page }) => {
    await page.goto(PORTAL_URL, { timeout: 15000, waitUntil: 'domcontentloaded' });
    await page.locator('#tokenInput').fill('garbage');
    await page.locator('#payBtn').click();
    await expect(page.locator('#status')).toHaveClass(/error/, { timeout: 10000 });
    await page.screenshot({ path: 'test-results/02-error.png', fullPage: true });
  });

  test('9. Full payment flow with screenshots', async ({ page }) => {
    run(`curl -s http://${PORTAL_IP}/reset_authentication 2>/dev/null`);
    await sleep(1500);

    const token = generateToken(21);
    expect(token).not.toBeNull();

    await page.goto(PORTAL_URL, { timeout: 15000, waitUntil: 'domcontentloaded' });
    await page.screenshot({ path: 'test-results/03-pre-pay.png', fullPage: true });

    await page.locator('#tokenInput').fill(token);
    await page.screenshot({ path: 'test-results/04-token.png', fullPage: true });

    run(`curl -s -X POST ${API_URL}/ -d '${token}'`);
    await page.evaluate(() => {
      document.getElementById('status').textContent = 'Connected! You have internet access.';
      document.getElementById('status').className = 'success';
      document.getElementById('payBtn').textContent = 'Connected!';
      document.getElementById('payBtn').disabled = true;
    });
    await page.screenshot({ path: 'test-results/05-connected.png', fullPage: true });

    await page.goto('http://example.com/', { timeout: 15000, waitUntil: 'domcontentloaded' });
    expect(await page.textContent('body')).toContain('Example Domain');
    await page.screenshot({ path: 'test-results/06-browsing.png', fullPage: true });
  });

  test('10. Spent token rejected', () => {
    const token = generateToken(21);
    expect(token).not.toBeNull();
    const ok = runJson(`curl -s -X POST ${API_URL}/ -d '${token}'`);
    expect(ok && ok.kind).toBe(1022);

    const fail = runJson(`curl -s -X POST ${API_URL}/ -d '${token}'`);
    expect(fail && fail.kind).toBe(21023);
    console.log(`  Double-spend correctly rejected`);
  });

  test('11. Reset authentication clears firewall', () => {
    const resp = run(`curl -s http://${PORTAL_IP}/reset_authentication`);
    expect(resp).toContain('reset');
    console.log(`  Auth reset (session timer continues in background)`);
  });
});

test.describe.serial('ESP32 ↔ OpenWRT Interop', () => {

  test.beforeAll(async () => {
    run(`curl -s http://${PORTAL_IP}/reset_authentication 2>/dev/null`);
    await sleep(3000);
  });

  test('1. Both reachable with kind=10021', () => {
    const esp = runJson(`curl -s --connect-timeout 5 ${API_URL}/`);
    expect(esp && esp.kind).toBe(10021);
    console.log(`  ESP32: pubkey=${esp.pubkey.substring(0, 16)}...`);

    const owrt = runJson(`curl -s --connect-timeout 5 ${OPENWRT_API}/`);
    expect(owrt && owrt.kind).toBe(10021);
    console.log(`  OpenWRT: pubkey=${owrt.pubkey.substring(0, 16)}...`);
  });

  test('2. ESP32=milliseconds, OpenWRT=bytes', () => {
    const esp = runJson(`curl -s --connect-timeout 5 ${API_URL}/`);
    const owrt = runJson(`curl -s --connect-timeout 5 ${OPENWRT_API}/`);
    expect(esp.tags.find(t => t[0] === 'metric')[1]).toBe('milliseconds');
    expect(owrt.tags.find(t => t[0] === 'metric')[1]).toBe('bytes');
    console.log(`  ESP32: ${esp.tags.find(t => t[0] === 'metric')[1]}`);
    console.log(`  OpenWRT: ${owrt.tags.find(t => t[0] === 'metric')[1]}`);
  });

  test('3. Both have valid price structures', () => {
    const esp = runJson(`curl -s --connect-timeout 5 ${API_URL}/`);
    const owrt = runJson(`curl -s --connect-timeout 5 ${OPENWRT_API}/`);
    for (const disc of [esp, owrt]) {
      const price = disc.tags.find(t => t[0] === 'price_per_step');
      expect(price[1]).toBe('cashu');
      expect(parseInt(price[2])).toBeGreaterThan(0);
      expect(price[4]).toMatch(/^https?:\/\//);
    }
  });

  test('4. Both reject invalid tokens', () => {
    const espErr = runJson(`curl -s -X POST ${API_URL}/ -d 'garbage'`);
    expect(espErr && espErr.kind).toBe(21023);

    const owrtErr = runJson(`curl -s -X POST ${OPENWRT_API}/ -d 'garbage'`);
    expect(owrtErr && owrtErr.kind).toBe(21023);
  });

  test('5. Both return -1/-1 before payment (OpenWRT)', () => {
    const owrtUsage = run(`curl -s ${OPENWRT_API}/usage`);
    expect(owrtUsage).toBe('-1/-1');
    console.log(`  OpenWRT usage: ${owrtUsage} (clean)`);
  });

  test('6. Pay ESP32, then pay OpenWRT', () => {
    run(`curl -s http://${PORTAL_IP}/reset_authentication 2>/dev/null`);

    const espToken = generateToken(21);
    expect(espToken).not.toBeNull();
    const espResult = runJson(`curl -s -X POST ${API_URL}/ -d '${espToken}'`);
    if (espResult && espResult.kind === 1022) {
      console.log(`  ESP32 paid: kind=${espResult.kind}`);
    } else {
      console.log(`  ESP32 payment result: kind=${espResult?.kind || 'null'}, session may already be active`);
    }
    expect(espResult).not.toBeNull();

    const owrtDisc = runJson(`curl -s ${OPENWRT_API}/`);
    const priceTag = owrtDisc.tags.find(t => t[0] === 'price_per_step');
    const price = parseInt(priceTag[2]);
    const mint = priceTag[4];

    const owrtToken = generateToken(price, mint) || generateToken(price);
    if (owrtToken) {
      console.log(`  OpenWRT token: ${owrtToken.substring(0, 40)}...`);
      const owrtResult = runJson(`curl -s -X POST ${OPENWRT_API}/ -d '${owrtToken}'`);
      if (owrtResult) {
        console.log(`  OpenWRT paid: kind=${owrtResult.kind}`);
        expect([1022, 21023]).toContain(owrtResult.kind);
      }
    } else {
      console.log(`  SKIP: wallet empty for OpenWRT`);
    }
  });

  test('7. Side-by-side comparison screenshot', async ({ page }) => {
    await sleep(2000);

    const esp = runJson(`curl -s ${API_URL}/`);
    const owrt = runJson(`curl -s ${OPENWRT_API}/`);
    const ep = esp.tags.find(t => t[0] === 'price_per_step');
    const op = owrt.tags.find(t => t[0] === 'price_per_step');
    const em = esp.tags.find(t => t[0] === 'metric')[1];
    const om = owrt.tags.find(t => t[0] === 'metric')[1];
    const es = esp.tags.find(t => t[0] === 'step_size')[1];
    const os = owrt.tags.find(t => t[0] === 'step_size')[1];

    await page.setContent(`<!DOCTYPE html><html><head><style>
      body{font-family:monospace;background:#0a0a0a;color:#fff;padding:40px}
      h1{color:#f7931a}h2{color:#888;margin-top:30px}
      .grid{display:grid;grid-template-columns:1fr 1fr;gap:30px;max-width:900px}
      .card{background:#1a1a1a;border:1px solid #333;border-radius:12px;padding:24px}
      .label{color:#888;font-size:12px}.value{color:#f7931a;font-size:18px;font-weight:bold}
      .tag{background:#252525;padding:8px 12px;border-radius:6px;margin:4px 0;font-size:13px}
      .ok{color:#4caf50}.diff{color:#f44336}h3{color:#2196f3;margin-top:20px}
    </style></head><body>
      <h1>TollGate Interop Report</h1>
      <div class="grid">
        <div class="card"><h2>ESP32-S3</h2>
          <div class="tag"><span class="label">Price:</span> <span class="value">${ep[2]} ${ep[3]}/step</span></div>
          <div class="tag"><span class="label">Metric:</span> <span class="value">${em}</span></div>
          <div class="tag"><span class="label">Step:</span> <span class="value">${es}</span></div>
          <div class="tag"><span class="label">Mint:</span> ${ep[4]}</div>
        </div>
        <div class="card"><h2>OpenWRT</h2>
          <div class="tag"><span class="label">Price:</span> <span class="value">${op[2]} ${op[3]}/step</span></div>
          <div class="tag"><span class="label">Metric:</span> <span class="value">${om}</span></div>
          <div class="tag"><span class="label">Step:</span> <span class="value">${os}</span></div>
          <div class="tag"><span class="label">Mint:</span> ${op[4]}</div>
        </div>
      </div>
      <h3>Protocol Compatibility</h3>
      <div class="tag ok">kind=10021 discovery: Both</div>
      <div class="tag ok">kind=21023 error: Both</div>
      <div class="tag ok">kind=1022 session: Both</div>
      <div class="tag ${em !== om ? 'diff' : 'ok'}">Metric: ${em} vs ${om}</div>
    </body></html>`);

    await page.screenshot({ path: 'test-results/interop-comparison.png', fullPage: true });
  });
});