File size: 5,188 Bytes
2d71dc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* Checks the logic of a step's `refused` (src/fusion/refusal.ts) without a browser or a pod: `npx tsx scripts/check-refusal.ts`
 * (coworker/tests/test_edge_cases.py runs it when tsx is installed). A refused step passes only when FUSION said no in the
 * words expected; the runner's own failures (a locator timeout, a page that did not open) must never pass as a refusal. */
import assert from 'node:assert/strict';
import { expectRefusal, FusionRefused, refusalOf, refusalWords, refusedWith, ValueRefused } from '../src/fusion/refusal';

const fails = async (p: Promise<unknown>, re: RegExp) => { await assert.rejects(p, re); };

(async () => {
  // the words: alternatives, any case, any spacing, curly quotes
  assert.deepEqual(refusalWords(' Negative | less  than ZERO |'), ['negative', 'less than zero']);
  assert.deepEqual(refusalWords(undefined), []);
  assert.equal(refusedWith("The amount can’t be negative.", ["can't be negative"]), "can't be negative");
  assert.equal(refusedWith('Enter a value for Supplier', ['negative', 'supplier']), 'supplier');
  assert.equal(refusedWith('Something else', ['negative']), undefined);

  // what counts as Fusion's own words
  assert.equal(refusalOf(new FusionRefused('Error: Enter a negative amount', 'saving: Fusion said Error: Enter a negative amount')), 'Error: Enter a negative amount');
  assert.equal(refusalOf(new ValueRefused('Fusion did not accept "5" in "Amount": must be negative', 'must be negative')), 'must be negative');
  assert.equal(refusalOf(new ValueRefused('"X" is not in the list (A, B)')), null);        // a pod value Fusion does not list: the pod's data, not a refusal
  assert.equal(refusalOf(new Error('locator.click: Timeout 30000ms exceeded')), null);     // the runner's failure
  assert.equal(refusalOf(new Error('Fusion said no')), null);                                // a plain Error is never taken for Fusion, whatever it says
  assert.equal(refusalOf('Fusion said negative'), null);

  // 1. the action throws Fusion's message with the expected words: the step passes and says what Fusion said
  let closed = 0;
  const said = await expectRefusal('create_invoice', 'negative|less than zero', async () => {
    throw new FusionRefused('Error: The amount must be less than zero for a credit memo.', 'Fusion did not save invoice X: Error: ...');
  }, async () => { closed++; });
  assert.match(said, /less than zero/);
  assert.equal(closed, 1);   // the page is left clean

  // 2. the action succeeds: Fusion took what it should refuse
  await fails(expectRefusal('create_invoice', 'negative', async () => undefined), /Fusion took create_invoice that it should refuse/);

  // 3. a locator timeout (or any failure that is not Fusion's message): the step fails with that reason, never a pass
  await fails(expectRefusal('create_invoice', 'negative', async () => { throw new Error('locator.click: Timeout 30000ms exceeded.\n call log: ...'); }),
    /failed for another reason.*locator\.click: Timeout 30000ms exceeded/);
  await fails(expectRefusal('create_invoice', 'negative', async () => { throw new Error('page.goto: net::ERR_CONNECTION_RESET'); }), /another reason.*ERR_CONNECTION_RESET/);
  // ... even when its text mentions the expected words: only Fusion's own message counts
  await fails(expectRefusal('create_invoice', 'negative', async () => { throw new Error('the amount is negative and the locator timed out'); }), /another reason/);
  // a pod value Fusion does not list is not a refusal of the document
  await fails(expectRefusal('create_invoice', 'supplier', async () => { throw new ValueRefused('"S" is not in the list (A, B)'); }), /another reason/);
  await fails(expectRefusal('create_invoice', 'supplier', async () => { throw new ValueRefused('Fusion marked it invalid'); }), /another reason/);

  // 4. Fusion refused, but for another reason: the step fails and shows what Fusion said
  let closedOther = 0;
  await fails(expectRefusal('create_invoice', 'negative', async () => { throw new FusionRefused('Error: You must enter a supplier site.'); }, async () => { closedOther++; }),
    /Fusion refused create_invoice, but not for the reason expected: it said "Error: You must enter a supplier site\."/);
  assert.equal(closedOther, 1);

  // 5. a field message Fusion showed (ValueRefused with its words) counts like a window
  assert.match(await expectRefusal('create_invoice', 'negative', async () => { throw new ValueRefused('Fusion did not accept "100" in "Amount"', 'Enter a negative amount.'); }), /negative/);

  // 6. a step without words is a mistake of the scenario, not a pass
  await fails(expectRefusal('create_invoice', '', async () => { throw new FusionRefused('anything'); }), /refused needs the words/);
  await fails(expectRefusal('create_invoice', ' | ', async () => undefined), /refused needs the words/);

  // 7. a failing clean-up of the page does not hide the refusal
  assert.match(await expectRefusal('save', 'required', async () => { throw new FusionRefused('Supplier is required'); }, async () => { throw new Error('page closed'); }), /required/);

  console.log('check-refusal: ok');
})().catch((e) => { console.error(e); process.exit(1); });