diff --git a/package-lock.json b/package-lock.json index a37d9e7..f2e5a37 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "dependencies": { "@pnpm/tabtab": "0.5.4", "@todoist/cli-core": "2.0.0", - "@todoist/comms-sdk": "5.2.2", + "@todoist/comms-sdk": "6.0.0", "chalk": "5.6.2", "commander": "14.0.3", "marked": "18.0.3", @@ -2717,9 +2717,9 @@ } }, "node_modules/@todoist/comms-sdk": { - "version": "5.2.2", - "resolved": "https://registry.npmjs.org/@todoist/comms-sdk/-/comms-sdk-5.2.2.tgz", - "integrity": "sha512-u2SGZzAAgmw1u1XkzTg5NNvZlLlygLxra7PP9UtTFmG8dafoCPLnBcxKgBHStDiRJnFbP6dp60Yl3islNuEwsw==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/@todoist/comms-sdk/-/comms-sdk-6.0.0.tgz", + "integrity": "sha512-77YgBt4pozOjA5mqvHOGpQF3/KgsU7Ne7j7WUInhgEnvW7P1rpfb6di3hGSUI/t0ojnpus4IZmO252Z/URGvog==", "license": "MIT", "dependencies": { "@doist/sdk-kmp": "0.6.0", diff --git a/package.json b/package.json index dffc205..aefc589 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "dependencies": { "@pnpm/tabtab": "0.5.4", "@todoist/cli-core": "2.0.0", - "@todoist/comms-sdk": "5.2.2", + "@todoist/comms-sdk": "6.0.0", "chalk": "5.6.2", "commander": "14.0.3", "marked": "18.0.3", diff --git a/src/commands/comment/comment.test.ts b/src/commands/comment/comment.test.ts index 10a9956..a971e56 100644 --- a/src/commands/comment/comment.test.ts +++ b/src/commands/comment/comment.test.ts @@ -1,5 +1,7 @@ import { captureConsole, createTestProgram, mockStdin } from '@todoist/cli-core/testing' +import { CommsApi } from '@todoist/comms-sdk' import { beforeEach, describe, expect, it, vi } from 'vitest' +import { mockJsonFetch } from '../../lib/__fixtures__/custom-fetch.js' const apiMocks = vi.hoisted(() => ({ getCommsClient: vi.fn(), @@ -231,6 +233,35 @@ describe('comment delete', () => { expect(consoleSpy).toHaveBeenCalledWith('Comment 300 deleted.') }) + it('succeeds when the API answers the delete with the deleted comment', async () => { + const deleted = { + id: '300', + thread_id: 'TH500', + channel_id: 'CH100', + workspace_id: 10, + creator: 1, + content: '-', + posted_ts: 1772409600, + deleted: true, + deleted_by: 1, + version: 1, + reactions: {}, + } + const customFetch = mockJsonFetch(deleted) + const client = new CommsApi('test-token', { customFetch }) + vi.spyOn(client.comments, 'getComment').mockResolvedValue( + createCommentFixture('300') as never, + ) + vi.spyOn(client.users, 'getSessionUser').mockResolvedValue({ id: 1 } as never) + apiMocks.getCommsClient.mockResolvedValue(client) + const consoleSpy = captureConsole('log') + + await createProgram().parseAsync(['node', 'tdc', 'comment', 'delete', '300', '--yes']) + + expect(customFetch).toHaveBeenCalledTimes(1) + expect(consoleSpy).toHaveBeenCalledWith('Comment 300 deleted.') + }) + it('prompts for confirmation without --yes', async () => { const client = createClient() apiMocks.getCommsClient.mockResolvedValue(client) diff --git a/src/commands/msg/msg.test.ts b/src/commands/msg/msg.test.ts index 6fddfc5..b28dbd7 100644 --- a/src/commands/msg/msg.test.ts +++ b/src/commands/msg/msg.test.ts @@ -1,5 +1,7 @@ import { captureConsole, createTestProgram, mockStdin } from '@todoist/cli-core/testing' +import { CommsApi } from '@todoist/comms-sdk' import { beforeEach, describe, expect, it, vi } from 'vitest' +import { mockJsonFetch } from '../../lib/__fixtures__/custom-fetch.js' const apiMocks = vi.hoisted(() => ({ getCommsClient: vi.fn(), @@ -93,6 +95,33 @@ describe('msg delete', () => { expect(consoleSpy).toHaveBeenCalledWith('Message 200 deleted.') }) + it('succeeds when the API answers the delete with the deleted message', async () => { + const deleted = { + id: '200', + conversation_id: 'CV42', + workspace_id: 10, + creator: 1, + content: '-', + posted_ts: 1772409600, + deleted: true, + deleted_by: 1, + version: 1, + } + const customFetch = mockJsonFetch(deleted) + const client = new CommsApi('test-token', { customFetch }) + vi.spyOn(client.conversationMessages, 'getMessage').mockResolvedValue( + createMessageFixture('200') as never, + ) + vi.spyOn(client.users, 'getSessionUser').mockResolvedValue({ id: 1 } as never) + apiMocks.getCommsClient.mockResolvedValue(client) + const consoleSpy = captureConsole('log') + + await createProgram().parseAsync(['node', 'tdc', 'msg', 'delete', '200', '--yes']) + + expect(customFetch).toHaveBeenCalledTimes(1) + expect(consoleSpy).toHaveBeenCalledWith('Message 200 deleted.') + }) + it('prompts for confirmation without --yes', async () => { const client = createClient() apiMocks.getCommsClient.mockResolvedValue(client) diff --git a/src/commands/thread/thread.test.ts b/src/commands/thread/thread.test.ts index 2141eb6..9fd05f8 100644 --- a/src/commands/thread/thread.test.ts +++ b/src/commands/thread/thread.test.ts @@ -7,8 +7,9 @@ import { createTestProgram, mockStdin, } from '@todoist/cli-core/testing' -import { CommsApi, type CustomFetch, generateId } from '@todoist/comms-sdk' +import { CommsApi, generateId } from '@todoist/comms-sdk' import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' +import { mockJsonFetch } from '../../lib/__fixtures__/custom-fetch.js' const apiMocks = vi.hoisted(() => ({ getCommsClient: vi.fn(), @@ -409,14 +410,7 @@ describe.each(['reply', 'close', 'reopen'] as const)('thread %s notifications', creator: 2, posted_ts: 1772409600, } - const customFetch = vi.fn().mockResolvedValue({ - ok: true, - status: 200, - statusText: 'OK', - headers: {}, - text: async () => JSON.stringify(comment), - json: async () => comment, - }) + const customFetch = mockJsonFetch(comment) const client = new CommsApi('test-token', { customFetch }) vi.spyOn(client.threads, 'getThread').mockResolvedValue({ ...createThreadFixture(threadId), diff --git a/src/lib/__fixtures__/custom-fetch.ts b/src/lib/__fixtures__/custom-fetch.ts new file mode 100644 index 0000000..6ed24a1 --- /dev/null +++ b/src/lib/__fixtures__/custom-fetch.ts @@ -0,0 +1,19 @@ +import type { CustomFetch } from '@todoist/comms-sdk' +import { vi } from 'vitest' + +/** + * Builds a `customFetch` stub that answers every request with a 200 and the given JSON body, to run + * the real SDK client without a network. + * + * @param body - The raw wire payload (snake_case, `*_ts` epoch seconds) to answer with. + */ +export function mockJsonFetch(body: unknown) { + return vi.fn().mockResolvedValue({ + ok: true, + status: 200, + statusText: 'OK', + headers: {}, + text: async () => JSON.stringify(body), + json: async () => body, + }) +}