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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Bot commands:
- `/whoami` show your Telegram user/chat IDs and authorization state
- `/help` show command reference

Outgoing assistant messages are sent with Telegram `parse_mode=HTML` for formatting, with automatic plain-text fallback if HTML delivery fails.
Outgoing assistant and bridge status messages are sent with Telegram Rich Messages using markdown input, so headings, lists, tables, quotes, and code blocks render natively. Local file references are degraded to bold text labels, and the bridge falls back to plain `sendMessage` text if Rich Message delivery fails.

---

Expand Down
136 changes: 136 additions & 0 deletions src/server/telegramThreadBridge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { TelegramThreadBridge } from './telegramThreadBridge.js'

describe('TelegramThreadBridge', () => {
const originalTelegramBotToken = process.env.TELEGRAM_BOT_TOKEN
const originalTelegramAllowedUserIds = process.env.TELEGRAM_ALLOWED_USER_IDS

beforeEach(() => {
process.env.TELEGRAM_BOT_TOKEN = 'test-token'
process.env.TELEGRAM_ALLOWED_USER_IDS = '*'
})

afterEach(() => {
vi.restoreAllMocks()
restoreEnvValue('TELEGRAM_BOT_TOKEN', originalTelegramBotToken)
restoreEnvValue('TELEGRAM_ALLOWED_USER_IDS', originalTelegramAllowedUserIds)
})

it('resumes an existing thread before retrying turn/start', async () => {
const calls: Array<{ method: string; params: unknown }> = []
let startCalls = 0
const appServer = {
rpc: vi.fn(async (method: string, params: unknown) => {
calls.push({ method, params })
if (method === 'turn/start') {
startCalls += 1
if (startCalls === 1) {
throw new Error('thread not found: test-thread')
}
return { turn: { id: 'turn-2' } }
}
if (method === 'thread/resume') {
return { thread: { id: 'test-thread', turns: [] } }
}
throw new Error(`Unexpected rpc method ${method}`)
}),
onNotification: vi.fn(() => () => {}),
}

const bridge = new TelegramThreadBridge(appServer)
vi.spyOn(bridge as never as { sendTelegramMessage: (...args: unknown[]) => Promise<void> }, 'sendTelegramMessage')
.mockResolvedValue(undefined)
;(bridge as unknown as { bindChatToThread: (chatId: number, threadId: string) => void })
.bindChatToThread(42, 'test-thread')

await (bridge as unknown as {
handleIncomingUpdate: (update: unknown) => Promise<void>
}).handleIncomingUpdate({
message: {
text: 'hello',
from: { id: 7 },
chat: { id: 42 },
},
})

expect(calls).toEqual([
{
method: 'turn/start',
params: {
threadId: 'test-thread',
input: [{ type: 'text', text: 'hello' }],
},
},
{
method: 'thread/resume',
params: { threadId: 'test-thread' },
},
{
method: 'turn/start',
params: {
threadId: 'test-thread',
input: [{ type: 'text', text: 'hello' }],
},
},
])
})

it('resumes an existing thread before binding it from the /thread command', async () => {
const calls: Array<{ method: string; params: unknown }> = []
const appServer = {
rpc: vi.fn(async (method: string, params: unknown) => {
calls.push({ method, params })
if (method === 'thread/resume') {
return { thread: { id: 'test-thread', turns: [] } }
}
if (method === 'thread/read') {
return {
thread: {
id: 'test-thread',
turns: [],
},
}
}
throw new Error(`Unexpected rpc method ${method}`)
}),
onNotification: vi.fn(() => () => {}),
}

const bridge = new TelegramThreadBridge(appServer)
const sendTelegramMessage = vi.spyOn(
bridge as never as { sendTelegramMessage: (...args: unknown[]) => Promise<void> },
'sendTelegramMessage',
).mockResolvedValue(undefined)

await (bridge as unknown as {
handleIncomingUpdate: (update: unknown) => Promise<void>
}).handleIncomingUpdate({
message: {
text: '/thread test-thread',
from: { id: 7 },
chat: { id: 42 },
},
})

expect(calls).toEqual([
{
method: 'thread/resume',
params: { threadId: 'test-thread' },
},
{
method: 'thread/read',
params: { threadId: 'test-thread', includeTurns: true },
},
])
expect(sendTelegramMessage).toHaveBeenCalledWith(42, expect.stringContaining('Connected to existing thread'))
})
})

function restoreEnvValue(name: string, value: string | undefined): void {
if (typeof value === 'undefined') {
delete process.env[name]
return
}
process.env[name] = value
}
Loading