File size: 2,044 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { expect, test } from '@playwright/test';
import { MockApi } from './api';

type Attr = { name: string; type: string; mandatory: boolean };
export type Contract = { status: string; resources: Record<string, { expectedAttributes: Record<string, string>; allowedMandatory: string[] }> };

/**

 * Module-agnostic REST contract check (used by O2C-INT-002 and future packs; P2P-INT-006 keeps its own inline copy).

 * FAILS on: removed expected attribute, type change, or a mandatory attribute not in allowedMandatory (newly required).

 */
export async function checkContract(api: MockApi, contract: Contract, file: string) {
  test.info().annotations.push({ type: 'contract', description: `${file} (status ${contract.status}; hand-authored, not captured)` });
  const violations: string[] = [];
  for (const [resource, spec] of Object.entries(contract.resources)) {
    await test.step(`${resource}/describe vs expected contract`, async () => {
      const r = await api.rest('integration_user', 'GET', `/${resource}/describe`);
      expect(r.status, `${resource}/describe HTTP status`).toBe(200);
      const attrs: Attr[] = r.body?.Resources?.[resource]?.attributes ?? [];
      expect(attrs.length, `${resource}/describe returned attributes`).toBeGreaterThan(0);
      for (const [name, type] of Object.entries(spec.expectedAttributes)) {
        const cur = attrs.find((a) => a.name === name);
        if (!cur) violations.push(`${resource}.${name}: expected attribute missing`);
        else if (cur.type !== type) violations.push(`${resource}.${name}: type ${cur.type}, contract ${type}`);
      }
      for (const a of attrs.filter((x) => x.mandatory && !spec.allowedMandatory.includes(x.name))) violations.push(`${resource}.${a.name}: newly mandatory (not supplied by our integrations)`);
    });
  }
  test.info().annotations.push({ type: 'contract-violations', description: violations.join('; ') || 'none' });
  expect(violations, 'REST describe() deviates from the expected contract').toEqual([]);
}