Skip to content

Commit 24f4385

Browse files
committed
init
1 parent 06c3f4c commit 24f4385

10 files changed

Lines changed: 272 additions & 10 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ test-component:
106106
test-performance:
107107
(cd tests && npm install && npm run test:performance)
108108

109+
test-contract: # Run provider contract tests @Testing
110+
npm run test:contracts --workspace tests/contracts/provider
111+
109112
version:
110113
rm -f .version
111114
make version-create-effective-file dir=.

package-lock.json

Lines changed: 97 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"lambdas/*",
9090
"pact-contracts",
9191
"scripts/utilities/*",
92-
"tests"
92+
"tests",
93+
"tests/contracts/*"
9394
]
9495
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Downloaded contracts and packages
2+
.contracts/
3+
.packages/
4+
5+
# Build artifacts
6+
dist/
7+
node_modules/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { MessageProviderPact } from "@pact-foundation/pact";
2+
import {
3+
getAllLetterStatuses,
4+
getMessageProviderForStatus,
5+
getPactUrlForStatus,
6+
} from "./utils/utils";
7+
8+
const CONSUMER_PACKAGE = "@nhsdigital/notify-core-consumer-contracts";
9+
10+
describe("Supplier API letter status provider tests", () => {
11+
const statuses = getAllLetterStatuses();
12+
13+
describe.each(statuses)("letter.%s event", (status) => {
14+
test(`verifies letter-${status.toLowerCase()} pact`, async () => {
15+
const p = new MessageProviderPact({
16+
provider: `letter-${status.toLowerCase()}`,
17+
messageProviders: getMessageProviderForStatus(status),
18+
pactUrls: [getPactUrlForStatus(CONSUMER_PACKAGE, status)],
19+
logLevel: "error",
20+
});
21+
22+
await expect(p.verify()).resolves.not.toThrow();
23+
}, 60_000);
24+
});
25+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import path from "node:path";
2+
import fs from "node:fs";
3+
import { globSync } from "glob";
4+
5+
const LETTER_STATUSES = [
6+
"ACCEPTED",
7+
"CANCELLED",
8+
"DELIVERED",
9+
"DISPATCHED",
10+
"ENCLOSED",
11+
"FAILED",
12+
"FORWARDED",
13+
"PENDING",
14+
"PRINTED",
15+
"REJECTED",
16+
"RETURNED",
17+
] as const;
18+
19+
type LetterStatus = (typeof LETTER_STATUSES)[number];
20+
21+
export function getExampleEvent(status: LetterStatus): unknown {
22+
const examplePath = path.join(
23+
__dirname,
24+
"../../../../../internal/events/schemas/examples",
25+
`letter.${status}.json`
26+
);
27+
28+
const content = fs.readFileSync(examplePath, "utf-8");
29+
return JSON.parse(content);
30+
}
31+
32+
export function getMessageProviderForStatus(
33+
status: LetterStatus
34+
): Record<string, () => Promise<unknown>> {
35+
return {
36+
[`letter-${status.toLowerCase()}`]: async () => getExampleEvent(status),
37+
};
38+
}
39+
40+
export function getPactUrlForStatus(
41+
consumerPackage: string,
42+
status: LetterStatus
43+
): string {
44+
const contractsDir = path.join(
45+
__dirname,
46+
"../../.contracts",
47+
consumerPackage,
48+
"pacts/supplier-api"
49+
);
50+
51+
return path.join(contractsDir, `core-letter-${status.toLowerCase()}.json`);
52+
}
53+
54+
export function getAllLetterStatuses(): readonly LetterStatus[] {
55+
return LETTER_STATUSES;
56+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Config } from "jest";
2+
3+
const config: Config = {
4+
testEnvironment: "node",
5+
transform: {
6+
"^.+\\.tsx?$": "ts-jest",
7+
},
8+
testMatch: ["**/*.test.ts"],
9+
moduleNameMapper: {
10+
"@nhsdigital/nhs-notify-event-schemas-supplier-api$":
11+
"<rootDir>/../../../internal/events/src",
12+
},
13+
};
14+
15+
export default config;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@sap/contracts-provider",
3+
"version": "1.0.0",
4+
"private": true,
5+
"scripts": {
6+
"test:contracts": "./scripts/test.sh"
7+
},
8+
"dependencies": {
9+
"@nhsdigital/nhs-notify-event-schemas-supplier-api": "*",
10+
"@pact-foundation/pact": "^16.0.4"
11+
},
12+
"devDependencies": {
13+
"@tsconfig/node22": "^22.0.2",
14+
"@types/jest": "^30.0.0",
15+
"@types/node": "^22.0.0",
16+
"glob": "^11.0.0",
17+
"jest": "^30.0.0",
18+
"ts-jest": "^29.4.0",
19+
"typescript": "^5.9.3"
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
ROOT_DIR="$(git rev-parse --show-toplevel)"
6+
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
7+
TESTS_ROOT="$(dirname "$SCRIPT_DIR")"
8+
9+
cd "$TESTS_ROOT"
10+
11+
rm -rf .packages .contracts
12+
mkdir -p .packages
13+
14+
# Use local tarball if available (for development), otherwise fetch from npm
15+
CORE_CONTRACTS_LOCAL="${CORE_CONTRACTS_LOCAL:-}"
16+
17+
if [[ -n "$CORE_CONTRACTS_LOCAL" && -f "$CORE_CONTRACTS_LOCAL" ]]; then
18+
echo "Using local core consumer contracts: $CORE_CONTRACTS_LOCAL"
19+
PKG_NAME="@nhsdigital/notify-core-consumer-contracts"
20+
mkdir -p ".contracts/$PKG_NAME"
21+
tar -xvzf "$CORE_CONTRACTS_LOCAL" -C ".contracts/$PKG_NAME" --strip-components=1
22+
else
23+
CONSUMER_PACKAGES=(
24+
"@nhsdigital/notify-core-consumer-contracts"
25+
)
26+
27+
for PKG in "${CONSUMER_PACKAGES[@]}"; do
28+
mkdir -p ".contracts/$PKG"
29+
TGZ_NAME=$(npm pack "$PKG" --pack-destination .packages)
30+
tar -xvzf ".packages/$TGZ_NAME" -C ".contracts/$PKG" --strip-components=1
31+
done
32+
fi
33+
34+
npx jest --runInBand
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "@tsconfig/node22/tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist",
5+
"rootDir": "./",
6+
"resolveJsonModule": true,
7+
"esModuleInterop": true,
8+
"isolatedModules": true
9+
},
10+
"include": ["**/*.ts"],
11+
"exclude": ["node_modules", "dist"]
12+
}

0 commit comments

Comments
 (0)