import { describe, expect, it } from 'vitest' import { buildSessionHandoffManifest, isMailboxIssue, MAILBOX_MARKER, MAILBOX_TITLE, parseSessionHandoffAttachmentName, parseSessionHandoffManifest, sessionHandoffAttachmentName, slugSessionName, } from './sessionHandoff' const SID = '11111111-1111-4111-8111-111111111111' describe('isMailboxIssue', () => { it('title 命中', () => { expect(isMailboxIssue({ title: MAILBOX_TITLE })).toBe(true) }) it('label 命中', () => { expect(isMailboxIssue({ title: '其他', labels: [{ name: 'spx-session-mailbox' }] })).toBe(true) }) it('body marker 命中', () => { expect(isMailboxIssue({ title: 'x', body: `hello ${MAILBOX_MARKER}` })).toBe(true) }) it('普通工单不是', () => { expect(isMailboxIssue({ title: '修登录', body: 'foo', labels: [{ name: 'bug' }] })).toBe(false) }) }) describe('sessionHandoffAttachmentName', () => { it('编解码往返', () => { const name = sessionHandoffAttachmentName({ sid: SID, from: 'chw', to: 'cruldra', name: '测试服初始化', }) expect(name).toBe(`spx-handoff-session!${SID}!chw!cruldra!测试服初始化.tgz`) expect(parseSessionHandoffAttachmentName(name)).toEqual({ sid: SID, from: 'chw', to: 'cruldra', name: '测试服初始化', }) }) it('空名字也合法', () => { const name = sessionHandoffAttachmentName({ sid: SID, from: 'a', to: 'b', name: '' }) expect(parseSessionHandoffAttachmentName(name)).toEqual({ sid: SID, from: 'a', to: 'b', name: '', }) }) it('slug 去掉非法字符', () => { expect(slugSessionName('foo/bar baz')).toBe('foo_bar_baz') }) it('sid 非 UUID → 抛错', () => { expect(() => sessionHandoffAttachmentName({ sid: '../x', from: 'a', to: 'b', name: '' })).toThrow(/UUID/) }) it('login 含 ! → 抛错', () => { expect(() => sessionHandoffAttachmentName({ sid: SID, from: 'a!b', to: 'c', name: '' })).toThrow(/from/) }) it('工单移交包名解析不到', () => { expect(parseSessionHandoffAttachmentName('spx-handoff-issue-12.tgz')).toBeUndefined() }) }) describe('parseSessionHandoffManifest', () => { const good = { version: 1, kind: 'managed-session', from: 'chw', to: 'cruldra', createdAt: '2026-08-27T00:00:00Z', workspacePath: '/home/chw/proj', session: { id: SID, name: '测试服初始化', profilePath: '/x/offical.json' }, claude: [{ id: SID }], } it('合法清单原样返回', () => { expect(parseSessionHandoffManifest(JSON.stringify(good))).toEqual(good) }) it('kind 不对 → 抛错', () => { expect(() => parseSessionHandoffManifest(JSON.stringify({ ...good, kind: 'issue' }))).toThrow(/kind/) }) it('缺少 workspacePath → 抛错', () => { const { workspacePath: _, ...rest } = good expect(() => parseSessionHandoffManifest(JSON.stringify(rest))).toThrow(/workspacePath/) }) it('claude 空 → 抛错', () => { expect(() => parseSessionHandoffManifest(JSON.stringify({ ...good, claude: [] }))).toThrow(/claude/) }) it('build 再 parse 往返', () => { const built = buildSessionHandoffManifest({ from: 'chw', to: 'cruldra', workspacePath: '/home/chw/proj', session: { id: SID, name: 'n' }, createdAt: '2026-08-27T00:00:00Z', }) expect(parseSessionHandoffManifest(JSON.stringify(built))).toEqual(built) }) })