Skip to content
Open
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
51 changes: 32 additions & 19 deletions packages/outpost/queue/src/__tests__/tracker-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ vi.mock('@copilotkit/outpost/db', () => ({
const mockGetPlugin = vi.fn();
const mockIsInternalTracker = vi.fn().mockReturnValue(false);
const mockComputeHash = vi.fn().mockReturnValue('abc123hash');
const mockIsEchoEvent = vi.fn().mockResolvedValue(false);
const mockRecordSyncEvent = vi.fn().mockResolvedValue(undefined);

const mockEngine = {
getPlugin: mockGetPlugin,
isInternalTracker: mockIsInternalTracker,
computeHash: mockComputeHash,
isEchoEvent: mockIsEchoEvent,
recordSyncEvent: mockRecordSyncEvent,
};

Expand Down Expand Up @@ -119,14 +117,18 @@ describe('handleTrackerSync', () => {
expect(result.error).toContain('No external link found');
});

it('skips push when echo is detected', async () => {
it('pushes what the job says, without consulting an echo guard', async () => {
// The echo guard lives at the fan-out, in SyncEngine.onTicketChange:
// a change that comes back from a tracker is recognised there,
// before it becomes a job. A job is a promise to push, and the
// operator-facing force sync enqueues jobs directly and expects
// every one of them to push.
const mockPlugin = {
name: 'github',
pushStatusChange: vi.fn(),
pushStatusChange: vi.fn().mockResolvedValue(undefined),
};
mockGetPlugin.mockReturnValue(mockPlugin);
mockPrismaTicketExternalLink.findUnique.mockResolvedValue(sampleLink);
mockIsEchoEvent.mockResolvedValue(true);

const result = await handler(
{
Expand All @@ -139,8 +141,16 @@ describe('handleTrackerSync', () => {
);

expect(result.success).toBe(true);
expect(result.data).toEqual({ skipped: true, reason: 'echo detected' });
expect(mockPlugin.pushStatusChange).not.toHaveBeenCalled();
expect(mockPlugin.pushStatusChange).toHaveBeenCalledTimes(1);
expect(mockRecordSyncEvent).toHaveBeenCalledWith(
'outpost',
'github',
'ticket',
'tkt-1',
'status_change',
'abc123hash',
'success',
);
});

it('pushes status_change and records success', async () => {
Expand All @@ -150,7 +160,6 @@ describe('handleTrackerSync', () => {
};
mockGetPlugin.mockReturnValue(mockPlugin);
mockPrismaTicketExternalLink.findUnique.mockResolvedValue(sampleLink);
mockIsEchoEvent.mockResolvedValue(false);

const result = await handler(
{
Expand All @@ -168,8 +177,13 @@ describe('handleTrackerSync', () => {
'RESOLVED',
);
expect(mockRecordSyncEvent).toHaveBeenCalledWith(
'outpost', 'github', 'ticket', 'tkt-1',
'status_change', 'abc123hash', 'success',
'outpost',
'github',
'ticket',
'tkt-1',
'status_change',
'abc123hash',
'success',
);
});

Expand All @@ -180,7 +194,6 @@ describe('handleTrackerSync', () => {
};
mockGetPlugin.mockReturnValue(mockPlugin);
mockPrismaTicketExternalLink.findUnique.mockResolvedValue(sampleLink);
mockIsEchoEvent.mockResolvedValue(false);

const result = await handler(
{
Expand All @@ -206,7 +219,6 @@ describe('handleTrackerSync', () => {
};
mockGetPlugin.mockReturnValue(mockPlugin);
mockPrismaTicketExternalLink.findUnique.mockResolvedValue(sampleLink);
mockIsEchoEvent.mockResolvedValue(false);

const result = await handler(
{
Expand All @@ -232,7 +244,6 @@ describe('handleTrackerSync', () => {
};
mockGetPlugin.mockReturnValue(mockPlugin);
mockPrismaTicketExternalLink.findUnique.mockResolvedValue(sampleLink);
mockIsEchoEvent.mockResolvedValue(false);

const result = await handler(
{
Expand All @@ -247,8 +258,14 @@ describe('handleTrackerSync', () => {
expect(result.success).toBe(false);
expect(result.error).toBe('API 500');
expect(mockRecordSyncEvent).toHaveBeenCalledWith(
'outpost', 'github', 'ticket', 'tkt-1',
'status_change', 'abc123hash', 'failure', 'API 500',
'outpost',
'github',
'ticket',
'tkt-1',
'status_change',
'abc123hash',
'failure',
'API 500',
);
});

Expand All @@ -264,8 +281,6 @@ describe('handleTrackerSync', () => {
plugin: 'linear',
externalId: 'lin-99',
});
mockIsEchoEvent.mockResolvedValue(false);

const member = { id: 'tm-1', name: 'Alice', email: 'alice@example.com' };

const result = await handler(
Expand All @@ -289,7 +304,6 @@ describe('handleTrackerSync', () => {
const mockPlugin = { name: 'github' };
mockGetPlugin.mockReturnValue(mockPlugin);
mockPrismaTicketExternalLink.findUnique.mockResolvedValue(sampleLink);
mockIsEchoEvent.mockResolvedValue(false);

const result = await handler(
{
Expand All @@ -312,7 +326,6 @@ describe('handleTrackerSync', () => {
};
mockGetPlugin.mockReturnValue(mockPlugin);
mockPrismaTicketExternalLink.findUnique.mockResolvedValue(sampleLink);
mockIsEchoEvent.mockResolvedValue(false);

const ctx = makeContext();
await handler(
Expand Down
34 changes: 16 additions & 18 deletions packages/outpost/queue/src/handlers/tracker-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,21 @@ export function createTrackerSyncHandler(engine: SyncEngine) {

await context.reportProgress(40);

// Echo detection: compute hash and check before pushing
const payloadHash = engine.computeHash(ticketId, targetPlugin, action, changeData);
const isEcho = await engine.isEchoEvent(
ticketId,
'outpost', // We are pushing FROM outpost TO target
targetPlugin,
payloadHash,
);

if (isEcho) {
return {
success: true,
data: { skipped: true, reason: 'echo detected' },
};
}
// NO echo check here, and that is deliberate.
//
// The guard belongs at the fan-out, in SyncEngine.onTicketChange: a
// change that comes back from a tracker is recognised there, before
// it is turned into a job. Checking here instead was wrong twice
// over. The guard used to look for a record in the reverse direction
// of the one a push is recorded in, so it never fired at all; and
// once it is fixed to look the right way, it would fire on the
// record this very job created — the engine writes a pending record
// before enqueueing — and the push would never happen.
//
// A job is a promise to push, and this handler keeps it. The
// operator-facing force sync enqueues jobs directly, without the
// engine, and expects every one of them to push.
const payloadHash = engine.computeHash(ticketId, action, changeData);

await context.reportProgress(50);

Expand All @@ -107,9 +107,7 @@ export function createTrackerSyncHandler(engine: SyncEngine) {

await context.reportProgress(100);

console.log(
`[TrackerSync] Pushed ${action} for ticket ${ticketId} to ${targetPlugin}`,
);
console.log(`[TrackerSync] Pushed ${action} for ticket ${ticketId} to ${targetPlugin}`);

return {
success: true,
Expand Down
Loading