106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import {
|
|
campaignStatus,
|
|
publicCampaignView,
|
|
} from '../src/campaigns/campaign-policy';
|
|
import {
|
|
campaignCreateSchema,
|
|
campaignPatchSchema,
|
|
} from '../src/campaigns/campaign.schemas';
|
|
|
|
const startsAt = new Date('2030-10-01T00:00:00.000Z');
|
|
const endsAt = new Date('2030-10-02T00:00:00.000Z');
|
|
const campaign = { state: 'PUBLISHED' as const, startsAt, endsAt };
|
|
const input = {
|
|
slug: 'seasonal-gifts',
|
|
title: 'Seasonal gifts',
|
|
summary: '',
|
|
collectionId: randomUUID(),
|
|
startsAt: startsAt.toISOString(),
|
|
endsAt: endsAt.toISOString(),
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
|
|
describe('seasonal campaign contract', () => {
|
|
it('includes the start and excludes the end of the live window', () => {
|
|
expect(campaignStatus(campaign, new Date(startsAt.getTime() - 1))).toBe(
|
|
'SCHEDULED',
|
|
);
|
|
expect(campaignStatus(campaign, startsAt)).toBe('ACTIVE');
|
|
expect(campaignStatus(campaign, new Date(endsAt.getTime() - 1))).toBe(
|
|
'ACTIVE',
|
|
);
|
|
expect(campaignStatus(campaign, endsAt)).toBe('ENDED');
|
|
});
|
|
it('keeps drafts and cancelled campaigns private regardless of the clock', () => {
|
|
expect(campaignStatus({ ...campaign, state: 'DRAFT' }, startsAt)).toBe(
|
|
'DRAFT',
|
|
);
|
|
expect(campaignStatus({ ...campaign, state: 'CANCELLED' }, startsAt)).toBe(
|
|
'CANCELLED',
|
|
);
|
|
});
|
|
it('normalizes timezone offsets and rejects collapsed or invalid windows', () => {
|
|
const parsed = campaignCreateSchema.parse({
|
|
...input,
|
|
startsAt: '2030-10-01T05:30:00+05:30',
|
|
});
|
|
expect(parsed.startsAt.toISOString()).toBe(startsAt.toISOString());
|
|
for (const endsAt of [
|
|
input.startsAt,
|
|
'2030-09-30T00:00:00Z',
|
|
'2030-10-01T00:00:00.000001Z',
|
|
]) {
|
|
expect(campaignCreateSchema.safeParse({ ...input, endsAt }).success).toBe(
|
|
false,
|
|
);
|
|
}
|
|
expect(
|
|
campaignCreateSchema.safeParse({
|
|
...input,
|
|
startsAt: '2030-10-01T00:00:00',
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
it('rejects forged lifecycle/ownership fields and empty edits', () => {
|
|
for (const fields of [
|
|
{ state: 'PUBLISHED' },
|
|
{ organizationId: randomUUID() },
|
|
{ title: '<b>Offer</b>' },
|
|
{ slug: '../unsafe' },
|
|
])
|
|
expect(
|
|
campaignCreateSchema.safeParse({ ...input, ...fields }).success,
|
|
).toBe(false);
|
|
expect(campaignPatchSchema.safeParse({ version: 0 }).success).toBe(false);
|
|
expect(
|
|
campaignPatchSchema.safeParse({ version: -1, title: 'Offer' }).success,
|
|
).toBe(false);
|
|
expect(
|
|
campaignPatchSchema.safeParse({ version: 0, title: 'Offer' }).success,
|
|
).toBe(true);
|
|
});
|
|
it('allowlists public content without staff identifiers or replay metadata', () => {
|
|
const record = {
|
|
...campaign,
|
|
id: 'campaign',
|
|
slug: 'seasonal-gifts',
|
|
title: 'Seasonal gifts',
|
|
summary: '',
|
|
organizationId: 'private-org',
|
|
createdById: 'private-staff',
|
|
requestHash: 'private-hash',
|
|
collection: { id: 'collection', name: 'Gifts', slug: 'gifts' },
|
|
};
|
|
const result = publicCampaignView(record);
|
|
expect(result.collection.id).toBe('collection');
|
|
for (const field of [
|
|
'state',
|
|
'organizationId',
|
|
'createdById',
|
|
'requestHash',
|
|
])
|
|
expect(result).not.toHaveProperty(field);
|
|
});
|
|
});
|