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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Comment thread
rfgamaral marked this conversation as resolved.
Resolved
"chalk": "5.6.2",
"commander": "14.0.3",
"marked": "18.0.3",
Expand Down
31 changes: 31 additions & 0 deletions src/commands/comment/comment.test.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions src/commands/msg/msg.test.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 3 additions & 9 deletions src/commands/thread/thread.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -409,14 +410,7 @@ describe.each(['reply', 'close', 'reopen'] as const)('thread %s notifications',
creator: 2,
posted_ts: 1772409600,
}
const customFetch = vi.fn<CustomFetch>().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),
Expand Down
19 changes: 19 additions & 0 deletions src/lib/__fixtures__/custom-fetch.ts
Original file line number Diff line number Diff line change
@@ -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<CustomFetch>().mockResolvedValue({
ok: true,
status: 200,
statusText: 'OK',
headers: {},
text: async () => JSON.stringify(body),
json: async () => body,
})
}
Loading