From 1f4484295b99019ee706d428b139fa886df1955b Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Fri, 4 Dec 2026 14:40:45 +0000 Subject: [PATCH 1/3] feat(conversation): add tdc conversation mark-unread The inverse of `tdc conversation mark-read`, on the same read-state runner, with the options of `tdc thread mark-unread`: one or more refs (or --stdin), --yes for a bulk run, --dry-run, --json rows, and --from for a single conversation. A conversation's unread marker is its first unread message, so --from marks that message and everything after it unread. Without --from it marks the latest message unread, as "Mark unread" on a conversation does in the Comms app, rather than the whole history. The --json rows report lastReadObjIndex, the last message still read, as the thread rows do. --- docs/SPEC.md | 16 ++ skills/comms-cli/SKILL.md | 6 +- .../conversation/conversation.test.ts | 203 ++++++++++++++++++ src/commands/conversation/index.ts | 29 +++ src/commands/conversation/mark-unread.ts | 99 +++++++++ src/lib/api.ts | 1 + src/lib/skills/content.ts | 6 +- 7 files changed, 356 insertions(+), 4 deletions(-) create mode 100644 src/commands/conversation/mark-unread.ts diff --git a/docs/SPEC.md b/docs/SPEC.md index 0f86d40..1d4e628 100644 --- a/docs/SPEC.md +++ b/docs/SPEC.md @@ -299,6 +299,22 @@ Options: - `--dry-run` - Show what would happen without executing - `--json` - One `{ id, isRead }` row per conversation +#### `tdc conversation mark-unread [conversation-refs...]` + +Mark conversations unread for the current user. By default this marks the latest message unread, as "Mark unread" on a conversation does in the Comms app; `--from` marks unread from an earlier message onward. Conversations already unread from that message or an earlier one are reported and left alone. + +Arguments: + +- `conversation-refs` - Conversation IDs or Comms URLs + +Options: + +- `--from ` - Mark unread from this message onward (single conversation only) +- `--yes` - Confirm a bulk run (required to execute more than one conversation) +- `--stdin` - Read conversation refs from stdin, one per line (`#` lines are skipped) +- `--dry-run` - Show what would happen without executing +- `--json` - One `{ id, isRead, lastReadObjIndex }` row per conversation, where `lastReadObjIndex` is the last message still read (`-1` when every message is unread) + --- ### Conversation Message Commands diff --git a/skills/comms-cli/SKILL.md b/skills/comms-cli/SKILL.md index 3a12e0b..bb23437 100644 --- a/skills/comms-cli/SKILL.md +++ b/skills/comms-cli/SKILL.md @@ -206,6 +206,8 @@ tdc conversation reply "content" --file ./a.png # Attach a file (repeatab tdc conversation reply "content" --no-mark-read # Reply but leave the conversation unread (a reply marks it read by default) tdc conversation mark-read # Mark a conversation read tdc conversation mark-read --yes # Mark multiple conversations read +tdc conversation mark-unread # Mark a conversation's latest message unread, as the Comms app does +tdc conversation mark-unread --from # Mark unread from a message onward tdc conversation done # Preview conversation archive (requires --yes to execute) tdc conversation done --yes # Archive conversation tdc conversation done --yes --json # Archive and return status as JSON @@ -478,7 +480,7 @@ Mutating commands accept `--dry-run` to preview the operation without making the Run without --dry-run to execute. ``` -Destructive commands (`channel delete`, `comment delete`, `msg delete`, `groups delete`, `thread delete`, `thread done`/`undone`, `conversation done`/`undone`, bulk `thread mark-read`/`mark-unread` and bulk `conversation mark-read`) only preview without `--yes`: they print `Would …` and `Use --yes to confirm.` and change nothing. Under `--json`, a missing `--yes` (without `--dry-run`) fails with `CONFIRMATION_REQUIRED` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. `--dry-run --json` prints one `{ "status": "preview", "command", "messages" }` object instead of the text preview; bulk `mark-read`/`mark-unread` keep their per-thread or per-conversation rows with `dryRun: true`. +Destructive commands (`channel delete`, `comment delete`, `msg delete`, `groups delete`, `thread delete`, `thread done`/`undone`, `conversation done`/`undone`, and bulk `mark-read`/`mark-unread` on threads and conversations) only preview without `--yes`: they print `Would …` and `Use --yes to confirm.` and change nothing. Under `--json`, a missing `--yes` (without `--dry-run`) fails with `CONFIRMATION_REQUIRED` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. `--dry-run --json` prints one `{ "status": "preview", "command", "messages" }` object instead of the text preview; bulk `mark-read`/`mark-unread` keep their per-thread or per-conversation rows with `dryRun: true`. ## Reference System @@ -499,7 +501,7 @@ echo "Quick reply" | tdc conversation reply --stdin If neither a content argument nor `--stdin` is given, the CLI opens `$EDITOR` for interactive input. In non-TTY environments (e.g. when called by an agent or in a pipeline), the editor is automatically skipped and the command fails fast with an actionable error message. Use `--non-interactive` to force this behavior even in a TTY, or `--interactive` to override auto-detection. -`tdc thread mark-read`, `tdc thread mark-unread` and `tdc conversation mark-read` read their refs from stdin with `--stdin`, one per line (`#` lines are skipped), added to any refs passed as arguments: +`tdc thread mark-read`, `tdc thread mark-unread`, `tdc conversation mark-read` and `tdc conversation mark-unread` read their refs from stdin with `--stdin`, one per line (`#` lines are skipped), added to any refs passed as arguments: ```bash printf "id:CbT8n2Kp4Qx6Rz9Lm3Va\nid:CbT9m4Qr7Vz2Nx8Lp5Sa\n" | tdc thread mark-read --stdin --yes diff --git a/src/commands/conversation/conversation.test.ts b/src/commands/conversation/conversation.test.ts index 17b7553..97cd575 100644 --- a/src/commands/conversation/conversation.test.ts +++ b/src/commands/conversation/conversation.test.ts @@ -20,6 +20,7 @@ const apiMocks = vi.hoisted(() => ({ const refsMocks = vi.hoisted(() => ({ resolveConversationId: vi.fn((ref: string) => ref), + resolveMessageId: vi.fn((ref: string) => ref), resolveWorkspaceRef: vi.fn(), resolveUserRefs: vi.fn(), })) @@ -92,12 +93,14 @@ function createClient({ messagesByConversation = {}, pinnedByConversation = {}, users = {}, + messagesById = {}, }: { activeConversations?: TestConversation[] archivedConversations?: TestConversation[] messagesByConversation?: Record>> pinnedByConversation?: Record>> users?: Record + messagesById?: Record }) { const conversationsById = new Map( [...activeConversations, ...archivedConversations].map((conversation) => [ @@ -130,8 +133,12 @@ function createClient({ markRead: vi.fn(async (_args: { id: string; messageId: string }) => ({ status: 'ok', })), + markUnread: vi.fn(async (_args: { id: string; objIndex: number }) => ({ + status: 'ok', + })), }, conversationMessages: { + getMessage: vi.fn(async (id: string) => messagesById[id]), getPinnedMessages: vi.fn( async (conversationId: string) => pinnedByConversation[conversationId] ?? [], ), @@ -2115,3 +2122,199 @@ describe('conversation mark-read', () => { ]) }) }) + +describe('conversation mark-unread', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + // Conversation 42 runs to message index 7; M5 is its message at index 5, M9 belongs elsewhere. + function setupMarkUnread(unread: Array<{ conversationId: string; objIndex: number }> = []) { + const client = createClient({ + activeConversations: [ + { ...createConversation(42, [1, 2], '2026-03-08T10:00:00.000Z'), lastObjIndex: 7 }, + { ...createConversation(43, [1, 3], '2026-03-08T11:00:00.000Z'), lastObjIndex: 2 }, + ], + messagesById: { + M5: { id: 'M5', conversationId: '42', objIndex: 5 }, + M9: { id: 'M9', conversationId: '99', objIndex: 9 }, + }, + }) + client.conversations.getUnread.mockResolvedValue({ + data: unread.map((entry) => ({ ...entry, directMention: false })), + version: 1, + }) + apiMocks.getCommsClient.mockResolvedValue(client) + return { client, program: createProgram() } + } + + it('marks the latest message unread by default, as the Comms app does', async () => { + const { client, program } = setupMarkUnread() + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-unread', '42']) + + expect(client.conversations.markUnread).toHaveBeenCalledWith({ id: '42', objIndex: 7 }) + expect(logSpy).toHaveBeenCalledWith( + 'Conversation 42 marked unread from its latest message.', + ) + }) + + it('marks unread from the message given with --from', async () => { + const { client, program } = setupMarkUnread() + const logSpy = captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-unread', + '42', + '--from', + 'M5', + ]) + + expect(client.conversationMessages.getMessage).toHaveBeenCalledWith('M5') + expect(client.conversations.markUnread).toHaveBeenCalledWith({ id: '42', objIndex: 5 }) + expect(logSpy).toHaveBeenCalledWith('Conversation 42 marked unread from message M5.') + }) + + it('reports the last message still read in --json', async () => { + const { program } = setupMarkUnread() + const logSpy = captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-unread', + '42', + '--from', + 'M5', + '--json', + ]) + + expect(JSON.parse(String(logSpy.mock.calls.at(-1)?.[0]))).toEqual([ + { id: '42', isRead: false, lastReadObjIndex: 4 }, + ]) + }) + + it('leaves a conversation already unread from an earlier message unchanged', async () => { + const { client, program } = setupMarkUnread([{ conversationId: '42', objIndex: 3 }]) + const logSpy = captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-unread', + '42', + '--from', + 'M5', + '--json', + ]) + + expect(client.conversations.markUnread).not.toHaveBeenCalled() + expect(JSON.parse(String(logSpy.mock.calls.at(-1)?.[0]))).toEqual([ + { id: '42', isRead: false, lastReadObjIndex: 2 }, + ]) + }) + + it('says so when the latest message is already unread', async () => { + const { client, program } = setupMarkUnread([{ conversationId: '42', objIndex: 7 }]) + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-unread', '42']) + + expect(client.conversations.markUnread).not.toHaveBeenCalled() + expect(logSpy).toHaveBeenCalledWith( + 'Conversation 42 is already unread from its latest message.', + ) + }) + + it('moves the marker back when it sits after the --from message', async () => { + const { client, program } = setupMarkUnread([{ conversationId: '42', objIndex: 6 }]) + captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-unread', + '42', + '--from', + 'M5', + ]) + + expect(client.conversations.markUnread).toHaveBeenCalledWith({ id: '42', objIndex: 5 }) + }) + + it('refuses --from with more than one conversation', async () => { + const { client, program } = setupMarkUnread() + + await expect( + program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-unread', + '42', + '43', + '--from', + 'M5', + '--yes', + ]), + ).rejects.toMatchObject({ + code: 'CONFLICTING_OPTIONS', + message: + '--from applies to a single conversation; pass one conversation ref when using it.', + }) + expect(client.conversations.markUnread).not.toHaveBeenCalled() + }) + + it('refuses a --from message from another conversation', async () => { + const { client, program } = setupMarkUnread() + + await expect( + program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-unread', + '42', + '--from', + 'M9', + ]), + ).rejects.toMatchObject({ + code: 'INVALID_REF', + message: 'Message M9 belongs to conversation 99, not 42.', + }) + expect(client.conversations.markUnread).not.toHaveBeenCalled() + }) + + it('previews several conversations without --yes and marks them with it', async () => { + const { client, program } = setupMarkUnread() + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-unread', '42', '43']) + + expect(client.conversations.markUnread).not.toHaveBeenCalled() + expect(logSpy).toHaveBeenCalledWith( + 'Would mark unread conversation 42 from its latest message.', + ) + expect(logSpy).toHaveBeenCalledWith('Use --yes to confirm.') + + await createProgram().parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-unread', + '42', + '43', + '--yes', + ]) + + expect(client.conversations.markUnread).toHaveBeenCalledWith({ id: '42', objIndex: 7 }) + expect(client.conversations.markUnread).toHaveBeenCalledWith({ id: '43', objIndex: 2 }) + }) +}) diff --git a/src/commands/conversation/index.ts b/src/commands/conversation/index.ts index 575c1ea..d62f014 100644 --- a/src/commands/conversation/index.ts +++ b/src/commands/conversation/index.ts @@ -4,6 +4,7 @@ import { collect } from '../../lib/options.js' import { markConversationDone, markConversationUndone } from './archive.js' import { listConversations } from './list.js' import { markConversationRead } from './mark-read.js' +import { markConversationUnread } from './mark-unread.js' import { muteConversation } from './mute.js' import { replyToConversation } from './reply.js' import { unmuteConversation } from './unmute.js' @@ -205,6 +206,34 @@ Examples: return markConversationRead(refs, options) }) + const markUnreadCmd = conversation + .command('mark-unread [conversation-refs...]') + .description('Mark a conversation unread for the current user; inverse of mark-read') + .option( + '--from ', + 'Mark unread from this message onward (single conversation only; default: its latest message)', + ) + .option('--yes', 'Skip confirmation for bulk operations') + .option('--stdin', 'Read conversation refs from stdin, one per line') + .option('--dry-run', 'Show what would happen without executing') + .option('--json', 'Output result as JSON') + .addHelpText( + 'after', + ` +Examples: + tdc conversation mark-unread id:CbV8n2Kp4Qx6Rz9Lm3Va + tdc conversation mark-unread id:CbV8n2Kp4Qx6Rz9Lm3Va --from id:CbM8n2Kp4Qx6Rz9Lm3Va + tdc conversation mark-unread id:CbV8n2Kp4Qx6Rz9Lm3Va id:CbV9m4Qr7Vz2Nx8Lp5Sa --yes + printf "id:CbV8n2Kp4Qx6Rz9Lm3Va\\nid:CbV9m4Qr7Vz2Nx8Lp5Sa\\n" | tdc conversation mark-unread --stdin --yes`, + ) + .action((refs, options) => { + if (refs.length === 0 && !options.stdin) { + markUnreadCmd.help() + return + } + return markConversationUnread(refs, options) + }) + conversation .command('mute ') .description('Mute a conversation (stop notifications)') diff --git a/src/commands/conversation/mark-unread.ts b/src/commands/conversation/mark-unread.ts new file mode 100644 index 0000000..9e783cf --- /dev/null +++ b/src/commands/conversation/mark-unread.ts @@ -0,0 +1,99 @@ +import type { CommsApi, Conversation } from '@todoist/comms-sdk' +import { CliError } from '../../lib/errors.js' +import type { MutationOptions, StdinOptions } from '../../lib/options.js' +import { type ReadState, runReadStateMutation } from '../../lib/read-state.js' +import { resolveMessageId } from '../../lib/refs.js' +import { CONVERSATION_READ_STATE } from './helpers.js' + +export type MarkConversationUnreadOptions = MutationOptions & StdinOptions & { from?: string } + +type MarkUnreadStatus = { + id: string + isRead: false + /** Object index of the last message still read; `-1` when every message is unread. */ + lastReadObjIndex: number +} + +/** + * A conversation's unread marker is the index of its first unread message: marking + * unread from message N makes N and everything after it unread. Without --from this + * marks the latest message unread, as "Mark unread" on a conversation does in the + * Comms app, rather than sending the reader back to the start of its history. + */ +export async function markConversationUnread( + refs: string[], + options: MarkConversationUnreadOptions, +): Promise { + const from = options.from + + await runReadStateMutation( + CONVERSATION_READ_STATE, + refs, + options, + { + verb: 'unread', + validateRefs: (rawRefs) => { + if (from !== undefined && rawRefs.length > 1) { + throw new CliError( + 'CONFLICTING_OPTIONS', + '--from applies to a single conversation; pass one conversation ref when using it.', + ) + } + }, + plan: async (client, conversationId) => { + const fromObjIndex = + from === undefined + ? undefined + : await resolveFromObjIndex(client, conversationId, from) + function firstUnread(state: ReadState): number { + return fromObjIndex ?? state.item.lastObjIndex + } + return { + scope: + from === undefined ? ' from its latest message' : ` from message ${from}`, + // Already unread from that message or an earlier one: nothing to move. + isUnchanged: (state) => + state.unreadObjIndex !== null && state.unreadObjIndex <= firstUnread(state), + status: (state, outcome) => ({ + id: conversationId, + isRead: false, + lastReadObjIndex: + (outcome === 'unchanged' + ? (state.unreadObjIndex ?? firstUnread(state)) + : firstUnread(state)) - 1, + }), + apply: async (client, state) => { + const objIndex = firstUnread(state) + await client.conversations.markUnread({ id: conversationId, objIndex }) + return objIndex + }, + } + }, + }, + ) +} + +/** Turns a message ref into its `objIndex`, checking it belongs to the conversation. */ +async function resolveFromObjIndex( + client: CommsApi, + conversationId: string, + messageRef: string, +): Promise { + const messageId = resolveMessageId(messageRef) + const message = await client.conversationMessages.getMessage(messageId) + + if (message.conversationId !== conversationId) { + throw new CliError( + 'INVALID_REF', + `Message ${messageId} belongs to conversation ${message.conversationId}, not ${conversationId}.`, + ) + } + if (typeof message.objIndex !== 'number') { + throw new CliError( + 'INVALID_REF', + `Message ${messageId} has no object index; cannot mark unread from it.`, + ) + } + + return message.objIndex +} diff --git a/src/lib/api.ts b/src/lib/api.ts index 8ed7780..0e1a635 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -84,6 +84,7 @@ const API_SPINNER_MESSAGES: Record "content" --file ./a.png # Attach a file (repeatab tdc conversation reply "content" --no-mark-read # Reply but leave the conversation unread (a reply marks it read by default) tdc conversation mark-read # Mark a conversation read tdc conversation mark-read --yes # Mark multiple conversations read +tdc conversation mark-unread # Mark a conversation's latest message unread, as the Comms app does +tdc conversation mark-unread --from # Mark unread from a message onward tdc conversation done # Preview conversation archive (requires --yes to execute) tdc conversation done --yes # Archive conversation tdc conversation done --yes --json # Archive and return status as JSON @@ -482,7 +484,7 @@ Mutating commands accept \`--dry-run\` to preview the operation without making t Run without --dry-run to execute. \`\`\` -Destructive commands (\`channel delete\`, \`comment delete\`, \`msg delete\`, \`groups delete\`, \`thread delete\`, \`thread done\`/\`undone\`, \`conversation done\`/\`undone\`, bulk \`thread mark-read\`/\`mark-unread\` and bulk \`conversation mark-read\`) only preview without \`--yes\`: they print \`Would …\` and \`Use --yes to confirm.\` and change nothing. Under \`--json\`, a missing \`--yes\` (without \`--dry-run\`) fails with \`CONFIRMATION_REQUIRED\` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. \`--dry-run --json\` prints one \`{ "status": "preview", "command", "messages" }\` object instead of the text preview; bulk \`mark-read\`/\`mark-unread\` keep their per-thread or per-conversation rows with \`dryRun: true\`. +Destructive commands (\`channel delete\`, \`comment delete\`, \`msg delete\`, \`groups delete\`, \`thread delete\`, \`thread done\`/\`undone\`, \`conversation done\`/\`undone\`, and bulk \`mark-read\`/\`mark-unread\` on threads and conversations) only preview without \`--yes\`: they print \`Would …\` and \`Use --yes to confirm.\` and change nothing. Under \`--json\`, a missing \`--yes\` (without \`--dry-run\`) fails with \`CONFIRMATION_REQUIRED\` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. \`--dry-run --json\` prints one \`{ "status": "preview", "command", "messages" }\` object instead of the text preview; bulk \`mark-read\`/\`mark-unread\` keep their per-thread or per-conversation rows with \`dryRun: true\`. ## Reference System @@ -503,7 +505,7 @@ echo "Quick reply" | tdc conversation reply --stdin If neither a content argument nor \`--stdin\` is given, the CLI opens \`$EDITOR\` for interactive input. In non-TTY environments (e.g. when called by an agent or in a pipeline), the editor is automatically skipped and the command fails fast with an actionable error message. Use \`--non-interactive\` to force this behavior even in a TTY, or \`--interactive\` to override auto-detection. -\`tdc thread mark-read\`, \`tdc thread mark-unread\` and \`tdc conversation mark-read\` read their refs from stdin with \`--stdin\`, one per line (\`#\` lines are skipped), added to any refs passed as arguments: +\`tdc thread mark-read\`, \`tdc thread mark-unread\`, \`tdc conversation mark-read\` and \`tdc conversation mark-unread\` read their refs from stdin with \`--stdin\`, one per line (\`#\` lines are skipped), added to any refs passed as arguments: \`\`\`bash printf "id:CbT8n2Kp4Qx6Rz9Lm3Va\\nid:CbT9m4Qr7Vz2Nx8Lp5Sa\\n" | tdc thread mark-read --stdin --yes From ef60218188bc4c58d108bde15ac4ac60532f0307 Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Fri, 4 Dec 2026 14:48:58 +0000 Subject: [PATCH 2/3] docs(conversation): list mark-unread in the README and test its help guard --- README.md | 1 + src/commands/conversation/conversation.test.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/README.md b/README.md index 3cdc841..39a69ef 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ tdc conversation view --pinned # only the pinned messages tdc conversation done --yes # archive a conversation tdc conversation undone --yes # unarchive it tdc conversation mark-read # mark a conversation read +tdc conversation mark-unread # mark its latest message unread again (--from for more) tdc msg view # view a conversation message tdc msg pin # pin a message for everyone in the chat (unpin to undo) tdc comment pin # pin a comment for everyone in the thread (unpin to undo) diff --git a/src/commands/conversation/conversation.test.ts b/src/commands/conversation/conversation.test.ts index 97cd575..aac09e2 100644 --- a/src/commands/conversation/conversation.test.ts +++ b/src/commands/conversation/conversation.test.ts @@ -2249,6 +2249,19 @@ describe('conversation mark-unread', () => { expect(client.conversations.markUnread).toHaveBeenCalledWith({ id: '42', objIndex: 5 }) }) + it('shows help instead of reading piped refs when given no refs', async () => { + const { client, program } = setupMarkUnread() + const stdout = captureStream('stdout') + mockStdin('42\n') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-unread']).catch(() => {}) + + expect(stdout.mock.calls.map((args) => String(args[0])).join('')).toContain( + 'Usage: tdc conversation mark-unread', + ) + expect(client.conversations.markUnread).not.toHaveBeenCalled() + }) + it('refuses --from with more than one conversation', async () => { const { client, program } = setupMarkUnread() From d1f0f6598e7cf8869cf8b39f6b5bd2f955bedb70 Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Fri, 4 Dec 2026 14:50:59 +0000 Subject: [PATCH 3/3] test(conversation): refuse a mark-unread --from message with no index --- .../conversation/conversation.test.ts | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/commands/conversation/conversation.test.ts b/src/commands/conversation/conversation.test.ts index aac09e2..5002550 100644 --- a/src/commands/conversation/conversation.test.ts +++ b/src/commands/conversation/conversation.test.ts @@ -100,7 +100,7 @@ function createClient({ messagesByConversation?: Record>> pinnedByConversation?: Record>> users?: Record - messagesById?: Record + messagesById?: Record }) { const conversationsById = new Map( [...activeConversations, ...archivedConversations].map((conversation) => [ @@ -2128,7 +2128,8 @@ describe('conversation mark-unread', () => { vi.clearAllMocks() }) - // Conversation 42 runs to message index 7; M5 is its message at index 5, M9 belongs elsewhere. + // Conversation 42 runs to message index 7; M5 is its message at index 5, M0 one with no + // index, and M9 belongs elsewhere. function setupMarkUnread(unread: Array<{ conversationId: string; objIndex: number }> = []) { const client = createClient({ activeConversations: [ @@ -2138,6 +2139,7 @@ describe('conversation mark-unread', () => { messagesById: { M5: { id: 'M5', conversationId: '42', objIndex: 5 }, M9: { id: 'M9', conversationId: '99', objIndex: 9 }, + M0: { id: 'M0', conversationId: '42' }, }, }) client.conversations.getUnread.mockResolvedValue({ @@ -2305,6 +2307,26 @@ describe('conversation mark-unread', () => { expect(client.conversations.markUnread).not.toHaveBeenCalled() }) + it('refuses a --from message that has no object index', async () => { + const { client, program } = setupMarkUnread() + + await expect( + program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-unread', + '42', + '--from', + 'M0', + ]), + ).rejects.toMatchObject({ + code: 'INVALID_REF', + message: 'Message M0 has no object index; cannot mark unread from it.', + }) + expect(client.conversations.markUnread).not.toHaveBeenCalled() + }) + it('previews several conversations without --yes and marks them with it', async () => { const { client, program } = setupMarkUnread() const logSpy = captureConsole('log')