Skip to content

refactor: declare tg chats list output once - #179

Merged
neoromantic merged 2 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/output-declaration
Nov 18, 2026
Merged

neoromantic merged 2 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/output-declaration

Conversation

@ertembiyik

@ertembiyik ertembiyik commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What and why

The trial you asked for on point 5 of #160, on one command. The data that tg chats list prints is declared once, in src/commands/chats/list-output.ts (82 lines), and its types, its docs table and an e2e check all come from that declaration. The output itself does not change: five invocations on the same made-up cache are byte-identical before and after. Refs #160

The declaration is a zod 4 schema with a description on every field:

export const chatItem = z.strictObject({
  id: z.int().describe('Marked peer ID: the user ID, `-N` for a basic group, `-100…` for a supergroup, channel or community'),
  type: z.enum(CHAT_TYPE_VALUES).describe('Chat type'),
  username: z.string().nullable().describe('Public username, without `@`'),
  lastMessageAt: z.int().nullable().describe('Time of the newest message, Unix milliseconds'),
  // …
})
export type ChatItem = z.infer<typeof chatItem>

Nothing new ships. zod is a devDependency. The command imports only the inferred type (import type), so it is not in the compiled binary (no zod strings in dist/tg, no zod module in the bundle's metafile), and tg chats list runs the same with node_modules/zod moved away. Only the tests and the docs script load the schema.

Which types it replaces

  • interface ChatItem in helpers.ts, interface ChatListItem in list.ts, and the inline PaginatedResult<ChatListItem> & { source: string; stale: boolean }. Both answers, from the cache and from the API, are now typed ChatsListOutput.
  • chats get and chats search print the same item, so their item type comes from the declaration too; their files are unchanged.

Which docs it replaces

Two paragraphs in docs/cli-commands.md that described 3 of the 16 fields are replaced by a table of every field, generated between <!-- output: chats list --> markers by bun run docs:outputs:

Field Type Description
items[].memberCount integer or null Members of a group or channel; null when unknown
items[].lastMessageAt integer or null Time of the newest message, Unix milliseconds
source cache or api cache, or api with --fresh or when the cache has no matching chat

A unit test fails when the table and the declaration differ. Changing one description without running the script gives:

- | `hasMore` | boolean | More chats follow this page |
+ | `hasMore` | boolean | More chats after this page |
(fail) docs:outputs > docs/cli-commands.md matches the output declarations (run `bun run docs:outputs`)

How the e2e check catches drift

src/__e2e__/chats-list-output.e2e.test.ts runs tg chats list on made-up chats (every type, nullable fields both set and null, with and without sync state) and parses the real output strictly against the declaration. Renaming unreadCount to unreadMessages in the command, without touching the declaration, fails it:

- []
+ [
+   "items.0.unreadCount: Invalid input: expected number, received undefined",
+   "items.0: Unrecognized key: \"unreadMessages\"",
    …
+ ]
(fail) E2E: tg chats list output > prints exactly the declared fields and types

tsc rejects the same rename, because the type is inferred from the declaration. The e2e check is what catches drift the compiler can't see, such as a value that arrives from untyped SQL or JSON.

What the next command would cost

One-off, done here: the devDependency, scripts/docs-outputs.ts (71 lines), the docs test and the docs:outputs script. Per command: a declaration of 40 to 80 lines, one line in the script's map, a marker pair in the docs, swapping the hand-written types for import type, and an e2e test of about 30 lines plus fixtures. Shared pieces (the chat item, the paging fields) reuse with .extend.

Decisions you may want to change:

  • zod at all. A dependency-free declaration is possible, but it would mean writing the type inference, the strict parser and the schema export by hand, which is the machinery zod already is. If you'd rather not have it even as a devDependency, say so and I'll show that version.
  • Where the file lives: next to its command as list-output.ts, not in a central src/outputs/.
  • Description wording is mine, reworded from the old prose.
  • No CHANGELOG entry, since nothing a user sees changes.

Not covered: the e2e test runs the cache path only. The --fresh path builds the same items with the same function and its answer is typed ChatsListOutput, so tsc checks it, but the fake client doesn't implement iterDialogs.

This touches src/commands/chats/list.ts, as #176 did; main is merged in, see the update below.

Update: main merged in (3a2fb89)

How I checked it

  • bun run check: clean. bun run test: 2302 pass, 0 fail. bun run test:e2e: 433 pass, 0 fail.

  • Output of tg chats list (default, --limit 2 --offset 1, --type community, --unread, --format pretty) on the same made-up cache: byte-identical before and after (cmp, same 5592 bytes).

  • bun run build, then searched dist/tg and the bundle's metafile for zod: none. Bundling the docs script the same way finds it, so the search would have.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard (docs/cli-commands.md, generated)

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

🤖 Generated with Claude Code

A trial of one output declaration on one command. The data that
tg chats list prints is a zod schema with a description per field; its
types are inferred from it, the field table in docs/cli-commands.md is
generated from it, and an e2e test parses the real output strictly
against it. zod is a devDependency: the command imports only the type,
so nothing new ships. The output is unchanged.

Refs neoromantic#160

Co-Authored-By: Claude Fable 5.2 <[email protected]>
No textual conflict. nextChatsPage (from neoromantic#185) takes the declared ChatsListOutput: PaginatedResult is no longer imported here.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
neoromantic added a commit that referenced this pull request Nov 18, 2026
refactor: declare tg chats list output once (#179)
ertembiyik pushed a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#179 (neoromantic#188). No conflict.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#179 (neoromantic#188). No conflict.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#179 (neoromantic#188). CHANGELOG: both Changed entries kept.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#179 (neoromantic#188). CHANGELOG: main's Changed section, then this PR's Removed.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#179 (neoromantic#188). No conflict.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
@neoromantic
neoromantic merged commit 36d5224 into neoromantic:main Nov 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants