Skip to content

Commit 8dcce8b

Browse files
committed
test(spec): push device registration widening, push route and transport config cases (red)
Claude-Session: https://claude.ai/code/session_01FEkQ5oc56xChD6Wrc4cGSt Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6b3ba82 commit 8dcce8b

3 files changed

Lines changed: 215 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// The data a push delivery carries to the shell. Its `route` is opened by the
4+
// shell on tap, so the contract admits only a same-server relative path: a
5+
// value that could navigate anywhere else (another origin, a scheme, a
6+
// protocol-relative reference, or a path a URL parser rewrites into one) is
7+
// refused here, and the server leaves `route` out instead of sending it.
8+
9+
import { describe, it, expect } from 'vitest';
10+
import { PushDeliveryDataSchema, PushRouteSchema } from './push-delivery.zod';
11+
12+
describe('PushDeliveryDataSchema', () => {
13+
it('accepts a notification id with a Console route', () => {
14+
const data = { notificationId: 'ntf_1', route: '/apps/crm/opportunity/123' };
15+
expect(PushDeliveryDataSchema.parse(data)).toEqual(data);
16+
});
17+
18+
it('accepts an absent route', () => {
19+
expect(PushDeliveryDataSchema.parse({ notificationId: 'ntf_1' })).toEqual({ notificationId: 'ntf_1' });
20+
});
21+
22+
it('accepts the derived record route `/OBJECT/ID` and a query or fragment', () => {
23+
expect(PushRouteSchema.safeParse('/opportunity/123').success).toBe(true);
24+
expect(PushRouteSchema.safeParse('/apps/crm/opportunity/123?tab=notes#activity').success).toBe(true);
25+
expect(PushRouteSchema.safeParse('/').success).toBe(true);
26+
});
27+
28+
it('requires a notification id', () => {
29+
const parsed = PushDeliveryDataSchema.safeParse({ route: '/x' });
30+
expect(parsed.success).toBe(false);
31+
expect(parsed.error?.issues[0]?.path).toEqual(['notificationId']);
32+
});
33+
34+
it.each([
35+
'https://evil.example/x',
36+
'//evil.example',
37+
'javascript:alert(1)',
38+
'apps/crm/opportunity/123',
39+
'',
40+
// A URL parser reads a backslash as a slash and strips tab / newline, so
41+
// each of these becomes a protocol-relative reference once parsed.
42+
'/\\evil.example',
43+
'/\t/evil.example',
44+
'/\n/evil.example',
45+
'/apps/crm\\..\\x',
46+
])('refuses route %j', (route) => {
47+
const parsed = PushDeliveryDataSchema.safeParse({ notificationId: 'ntf_1', route });
48+
expect(parsed.success).toBe(false);
49+
expect(parsed.error?.issues[0]?.path).toEqual(['route']);
50+
});
51+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// The device registration request is WIDENED for the push channel (ADR-0012
4+
// §6): it gains an optional `vendor`, `appId` and `locale`. Pure widening, so
5+
// the body every client sends today must keep parsing unchanged, and the new
6+
// members are optional. The one value the widening refuses is a vendor
7+
// outside the pair core delivers through (`apns` / `fcm`); other vendors are
8+
// alternate channel plugins (ADR-0012 §6), not values of this field.
9+
10+
import { describe, it, expect } from 'vitest';
11+
import {
12+
RegisterDeviceRequestSchema,
13+
RegisterDeviceResponseSchema,
14+
UnregisterDeviceRequestSchema,
15+
UnregisterDeviceResponseSchema,
16+
} from './protocol.zod';
17+
18+
describe('RegisterDeviceRequestSchema (push device registry)', () => {
19+
it('still parses the minimal body a client sends today, unchanged', () => {
20+
const body = { token: 'fcm_token_xyz', platform: 'android' };
21+
const parsed = RegisterDeviceRequestSchema.safeParse(body);
22+
expect(parsed.success).toBe(true);
23+
expect(parsed.data).toEqual(body);
24+
});
25+
26+
it('still parses the full pre-widening body (deviceId and name stay optional members)', () => {
27+
const body = { token: 't', platform: 'ios', deviceId: 'install-1', name: 'iPhone' };
28+
expect(RegisterDeviceRequestSchema.parse(body)).toEqual(body);
29+
});
30+
31+
it('round-trips the widened members: vendor, appId, locale', () => {
32+
const body = {
33+
token: 'apns-device-token',
34+
platform: 'ios',
35+
vendor: 'apns',
36+
deviceId: 'install-1',
37+
appId: 'com.example.shell',
38+
locale: 'zh-CN',
39+
name: 'iPhone',
40+
} as const;
41+
const parsed = RegisterDeviceRequestSchema.safeParse(body);
42+
expect(parsed.success).toBe(true);
43+
expect(parsed.data).toEqual(body);
44+
expect(RegisterDeviceRequestSchema.parse({ token: 't', platform: 'android', vendor: 'fcm' }).vendor).toBe('fcm');
45+
});
46+
47+
it('applies no default for an absent vendor: the server derives it from platform', () => {
48+
// No `.default()` on purpose: the derivation (ios → apns, android → fcm)
49+
// is the server's, stated in the describe, and the bare alias stays
50+
// isomorphic (type-alias-convention.pin.test.ts).
51+
const parsed = RegisterDeviceRequestSchema.parse({ token: 't', platform: 'ios' });
52+
expect('vendor' in parsed).toBe(false);
53+
});
54+
55+
it.each(['hms', 'xiaomi', 'web_push', 'APNS', ''])('refuses vendor %j (core delivers through apns / fcm only)', (vendor) => {
56+
const parsed = RegisterDeviceRequestSchema.safeParse({ token: 't', platform: 'android', vendor });
57+
expect(parsed.success).toBe(false);
58+
expect(parsed.error?.issues[0]?.path).toEqual(['vendor']);
59+
expect(parsed.error?.issues[0]?.code).toBe('invalid_value');
60+
});
61+
62+
it('keeps the response and unregister shapes as they were', () => {
63+
expect(RegisterDeviceResponseSchema.parse({ deviceId: 'install-1', success: true }))
64+
.toEqual({ deviceId: 'install-1', success: true });
65+
expect(UnregisterDeviceRequestSchema.parse({ deviceId: 'install-1' })).toEqual({ deviceId: 'install-1' });
66+
expect(UnregisterDeviceResponseSchema.parse({ success: true })).toEqual({ success: true });
67+
});
68+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// Push transport configuration (APNs token auth, FCM HTTP v1). Its two
4+
// secrets — the APNs `.p8` signing key and the FCM service-account JSON — are
5+
// named by reference only (ADR-0143 D3), so an inline secret is refused two
6+
// ways: under the key an operator would reach for (refused by name, with the
7+
// reference key as the prescription), and as the CONTENT of a reference (a
8+
// PEM block or a JSON document is a secret pasted where its name belongs).
9+
10+
import { describe, it, expect } from 'vitest';
11+
import {
12+
ApnsTransportConfigSchema,
13+
FcmTransportConfigSchema,
14+
PushServiceConfigSchema,
15+
PushVendorSchema,
16+
} from './push-config.zod';
17+
18+
const APNS = {
19+
teamId: 'ABCDE12345',
20+
keyId: 'KEY1234567',
21+
topic: 'com.example.shell',
22+
environment: 'production',
23+
keyRef: 'OS_PUSH_APNS_KEY',
24+
} as const;
25+
26+
const FCM = { projectId: 'example-project', serviceAccountRef: 'OS_PUSH_FCM_SERVICE_ACCOUNT' } as const;
27+
28+
const PEM = '-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg\n-----END PRIVATE KEY-----';
29+
const SERVICE_ACCOUNT_JSON = '{"type":"service_account","project_id":"example-project","private_key":"x"}';
30+
31+
describe('PushVendorSchema', () => {
32+
it('names exactly the two vendors core delivers through', () => {
33+
expect(PushVendorSchema.options).toEqual(['apns', 'fcm']);
34+
});
35+
});
36+
37+
describe('PushServiceConfigSchema', () => {
38+
it('accepts both transports configured by reference', () => {
39+
const config = { apns: APNS, fcm: FCM };
40+
expect(PushServiceConfigSchema.parse(config)).toEqual(config);
41+
});
42+
43+
it('accepts either transport alone, and the sandbox environment', () => {
44+
expect(PushServiceConfigSchema.safeParse({ apns: { ...APNS, environment: 'sandbox' } }).success).toBe(true);
45+
expect(PushServiceConfigSchema.safeParse({ fcm: FCM }).success).toBe(true);
46+
});
47+
48+
it.each([
49+
'OS_PUSH_APNS_KEY',
50+
'push/apns-signing-key',
51+
'vault:push.apns.p8',
52+
])('accepts the reference form %j', (keyRef) => {
53+
expect(ApnsTransportConfigSchema.safeParse({ ...APNS, keyRef }).success).toBe(true);
54+
expect(FcmTransportConfigSchema.safeParse({ ...FCM, serviceAccountRef: keyRef }).success).toBe(true);
55+
});
56+
57+
it.each(['key', 'privateKey', 'p8'])('refuses an inline APNs key under %j, by name', (key) => {
58+
const parsed = ApnsTransportConfigSchema.safeParse({ ...APNS, [key]: PEM });
59+
expect(parsed.success).toBe(false);
60+
const issue = parsed.error?.issues[0];
61+
expect(issue?.code).toBe('unrecognized_keys');
62+
expect(issue?.message).toContain('keyRef');
63+
});
64+
65+
it.each(['serviceAccount', 'serviceAccountJson', 'privateKey'])('refuses an inline FCM credential under %j, by name', (key) => {
66+
const parsed = FcmTransportConfigSchema.safeParse({ ...FCM, [key]: SERVICE_ACCOUNT_JSON });
67+
expect(parsed.success).toBe(false);
68+
const issue = parsed.error?.issues[0];
69+
expect(issue?.code).toBe('unrecognized_keys');
70+
expect(issue?.message).toContain('serviceAccountRef');
71+
});
72+
73+
it('refuses a PEM block pasted into keyRef', () => {
74+
const parsed = ApnsTransportConfigSchema.safeParse({ ...APNS, keyRef: PEM });
75+
expect(parsed.success).toBe(false);
76+
expect(parsed.error?.issues[0]?.path).toEqual(['keyRef']);
77+
});
78+
79+
it('refuses a service-account JSON document pasted into serviceAccountRef', () => {
80+
const parsed = FcmTransportConfigSchema.safeParse({ ...FCM, serviceAccountRef: SERVICE_ACCOUNT_JSON });
81+
expect(parsed.success).toBe(false);
82+
expect(parsed.error?.issues[0]?.path).toEqual(['serviceAccountRef']);
83+
});
84+
85+
it('refuses an environment outside sandbox / production', () => {
86+
const parsed = ApnsTransportConfigSchema.safeParse({ ...APNS, environment: 'development' });
87+
expect(parsed.success).toBe(false);
88+
expect(parsed.error?.issues[0]?.path).toEqual(['environment']);
89+
});
90+
91+
it('refuses an unknown transport key on the block', () => {
92+
const parsed = PushServiceConfigSchema.safeParse({ fcm: FCM, hms: {} });
93+
expect(parsed.success).toBe(false);
94+
expect(parsed.error?.issues[0]?.code).toBe('unrecognized_keys');
95+
});
96+
});

0 commit comments

Comments
 (0)