diff --git a/apps/api/src/modules/mail/__tests__/wecom-provider.test.ts b/apps/api/src/modules/mail/__tests__/wecom-provider.test.ts index c1758b4..d3d94f1 100644 --- a/apps/api/src/modules/mail/__tests__/wecom-provider.test.ts +++ b/apps/api/src/modules/mail/__tests__/wecom-provider.test.ts @@ -60,6 +60,17 @@ describe('token', () => { createWecomProvider({ fetch: scripted({ '/cgi-bin/gettoken': () => ({ errcode: 40001, errmsg: 'invalid credential' }) }).fetch }).pull(account, wecomCursor(1), { limit: 5, now, backfillFrom: null, known }), ).rejects.toBeInstanceOf(WecomApiError); }); + + it('-1 连击到边界不再重试(首次 + 3 次重试恰好 4 次)', async () => { + const { fetch, calls } = scripted({ + '/cgi-bin/gettoken': () => ({ errcode: 0, access_token: 't', expires_in: 7200 }), + 'get_email_alias': () => ({ errcode: -1, errmsg: 'system busy' }), + }); + const report = await createWecomProvider({ fetch }).probe(account); + expect(report.receive.ok).toBe(false); + expect(report.receive.error).toContain('-1'); + expect(calls.filter((c) => c.url.includes('get_email_alias'))).toHaveLength(4); + }); }); describe('probe / anchor', () => { @@ -100,6 +111,21 @@ describe('pull', () => { expect(result.more).toBe(false); }); + it('单页翻完但新信多过 limit —— 游标不许推进(has_more:0 也可能被 limit 截断)', async () => { + const { fetch } = scripted({ + '/cgi-bin/gettoken': () => ({ errcode: 0, access_token: 't', expires_in: 7200 }), + 'get_mail_list': () => ({ errcode: 0, next_cursor: '', has_more: 0, mail_list: [{ mail_id: 'm1' }, { mail_id: 'm2' }, { mail_id: 'm3' }] }), + 'read_mail': () => ({ errcode: 0, mail_data: EML }), + }); + const cursor = wecomCursor(1); + const result = await createWecomProvider({ fetch }).pull(account, cursor, { limit: 2, now, backfillFrom: null, known }); + expect(result.messages.map((m) => m.providerRef)).toEqual(['m1', 'm2']); + expect(result.more).toBe(true); + // 页翻完了(has_more:0)不等于「这一窗的新信全吃完了」—— LIST_PAGE(100)与 opts.limit 无关, + // 最后一页仍可能被 limit 截断;截断时游标必须原样退回,否则 m3 就永久丢了(重叠窗只兜 10 分钟)。 + expect(result.cursor).toEqual(cursor); + }); + it('取满 limit 就停,more=true', async () => { const { fetch } = scripted({ '/cgi-bin/gettoken': () => ({ errcode: 0, access_token: 't', expires_in: 7200 }), diff --git a/apps/api/src/modules/mail/internal/adapter/wecom-provider.ts b/apps/api/src/modules/mail/internal/adapter/wecom-provider.ts index 1336c4f..abd6577 100644 --- a/apps/api/src/modules/mail/internal/adapter/wecom-provider.ts +++ b/apps/api/src/modules/mail/internal/adapter/wecom-provider.ts @@ -158,11 +158,13 @@ export function createWecomProvider(deps: { fetch?: FetchLike; now?: () => numbe messages.push({ providerRef: mailId, eml: emlBytesOf(data), receivedAt: opts.now }); } /* - * 游标**只在把这个窗口翻完时**才推到窗口上沿;被 limit 截断时原样退回旧游标 —— - * 否则没读到的那几封就被跳过去了(重叠窗只兜 10 分钟,兜不住一次积压)。 - * 退回不会卡住:已入库的下一轮被 `known` 滤掉,每轮照旧往前吃一批。 + * 游标推进与 `more` **必须由同一个判据决定**:只有「这一窗的新信全吃完了」才推到窗口上沿。 + * `exhausted` 只说明**页翻完了** —— 最后一页可能同时 `has_more:0` 且新信多过本轮预算 + * (`LIST_PAGE` 是 100,与 `opts.limit` 无关),那时 `wanted` 截掉的那些若跟着推进游标就**永久丢**了 + * (重叠窗只兜 10 分钟)。退回不会卡住:已入库的下一轮被 `known` 滤掉,每轮照旧往前吃一批。 */ - return { messages, cursor: exhausted ? wecomCursor(endTime) : cursor, more: !exhausted || fresh.length > wanted.length }; + const more = !exhausted || fresh.length > wanted.length; + return { messages, cursor: more ? cursor : wecomCursor(endTime), more }; }, async send(account, message) {