Skip to content

Commit f16e299

Browse files
committed
CCM-10483: break out source code
1 parent 9b1f79e commit f16e299

File tree

12 files changed

+93
-69
lines changed

12 files changed

+93
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"lint:fix": "npm run lint:fix --workspaces",
4343
"start": "npm run start --workspace frontend",
4444
"test:contracts:clean": "npm --workspace=tests/contracts run pact:clean",
45-
"test:contracts:consumers": "npm --workspace=tests/contracts run test:consumers",
45+
"test:contracts:consumer": "npm --workspace=tests/contracts run test:consumer",
4646
"test:contracts:download:consumer": "npm --workspace=tests/contracts run pact:download:consumer",
4747
"test:contracts:provider": "npm --workspace=tests/contracts run test:provider",
4848
"test:contracts:provider:ci": "npm --workspace=tests/contracts run test:provider:ci",

tests/contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"pact:clean": "./scripts/clean.sh",
1616
"pact:download:consumer": "./scripts/download-consumer-pacts.sh",
1717
"pact:upload:consumer": "./scripts/upload-consumer-pacts.sh",
18-
"test:consumers": "jest consumer.pact.test.ts",
18+
"test:consumer": "jest consumer.pact.test.ts",
1919
"test:provider": "jest provider.pact.test.ts",
2020
"test:provider:ci": "./scripts/ci-verify-provider.sh",
2121
"test:unit": "echo 'No unit tests required'",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { randomUUID } from 'node:crypto';
2+
3+
export function createUserCreatedEvent() {
4+
return {
5+
'detail-type': 'UserCreated',
6+
source: 'uk.nhs.notify.auth',
7+
time: new Date().toISOString(),
8+
version: '1.0',
9+
detail: {
10+
clientId: randomUUID(),
11+
userId: randomUUID(),
12+
},
13+
};
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { z } from 'zod';
2+
3+
export const $TemplateDeletedEvent = z.object({
4+
'detail-type': z.literal('TemplateDeleted'),
5+
version: z.literal('1.0'),
6+
detail: z.object({
7+
id: z.string().uuid(),
8+
owner: z.string().uuid(),
9+
}),
10+
});
11+
12+
export async function handleTemplateDeleted(event: unknown): Promise<void> {
13+
$TemplateDeletedEvent.parse(event);
14+
15+
// Handler logic goes here
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { z } from 'zod';
2+
3+
// Handlers should define their own minimal input validators
4+
// Decoupled from implementations in other services
5+
// So this is slightly different to what's defined in the templates service which produces the event
6+
// And different to the TemplateDeleted handler in the auth service
7+
8+
export const $TemplateDeletedEvent = z.object({
9+
'detail-type': z.literal('TemplateDeleted'),
10+
detail: z.object({
11+
id: z.string().uuid(),
12+
owner: z.string().uuid(),
13+
}),
14+
});
15+
16+
export async function handleTemplateDeleted(event: unknown): Promise<void> {
17+
$TemplateDeletedEvent.parse(event);
18+
19+
// Handler logic goes here
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { randomUUID } from 'node:crypto';
2+
3+
export function createTemplateDeletedEvent() {
4+
return {
5+
'detail-type': 'TemplateDeleted',
6+
source: 'uk.nhs.notify.templates',
7+
time: new Date().toISOString(),
8+
version: '1.0',
9+
detail: {
10+
owner: randomUUID(),
11+
id: randomUUID(),
12+
},
13+
};
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { z } from 'zod';
2+
3+
// I guess this would be defined in the handler source code in the event consumer and imported here
4+
// Handlers should parse the incoming event before doing anything else
5+
export const $UserCreatedEvent = z.object({
6+
'detail-type': z.literal('UserCreated'),
7+
detail: z.object({
8+
userId: z.string().uuid(),
9+
clientId: z.string().uuid(),
10+
}),
11+
});
12+
13+
export async function handleUserCreatedEvent(event: unknown): Promise<void> {
14+
$UserCreatedEvent.parse(event);
15+
16+
// Handler logic goes here
17+
}

tests/contracts/tests/auth/consumer/template-deleted.consumer.pact.test.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,9 @@ import {
44
Matchers,
55
asynchronousBodyHandler,
66
} from '@pact-foundation/pact';
7-
import { z } from 'zod';
7+
import { $TemplateDeletedEvent } from '../../../src/auth/handlers/template-deleted.handler';
88

9-
// I guess this would be defined in the handler source code in the event consumer and imported here
10-
// Handlers should parse the incoming event before doing anything else
11-
const $TemplateDeletedEvent = z.object({
12-
'detail-type': z.literal('TemplateDeleted'),
13-
version: z.literal('1.0'),
14-
detail: z.object({
15-
id: z.string().uuid(),
16-
owner: z.string().uuid(),
17-
}),
18-
});
19-
20-
// Simulate consumer handler that processes the incoming event
9+
// Stub of handler that processes the incoming event
2110
// Only check the validation - don't run actual handler logic
2211
async function handleTemplateDeleted(event: unknown): Promise<void> {
2312
$TemplateDeletedEvent.parse(event);
@@ -34,7 +23,7 @@ describe('Pact Message Consumer - TemplateDeleted Event', () => {
3423

3524
it('should validate the template deleted event structure and handler logic', async () => {
3625
await messagePact
37-
.given('A template has been deleted')
26+
.given('a template has been deleted')
3827
.expectsToReceive('TemplateDeleted')
3928
.withContent({
4029
'detail-type': 'TemplateDeleted',

tests/contracts/tests/auth/provider/user-created.provider.pact.test.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { MessageProviderPact } from '@pact-foundation/pact';
4-
5-
// This would be the actual function that produces the event payload in the producer source code
6-
function createUserCreatedEvent() {
7-
return {
8-
'detail-type': 'UserCreated',
9-
source: 'uk.nhs.notify.auth',
10-
time: new Date().toISOString(),
11-
version: '1.0',
12-
detail: {
13-
clientId: '3aef04e4-4491-42ae-b24a-95c66b80cbc9',
14-
userId: 'f58efb29-27a0-4da3-a502-ddd387771c1e',
15-
},
16-
};
17-
}
4+
import { createUserCreatedEvent } from '../../../src/auth/events/user-created.event';
185

196
describe('Pact Message Provider - UserCreated Event', () => {
207
const pactDir = path.resolve(__dirname, 'pacts');

tests/contracts/tests/core/consumer/template-deleted.consumer.pact.test.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@ import {
44
Matchers,
55
asynchronousBodyHandler,
66
} from '@pact-foundation/pact';
7-
import { z } from 'zod';
7+
import { $TemplateDeletedEvent } from '../../../src/core/handlers/template-deleted.handler';
88

9-
// I guess this would be defined in the handler source code in the event consumer and imported here
10-
// Handlers should parse the incoming event before doing anything else
11-
const $TemplateDeletedEvent = z.object({
12-
'detail-type': z.literal('TemplateDeleted'),
13-
detail: z.object({
14-
id: z.string().uuid(),
15-
owner: z.string().uuid(),
16-
}),
17-
});
18-
19-
// Simulate consumer handler that processes the incoming event
9+
// Stub of handler that processes the incoming event
2010
// Only check the validation - don't run actual handler logic
2111
async function handleTemplateDeleted(event: unknown): Promise<void> {
2212
$TemplateDeletedEvent.parse(event);
@@ -33,7 +23,7 @@ describe('Pact Message Consumer - TemplateDeleted Event', () => {
3323

3424
it('should validate the template deleted event structure and handler logic', async () => {
3525
await messagePact
36-
.given('A template has been deleted')
26+
.given('a template has been deleted')
3727
.expectsToReceive('TemplateDeleted')
3828
.withContent({
3929
'detail-type': 'TemplateDeleted',

0 commit comments

Comments
 (0)