summaryrefslogtreecommitdiff
path: root/packages/vercel-optimize-tests/test/route-errors.test.mjs
blob: 943455a02807446aec5856540ba8413025fb3686 (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
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { gate, metadata } from '../../../skills/vercel-optimize/lib/gates/route-errors.mjs';

function signals({ route = '/api/orders', errors, total }) {
  return {
    metrics: {
      requestsByRouteStatus: { rows: [{ route, http_status: '500', value: errors }] },
      requestsByRouteCache: { rows: [{ route, cache_result: 'MISS', value: total }] },
    },
  };
}

test('route_errors: high rate below volume floor stays silent', () => {
  assert.deepEqual(gate(signals({ errors: 20, total: 200 })), []);
});

test('route_errors: high rate emits after volume floor', () => {
  const out = gate(signals({ errors: 20, total: 1000 }));
  assert.equal(out.length, 1);
  assert.equal(out[0].route, '/api/orders');
  assert.equal(out[0].evidence.metric, 'requestsByRouteStatus');
  assert.equal(out[0].evidence.totalRequests, 1000);
  assert.equal(out[0].evidence.errorRate, 0.02);
  assert.match(out[0].o11ySignal, /rate=2\.0%/);
});

test('route_errors: prefers function-level status for error counts and rates', () => {
  const out = gate({
    metrics: {
      fnStatusByRoute: { rows: [
        { route: '/api/orders', http_status: '200', value: 900 },
        { route: '/api/orders', http_status: '500', value: 100 },
      ] },
      requestsByRouteStatus: { rows: [
        { route: '/api/orders', http_status: '200', value: 990 },
        { route: '/api/orders', http_status: '500', value: 10 },
      ] },
      requestsByRouteCache: { rows: [{ route: '/api/orders', cache_result: 'MISS', value: 1000 }] },
    },
  });
  assert.equal(out.length, 1);
  assert.equal(out[0].evidence.metric, 'fnStatusByRoute');
  assert.equal(out[0].evidence.count, 100);
  assert.equal(out[0].evidence.totalRequests, 1000);
  assert.equal(out[0].evidence.errorRate, 0.1);
  assert.match(out[0].o11ySignal, /rate=10\.0%/);
});

test('route_errors: absolute count floor still emits without rate support', () => {
  const out = gate({
    metrics: {
      requestsByRouteStatus: { rows: [{ route: '/api/orders', http_status: '500', value: 251 }] },
      requestsByRouteCache: { rows: [] },
    },
  });
  assert.equal(out.length, 1);
  assert.equal(out[0].evidence.totalRequests, 0);
  assert.equal(out[0].evidence.errorRate, null);
});

test('route_errors: metadata states the rate volume floor', () => {
  assert.match(metadata.threshold, /totalRequests >= 1000/);
  assert.match(metadata.description, /at least 1,000 total requests/);
});