117 lines
3.9 KiB
TypeScript
117 lines
3.9 KiB
TypeScript
import { ConnectionService } from '../global/database/connection.service';
|
|
import { NatsService } from '../global/nats/nats.service';
|
|
import { DakaoOutboxService } from './dakao-outbox.service';
|
|
import { DakaoResultSubjects } from './dakao.types';
|
|
|
|
function execResult<T>(value: T) {
|
|
return { exec: jest.fn().mockResolvedValue(value) };
|
|
}
|
|
|
|
function makeService(outboxModel: Record<string, unknown>) {
|
|
const connectionService = {
|
|
getModel: jest.fn().mockResolvedValue(outboxModel),
|
|
} as unknown as ConnectionService;
|
|
const natsService = {
|
|
publishJetStream: jest.fn().mockResolvedValue(undefined),
|
|
} as unknown as NatsService;
|
|
|
|
return {
|
|
service: new DakaoOutboxService(connectionService, natsService),
|
|
connectionService,
|
|
natsService,
|
|
};
|
|
}
|
|
|
|
describe('DakaoOutboxService', () => {
|
|
it('persists result subjects when enqueuing send-message commands', async () => {
|
|
const outboxModel = {
|
|
findOneAndUpdate: jest.fn().mockReturnValue(execResult({})),
|
|
updateOne: jest.fn().mockReturnValue(execResult({})),
|
|
};
|
|
const { service, natsService } = makeService(outboxModel);
|
|
const resultSubjects: DakaoResultSubjects = {
|
|
succeeded: 'staging.lolympus.dakao.chat.send-message.succeeded.v1',
|
|
failed: 'staging.lolympus.dakao.chat.send-message.failed.v1',
|
|
};
|
|
|
|
await service.enqueueAndPublish({
|
|
subject: 'staging.dakao.chat.send-message.v1',
|
|
eventId: 'lolympus:chat.send-message:chat-log-1',
|
|
envelope: {
|
|
schema: 'dakao.command.v1',
|
|
eventId: 'lolympus:chat.send-message:chat-log-1',
|
|
type: 'chat.send-message',
|
|
eventSource: 'lolympus',
|
|
correlationId: 'chat-log-1',
|
|
resultSubjects,
|
|
payload: {
|
|
chatroomId: '18474390111480678',
|
|
segments: [{ kind: 'text', text: 'done' }],
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(outboxModel.findOneAndUpdate).toHaveBeenCalledWith(
|
|
{ eventId: 'lolympus:chat.send-message:chat-log-1' },
|
|
expect.objectContaining({
|
|
$setOnInsert: expect.objectContaining({ resultSubjects }),
|
|
}),
|
|
expect.any(Object),
|
|
);
|
|
expect(natsService.publishJetStream).toHaveBeenCalledWith(
|
|
'staging.dakao.chat.send-message.v1',
|
|
expect.objectContaining({ resultSubjects }),
|
|
{ messageId: 'lolympus:chat.send-message:chat-log-1' },
|
|
);
|
|
});
|
|
|
|
it('restores send-message result subjects when sweeping legacy stale rows', async () => {
|
|
const previousNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = 'staging';
|
|
|
|
try {
|
|
const exec = jest.fn().mockResolvedValue([
|
|
{
|
|
eventId: 'lolympus:chat.send-message:chat-log-1',
|
|
type: 'chat.send-message',
|
|
eventSource: 'lolympus',
|
|
subject: 'staging.dakao.chat.send-message.v1',
|
|
correlationId: 'chat-log-1',
|
|
payload: {
|
|
chatroomId: '18474390111480678',
|
|
segments: [{ kind: 'text', text: 'done' }],
|
|
},
|
|
},
|
|
]);
|
|
const lean = jest.fn().mockReturnValue({ exec });
|
|
const limit = jest.fn().mockReturnValue({ lean });
|
|
const sort = jest.fn().mockReturnValue({ limit });
|
|
const outboxModel = {
|
|
find: jest.fn().mockReturnValue({ sort }),
|
|
};
|
|
const { service } = makeService(outboxModel);
|
|
const enqueue = jest
|
|
.spyOn(service, 'enqueueAndPublish')
|
|
.mockResolvedValue(undefined);
|
|
|
|
await expect(service.sweepStale({ staleMs: 1, limit: 1 })).resolves.toBe(
|
|
1,
|
|
);
|
|
|
|
expect(enqueue).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
envelope: expect.objectContaining({
|
|
resultSubjects: {
|
|
succeeded:
|
|
'staging.lolympus.dakao.chat.send-message.succeeded.v1',
|
|
failed: 'staging.lolympus.dakao.chat.send-message.failed.v1',
|
|
},
|
|
}),
|
|
}),
|
|
);
|
|
} finally {
|
|
process.env.NODE_ENV = previousNodeEnv;
|
|
}
|
|
});
|
|
});
|