Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion src/modules/creators/creator-list-item.mapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { requestContextStorage } from '../../utils/als.utils';
import { logger } from '../../utils/logger.utils';

jest.mock('../../utils/logger.utils', () => ({
logger: { warn: jest.fn() },
logger: { warn: jest.fn(), error: jest.fn() },
}));

const warnMock = logger.warn as jest.Mock;
const errorMock = logger.error as jest.Mock;

beforeEach(() => {
warnMock.mockClear();
errorMock.mockClear();
});

describe('mapCreatorListItem()', () => {
Expand Down Expand Up @@ -64,4 +66,82 @@ describe('mapCreatorListItem()', () => {
requestId: 'req-333',
});
});

it('logs an error when a string field receives an unexpected type', () => {
const input = {
id: 'creator-2',
handle: 'test-handle',
displayName: 12345, // number instead of string
avatarUrl: null,
isVerified: false,
createdAt: new Date('2024-01-02T03:04:05.678Z'),
updatedAt: new Date('2024-01-03T03:04:05.678Z'),
} as any;

const result = requestContextStorage.run(
{ path: '/api/v1/creators', method: 'GET', requestId: 'req-type-1' },
() => mapCreatorListItem(input)
);

expect(result).toEqual({
id: 'creator-2',
name: 12345,
avatar: null,
followers: 0,
createdAt: '2024-01-02T03:04:05.678Z',
updatedAt: '2024-01-03T03:04:05.678Z',
});
expect(errorMock).toHaveBeenCalledWith({
msg: 'Creator list field type mismatch',
fieldName: 'displayName',
expectedType: 'string',
receivedType: 'number',
creatorId: 'creator-2',
requestId: 'req-type-1',
});
});

it('logs an error when a Date field receives a string', () => {
const input = {
id: 'creator-3',
handle: 'test-handle',
displayName: 'Alice',
avatarUrl: null,
isVerified: true,
createdAt: '2024-01-02T03:04:05.678Z', // string instead of Date
updatedAt: new Date('2024-01-03T03:04:05.678Z'),
} as any;

requestContextStorage.run(
{ path: '/api/v1/creators', method: 'GET', requestId: 'req-type-2' },
() => mapCreatorListItem(input)
);

expect(errorMock).toHaveBeenCalledWith(
expect.objectContaining({
msg: 'Creator list field type mismatch',
fieldName: 'createdAt',
expectedType: 'Date',
receivedType: 'string',
creatorId: 'creator-3',
requestId: 'req-type-2',
})
);
});

it('does not log an error for correctly typed fields', () => {
const input = {
id: 'creator-4',
handle: 'good-handle',
displayName: 'Bob',
avatarUrl: 'https://example.com/avatar.png',
isVerified: false,
createdAt: new Date('2024-01-02T03:04:05.678Z'),
updatedAt: new Date('2024-01-03T03:04:05.678Z'),
} as any;

mapCreatorListItem(input);

expect(errorMock).not.toHaveBeenCalled();
});
});
46 changes: 46 additions & 0 deletions src/modules/creators/creator-list-item.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@ export type CreatorListItem = {
updatedAt: string;
};

type ExpectedFieldType = 'string' | 'boolean' | 'number' | 'Date';

// Runtime type expectations for fields projected by CREATOR_LIST_DEFAULT_SELECT.
// A mismatch here signals a DB migration changed a column type without updating the mapping.
const CREATOR_LIST_FIELD_EXPECTED_TYPES: Record<string, ExpectedFieldType> = {
id: 'string',
handle: 'string',
displayName: 'string',
avatarUrl: 'string',
isVerified: 'boolean',
createdAt: 'Date',
updatedAt: 'Date',
};

function logIfFieldTypeMismatch(
creator: CreatorProfile,
fieldName: keyof typeof CREATOR_LIST_FIELD_EXPECTED_TYPES
): void {
const value = (creator as unknown as Record<string, unknown>)[fieldName];

if (value === null || value === undefined) return;

const expectedType = CREATOR_LIST_FIELD_EXPECTED_TYPES[fieldName];
const typeMatches =
expectedType === 'Date' ? value instanceof Date : typeof value === expectedType;

if (!typeMatches) {
logger.error({
msg: 'Creator list field type mismatch',
fieldName,
expectedType,
receivedType: value instanceof Date ? 'Date' : typeof value,
creatorId: creator.id,
requestId: requestContextStorage.getStore()?.requestId ?? null,
});
}
}

function warnIfUnexpectedNullCreatorField(
creator: CreatorProfile,
fieldName: 'displayName'
Expand Down Expand Up @@ -44,6 +82,14 @@ export const mapCreatorListItem = (
): CreatorListItem => {
warnIfUnexpectedNullCreatorField(creator, 'displayName');

logIfFieldTypeMismatch(creator, 'id');
logIfFieldTypeMismatch(creator, 'handle');
logIfFieldTypeMismatch(creator, 'displayName');
logIfFieldTypeMismatch(creator, 'avatarUrl');
logIfFieldTypeMismatch(creator, 'isVerified');
logIfFieldTypeMismatch(creator, 'createdAt');
logIfFieldTypeMismatch(creator, 'updatedAt');

return {
id: creator.id,
name: safeRead(creator, 'displayName', null),
Expand Down
Loading