cc-haha/adapters/dingtalk/__tests__/stream-state.test.ts
程序员阿江(Relakkes) 54c975bc09 fix: preserve DingTalk permission reply order
DingTalk keeps AI card placement tied to the original card delivery time, so a card created before a permission request can later be updated with the final answer above the permission card. Finish and drop the active stream when permission interrupts a turn so authorized output creates a new card below the prompt.

Constraint: DingTalk does not reorder an existing delivered AI card when its streaming content is updated
Rejected: Reuse the pre-permission AI card after approval | final answers can remain visually above the permission request
Confidence: high
Scope-risk: narrow
Directive: Keep permission_request as a lifecycle boundary for DingTalk AI card streams
Tested: cd adapters && bun test dingtalk/__tests__/stream-state.test.ts dingtalk/__tests__/ai-card.test.ts
Tested: cd adapters && bunx tsc --noEmit
Tested: bun run check:adapters
Tested: git diff --check
Not-tested: Live DingTalk account end-to-end permission approval
2026-05-05 21:49:39 +08:00

54 lines
1.9 KiB
TypeScript

import { describe, expect, it } from 'bun:test'
import { MessageBuffer } from '../../common/message-buffer.js'
import { finishAndResetDingTalkStreamingState, resetDingTalkStreamingState } from '../stream-state.js'
describe('DingTalk streaming state', () => {
it('drops the active AI card stream when permission interrupts output ordering', async () => {
let resetCalled = false
const buffer = new MessageBuffer(async () => {}, 100, 1000)
const originalReset = buffer.reset.bind(buffer)
buffer.reset = () => {
resetCalled = true
originalReset()
}
const state = {
aiCardBuffers: new Map([['chat-1', buffer]]),
streamingCards: new Map([['chat-1', Promise.resolve(null)]]),
streamingCardText: new Map([['chat-1', 'pre-permission text']]),
}
resetDingTalkStreamingState(state, 'chat-1')
expect(resetCalled).toBe(true)
expect(state.aiCardBuffers.has('chat-1')).toBe(false)
expect(state.streamingCards.has('chat-1')).toBe(false)
expect(state.streamingCardText.has('chat-1')).toBe(false)
})
it('completes the existing stream before dropping it for a permission request', async () => {
const flushed: Array<{ text: string; complete: boolean }> = []
const buffer = new MessageBuffer(
async (text, complete) => {
flushed.push({ text, complete })
},
100,
1000,
)
buffer.append('pre-permission text')
const state = {
aiCardBuffers: new Map([['chat-1', buffer]]),
streamingCards: new Map([['chat-1', Promise.resolve(null)]]),
streamingCardText: new Map([['chat-1', 'already streamed']]),
}
await finishAndResetDingTalkStreamingState(state, 'chat-1')
expect(flushed).toEqual([{ text: 'pre-permission text', complete: true }])
expect(state.aiCardBuffers.has('chat-1')).toBe(false)
expect(state.streamingCards.has('chat-1')).toBe(false)
expect(state.streamingCardText.has('chat-1')).toBe(false)
})
})