import { afterEach, beforeEach, describe, expect, it, spyOn } from 'bun:test' import { mkdtempSync, rmSync, unlinkSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { TeamsClient } from './client' import { TeamsCredentialManager } from './credential-manager' import { TeamsAuthCapabilityError, TeamsError } from './types' const TEMP_FILES_TO_CLEANUP = ['/tmp/test-teams-upload.txt'] const TEMP_DIRS_TO_CLEANUP: string[] = [] const SEARCH_TENANT_ID = '11111111-1111-1111-1111-111111111111' const SEARCH_USER_ID = '22222222-2222-2222-2222-222222222222' const GRAPH_AUDIENCE = 'https://graph.microsoft.com' describe('TeamsClient', () => { const originalFetch = globalThis.fetch let fetchCalls: Array<{ url: string; options?: RequestInit }> = [] let fetchResponses: Response[] = [] let fetchIndex = 0 beforeEach(() => { fetchCalls = [] fetchResponses = [] fetchIndex = 0 ;(globalThis as any).fetch = async (url: string | URL | Request, options?: RequestInit): Promise => { fetchCalls.push({ url: url.toString(), options }) const response = fetchResponses[fetchIndex] fetchIndex++ if (!response) { throw new Error('No mock response configured') } return response } }) afterEach(() => { globalThis.fetch = originalFetch for (const file of TEMP_FILES_TO_CLEANUP) { try { unlinkSync(file) } catch { // File may not exist, ignore } } for (const dir of TEMP_DIRS_TO_CLEANUP.splice(0)) { rmSync(dir, { recursive: true, force: true }) } }) const setupCredentialManager = async (): Promise => { const dir = join(import.meta.dir, `.test-teams-client-${Date.now()}-${Math.random().toString(36).slice(2)}`) TEMP_DIRS_TO_CLEANUP.push(dir) const manager = new TeamsCredentialManager(dir) await manager.setDeviceCodeAccount({ accountType: 'work', token: 'skype-token', tokenExpiresAt: '2100-01-01T00:00:00Z', aadRefreshToken: 'refresh-token', aadClientId: 'client-id', teams: {}, currentTeam: null, }) return manager } const createSearchJwt = (): string => { const header = Buffer.from(JSON.stringify({ alg: 'none', typ: 'JWT' })).toString('base64url') const payload = Buffer.from( JSON.stringify({ aud: 'https://substrate.office.com', tid: SEARCH_TENANT_ID, oid: SEARCH_USER_ID, exp: Math.floor(Date.now() / 1000) + 3600, }), ).toString('base64url') return `${header}.${payload}.signature` } const createGraphJwt = (): string => { const header = Buffer.from(JSON.stringify({ alg: 'none', typ: 'JWT' })).toString('base64url') const payload = Buffer.from( JSON.stringify({ aud: GRAPH_AUDIENCE, tid: SEARCH_TENANT_ID, oid: SEARCH_USER_ID, exp: Math.floor(Date.now() / 1000) + 3600, }), ).toString('base64url') return `${header}.${payload}.signature` } const headerValue = (init: RequestInit | undefined, name: string): string | undefined => { const headers = init?.headers if (headers instanceof Headers) return headers.get(name) ?? undefined if (Array.isArray(headers)) { const pair = headers.find(([key]) => key.toLowerCase() === name.toLowerCase()) return pair?.[1] } return headers?.[name] } const mockResponse = (body: unknown, status = 200, headers: Record = {}) => { const defaultHeaders: Record = { 'Content-Type': 'application/json', 'X-RateLimit-Remaining': '10', 'X-RateLimit-Reset': String(Date.now() / 1000 + 60), ...headers, } fetchResponses.push( new Response(body === null ? null : JSON.stringify(body), { status, headers: defaultHeaders, }), ) } const mockBinaryResponse = (body: string, status = 200) => { fetchResponses.push(new Response(body, { status })) } const graphClient = async () => { const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) ;(client as any).getTokenProvider().getGraphToken = async () => 'graph-token' return client } const graphMessage = (id = 'm1', content = 'Hello', replyToId?: string) => ({ id, replyToId, body: { contentType: 'html', content }, from: { user: { id: 'u1', displayName: 'Test User' } }, createdDateTime: '2024-01-01T00:00:00.000Z', }) describe('login', () => { it('requires token', async () => { await expect(new TeamsClient().login({ token: '', region: 'emea' })).rejects.toThrow(TeamsError) await expect(new TeamsClient().login({ token: '', region: 'emea' })).rejects.toThrow('Token is required') }) it('accepts valid token', async () => { const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) expect(client).toBeInstanceOf(TeamsClient) }) it('accepts token with expiry time', async () => { const expiresAt = new Date(Date.now() + 3600000).toISOString() const client = await new TeamsClient().login({ token: 'test-token', tokenExpiresAt: expiresAt, region: 'emea' }) expect(client).toBeInstanceOf(TeamsClient) }) }) describe('token expiry', () => { it('throws when token is expired', async () => { const expiredAt = new Date(Date.now() - 1000).toISOString() const client = await new TeamsClient().login({ token: 'expired-token', tokenExpiresAt: expiredAt, region: 'emea', }) await expect(client.testAuth()).rejects.toThrow(TeamsError) await expect(client.testAuth()).rejects.toThrow('Token has expired') }) it('works when token is not expired', async () => { const expiresAt = new Date(Date.now() + 3600000).toISOString() mockResponse({ userDetails: JSON.stringify({ name: 'Test User' }), locale: 'en-us', }) const client = await new TeamsClient().login({ token: 'valid-token', tokenExpiresAt: expiresAt, region: 'emea' }) const user = await client.testAuth() expect(user.id).toBe('ME') expect(user.displayName).toBe('Test User') }) }) describe('testAuth', () => { it.each([false, true])('authenticates an IC3 cookie token with region discovery=%s', async (discoverRegion) => { // given: the messaging token now stored in Unteams authtoken_asm cookies const token = 'eyJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJodHRwczovL2ljMy50ZWFtcy5vZmZpY2UuY29tIn0.signature' const client = await new TeamsClient().login({ token, accountType: 'work', region: discoverRegion ? undefined : 'emea', }) const fetchSpy = spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => { const headers = new Headers(init?.headers) if (headers.get('Authorization') !== `Bearer ${token}` || headers.has('X-Skypetoken')) { return new Response('{}', { status: 401 }) } if (new URL(input instanceof Request ? input.url : input.toString()).hostname.startsWith('amer.')) { return new Response('{}', { status: 403 }) } return Response.json({ userDetails: JSON.stringify({ name: 'Alice' }) }) }) try { // when const user = await client.testAuth() // then expect(user.displayName).toBe('Alice') expect(client.getRegion()).toBe('emea') for (const [, init] of fetchSpy.mock.calls) { expect(new Headers(init?.headers).get('Authorization')).toBe(`Bearer ${token}`) } } finally { fetchSpy.mockRestore() } }) it('returns current user info', async () => { mockResponse({ userDetails: JSON.stringify({ name: 'Test User' }), locale: 'en-us', }) const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) const user = await client.testAuth() expect(user.id).toBe('ME') expect(user.displayName).toBe('Test User') expect(fetchCalls.length).toBe(1) expect(fetchCalls[0].url).toBe('https://emea.ng.msg.teams.microsoft.com/v1/users/ME/properties') expect(fetchCalls[0].options?.headers).toMatchObject({ 'X-Skypetoken': 'test-token', }) }) it('throws TeamsError on API error', async () => { mockResponse({ message: 'Unauthorized', code: 'unauthorized' }, 401) const client = await new TeamsClient().login({ token: 'bad-token', region: 'emea' }) await expect(client.testAuth()).rejects.toThrow(TeamsError) }) }) describe('listTeams', () => { it('returns list of teams from conversations', async () => { mockResponse({ conversations: [ { id: '19:abc@thread.tacv2', threadProperties: { groupId: '111', spaceThreadTopic: 'Team One', productThreadType: 'TeamsChannel', threadType: 'space', }, }, { id: '19:def@thread.tacv2', threadProperties: { groupId: '222', spaceThreadTopic: 'Team Two', productThreadType: 'TeamsPrivateChannel', threadType: 'space', }, }, { id: '19:chat@thread.v2', threadProperties: { threadType: 'chat', }, }, ], }) const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) const teams = await client.listTeams() expect(teams).toHaveLength(2) expect(teams[0].id).toBe('111') expect(teams[0].name).toBe('Team One') expect(teams[1].id).toBe('222') expect(teams[1].name).toBe('Team Two') expect(fetchCalls[0].url).toBe('https://emea.ng.msg.teams.microsoft.com/v1/users/ME/conversations') }) }) describe('listChats', () => { it('classifies chats and excludes teams', async () => { mockResponse({ conversations: [ { id: '19:team@thread.tacv2', threadProperties: { groupId: '111', spaceThreadTopic: 'Team One', threadType: 'space' }, }, { id: '48:notes', threadProperties: { threadType: 'streamofnotes', productThreadType: 'StreamOfNotes' }, lastMessage: { content: 'Hi', composetime: '2024-01-03T00:00:00.000Z' }, }, { id: '19:1on1@unq.gbl.spaces', lastMessage: { content: '

Hi there

', composetime: '2024-01-01T00:00:00.000Z' }, }, { id: '19:group@thread.tacv2', threadProperties: { topic: 'Group Chat', threadType: 'chat' }, lastMessage: { content: 'Hello group', composetime: '2024-01-02T00:00:00.000Z' }, }, ], }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const chats = await client.listChats() expect(chats).toHaveLength(3) expect(chats[0]).toMatchObject({ id: '48:notes', type: 'self', last_message: 'Hi' }) expect(chats[1]).toMatchObject({ id: '19:1on1@unq.gbl.spaces', type: 'oneOnOne', last_message: 'Hi there' }) expect(chats[2]).toMatchObject({ id: '19:group@thread.tacv2', type: 'group', topic: 'Group Chat' }) expect(fetchCalls[0].url).toBe( 'https://msgapi.teams.live.com/v1/users/ME/conversations?view=msnp24Equivalent&pageSize=500', ) }) it('classifies untitled group chats as group, not oneOnOne', async () => { mockResponse({ conversations: [ { id: '19:aaa_bbb@unq.gbl.spaces', threadProperties: { threadType: 'chat', productThreadType: 'OneToOneChat', uniquerosterthread: 'true' }, }, { id: '19:40ac1fee18394793954c62576998dedc@thread.v2', threadProperties: { threadType: 'chat', productThreadType: 'Chat' }, }, { id: '19:untitled@thread.v2', threadProperties: { threadType: 'chat' } }, { id: '19:legacy_pair@unq.gbl.spaces' }, { id: '19:meeting@thread.v2', threadProperties: { threadType: 'meeting', topic: 'Standup' } }, ], }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const chats = await client.listChats() expect(chats.map((chat) => [chat.id, chat.type])).toEqual([ ['19:aaa_bbb@unq.gbl.spaces', 'oneOnOne'], ['19:40ac1fee18394793954c62576998dedc@thread.v2', 'group'], ['19:untitled@thread.v2', 'group'], ['19:legacy_pair@unq.gbl.spaces', 'oneOnOne'], ]) }) }) describe('getChatMessages', () => { it('returns user messages and hides system events unless requested', async () => { mockResponse({ messages: [ { id: 'm1', content: '

Hello

', from: 'host/users/ME/contacts/8:alice', imdisplayname: 'Alice', composetime: '2024-01-01T00:00:00.000Z', messagetype: 'RichText/Html', }, { id: 'm2', content: 'Bob joined', imdisplayname: 'System', composetime: '2024-01-01T00:01:00.000Z', messagetype: 'ThreadActivity/AddMember', }, ], }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const messages = await client.getChatMessages('19:1on1@unq.gbl.spaces', 30) expect(messages).toHaveLength(1) expect(messages[0].id).toBe('m1') expect(messages[0].content).toBe('Hello') expect(messages[0].author.displayName).toBe('Alice') expect(messages[0].channel_id).toBe('19:1on1@unq.gbl.spaces') expect(fetchCalls[0].url).toBe( 'https://msgapi.teams.live.com/v1/users/ME/conversations/19%3A1on1%40unq.gbl.spaces/messages?startTime=0&view=msnp24Equivalent&pageSize=30', ) mockResponse({ messages: [{ id: 'm2', content: 'Bob joined', messagetype: 'ThreadActivity/AddMember' }], }) const withSystem = await client.getChatMessages('19:1on1@unq.gbl.spaces', 30, { includeSystem: true }) expect(withSystem.map((message) => message.id)).toEqual(['m2']) }) it('exposes the raw html and downloadable files of rich messages', async () => { const imageUrl = 'https://us-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgo' const attachmentUrl = 'https://contoso-my.sharepoint.com/personal/alice/Documents/report.pdf' const html = `

See

` mockResponse({ messages: [ { id: 'm1', content: html, imdisplayname: 'Alice', composetime: '2024-01-01T00:00:00.000Z', messagetype: 'RichText/Html', properties: { files: JSON.stringify([{ objectUrl: attachmentUrl }]) }, }, ], }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const [message] = await client.getChatMessages('19:1on1@unq.gbl.spaces') expect(message.content).toBe('See') expect(message.html).toBe(html) expect(message.files).toEqual([imageUrl, attachmentUrl]) }) }) describe('getChatMessage', () => { it('fetches a single chat message by id', async () => { mockResponse({ id: '1700000000000', content: '

Hello & welcome

', imdisplayname: 'Alice', composetime: '2024-01-01T00:00:00.000Z', messagetype: 'RichText/Html', }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const message = await client.getChatMessage('19:group@thread.v2', '1700000000000') expect(fetchCalls[0].url).toBe( 'https://msgapi.teams.live.com/v1/users/ME/conversations/19%3Agroup%40thread.v2/messages/1700000000000', ) expect(message).toMatchObject({ id: '1700000000000', channel_id: '19:group@thread.v2', content: 'Hello & welcome', html: '

Hello & welcome

', }) }) }) describe('sendChatMessage', () => { it('sends an HTML-escaped message to a chat', async () => { mockResponse({ OriginalArrivalTime: 1704067200000 }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const message = await client.sendChatMessage('19:1on1@unq.gbl.spaces', 'a & c') expect(message.content).toBe('a & c') expect(fetchCalls[0].url).toBe( 'https://msgapi.teams.live.com/v1/users/ME/conversations/19%3A1on1%40unq.gbl.spaces/messages', ) expect(fetchCalls[0].options?.method).toBe('POST') expect(fetchCalls[0].options?.body).toBe( JSON.stringify({ content: 'a <b> & c', messagetype: 'RichText/Html', contenttype: 'text', }), ) }) it('converts markdown to HTML when format is markdown', async () => { mockResponse({ OriginalArrivalTime: 1704067200000 }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const message = await client.sendChatMessage('19:1on1@unq.gbl.spaces', '**bold** and `code`', 'markdown') // The return value echoes the caller's original markdown, not the converted HTML expect(message.content).toBe('**bold** and `code`') expect(fetchCalls[0].options?.body).toBe( JSON.stringify({ content: 'bold and code', messagetype: 'RichText/Html', contenttype: 'text', }), ) }) it('passes content through unchanged when format is html', async () => { mockResponse({ OriginalArrivalTime: 1704067200000 }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) await client.sendChatMessage('19:1on1@unq.gbl.spaces', 'raw', 'html') expect(fetchCalls[0].options?.body).toBe( JSON.stringify({ content: 'raw', messagetype: 'RichText/Html', contenttype: 'text', }), ) }) }) describe('editChatMessage', () => { it('PUTs an HTML-escaped edit to a chat message', async () => { mockResponse({ edittime: 1704067200000 }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const message = await client.editChatMessage('19:1on1@unq.gbl.spaces', 'msg1', 'a & c') expect(message.id).toBe('msg1') expect(message.content).toBe('a & c') expect(fetchCalls[0].url).toBe( 'https://msgapi.teams.live.com/v1/users/ME/conversations/19%3A1on1%40unq.gbl.spaces/messages/msg1', ) expect(fetchCalls[0].options?.method).toBe('PUT') expect(fetchCalls[0].options?.body).toBe( JSON.stringify({ content: 'a <b> & c', messagetype: 'RichText/Html', contenttype: 'text', skypeeditedid: 'msg1', }), ) }) it('converts markdown to HTML when format is markdown', async () => { mockResponse({ edittime: 1704067200000 }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) const message = await client.editChatMessage('19:1on1@unq.gbl.spaces', 'msg1', '**bold**', 'markdown') expect(message.content).toBe('**bold**') expect(fetchCalls[0].options?.body).toBe( JSON.stringify({ content: 'bold', messagetype: 'RichText/Html', contenttype: 'text', skypeeditedid: 'msg1', }), ) }) it('passes content through unchanged when format is html', async () => { mockResponse({ edittime: 1704067200000 }) const client = await new TeamsClient().login({ token: 'test-token', accountType: 'personal' }) await client.editChatMessage('19:1on1@unq.gbl.spaces', 'msg1', 'raw', 'html') expect(fetchCalls[0].options?.body).toBe( JSON.stringify({ content: 'raw', messagetype: 'RichText/Html', contenttype: 'text', skypeeditedid: 'msg1', }), ) }) }) describe('Graph teams and channels', () => { it('reads team, channel list and channel detail with Graph response shapes', async () => { mockResponse({ id: '111', displayName: 'Test Team', description: 'A test team' }) mockResponse({ value: [{ id: 'ch1', displayName: 'General', membershipType: 'standard' }] }) mockResponse({ id: 'ch1', displayName: 'General', membershipType: 'standard' }) const client = await graphClient() expect(await client.getTeam('111')).toEqual({ id: '111', name: 'Test Team', description: 'A test team' }) expect(await client.listChannels('111')).toEqual([ { id: 'ch1', team_id: '111', name: 'General', type: 'standard' }, ]) expect((await client.getChannel('111', 'ch1')).name).toBe('General') expect(fetchCalls.map((c) => c.url)).toEqual([ 'https://graph.microsoft.com/v1.0/teams/111', 'https://graph.microsoft.com/v1.0/teams/111/channels', 'https://graph.microsoft.com/v1.0/teams/111/channels/ch1', ]) expect(headerValue(fetchCalls[0].options, 'Authorization')).toBe('Bearer graph-token') }) }) describe('sendMessage', () => { it('posts root messages and replies to their distinct Graph endpoints', async () => { mockResponse(graphMessage()) mockResponse(graphMessage('reply1', 'Reply', 'root1')) const client = await graphClient() expect((await client.sendMessage('111', 'ch1', 'Hello')).id).toBe('m1') const reply = await client.sendMessage('111', 'ch1', 'Reply', 'root1') expect(reply.root_message_id).toBe('root1') expect(reply.parent_message_id).toBe('root1') expect(reply.is_thread_reply).toBe(true) expect(fetchCalls[0].url).toBe('https://graph.microsoft.com/v1.0/teams/111/channels/ch1/messages') expect(fetchCalls[1].url).toBe('https://graph.microsoft.com/v1.0/teams/111/channels/ch1/messages/root1/replies') expect(JSON.parse(String(fetchCalls[1].options?.body))).toEqual({ body: { contentType: 'html', content: 'Reply' }, }) }) it('escapes text, retains line breaks, renders markdown and sanitizes HTML', async () => { for (let i = 0; i < 3; i++) mockResponse(graphMessage()) const client = await graphClient() await client.sendMessage('111', 'ch1', 'a & c\nnext') await client.sendMessage('111', 'ch1', '**bold**', undefined, 'markdown') await client.sendMessage('111', 'ch1', 'ok', undefined, 'html') const bodies = fetchCalls.map((c) => JSON.parse(String(c.options?.body)).body.content) expect(bodies[0]).toBe('a <b> & c
next') expect(bodies[1]).toBe('bold') expect(bodies[2]).toBe('<script>alert(1)</script>ok') }) it('preserves Teams mention tags in explicit HTML', async () => { mockResponse(graphMessage()) const client = await graphClient() await client.sendMessage('111', 'ch1', 'Hey John', undefined, 'html') expect(JSON.parse(String(fetchCalls[0].options?.body)).body.content).toBe('Hey John') }) }) describe('CSA fallback for extract-only accounts', () => { const extractOnlyClient = async () => { const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) ;(client as any).getTokenProvider().getGraphToken = async () => { throw new TeamsAuthCapabilityError() } return client } it('preserves CSA headers for JSON and multipart requests with an IC3 token', async () => { // given: regional messaging uses a Bearer token, while CSA keeps its existing contract const dir = mkdtempSync(join(tmpdir(), 'teams-ic3-csa-')) TEMP_DIRS_TO_CLEANUP.push(dir) const filePath = join(dir, 'fixture.txt') await Bun.write(filePath, 'test') const token = 'eyJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJodHRwczovL2ljMy50ZWFtcy5vZmZpY2UuY29tIn0.signature' const client = await new TeamsClient(new TeamsCredentialManager(dir)).login({ token, region: 'emea' }) mockResponse([{ id: 'ch1', team_id: '111', name: 'General' }]) mockResponse({ id: 'file1', name: 'fixture.txt', size: 4 }) // when const channels = await client.listChannels('111') const file = await client.uploadFile('111', 'ch1', filePath) // then expect(channels[0].name).toBe('General') expect(file.id).toBe('file1') expect(fetchCalls).toHaveLength(2) for (const call of fetchCalls) { const headers = new Headers(call.options?.headers) expect(headers.get('X-Skypetoken')).toBe(token) expect(headers.has('Authorization')).toBe(false) } expect(fetchCalls[1].options?.body).toBeInstanceOf(FormData) }) it('reads channels and channel messages through CSA when no Graph token is available', async () => { mockResponse([{ id: 'ch1', team_id: '111', name: 'General' }]) mockResponse([ { id: 'msg1', channel_id: 'ch1', author: { id: '123', displayName: 'User 1' }, content: '

Release today

', timestamp: '2024-01-01T00:00:00.000Z', }, ]) const client = await extractOnlyClient() const channels = await client.listChannels('111') const [message] = await client.getMessages('111', 'ch1', 50) expect(channels[0].name).toBe('General') expect(message.content).toBe('Release today') expect(message.html).toBe('

Release today

') expect(fetchCalls.map((call) => call.url)).toEqual([ 'https://teams.microsoft.com/api/csa/api/v1/teams/111/channels', 'https://teams.microsoft.com/api/csa/emea/api/v2/teams/111/channels/ch1/messages?limit=50', ]) }) it('uses CSA for a bare-token client when no credentials are saved', async () => { // given: a real token provider over an empty config directory const dir = join(import.meta.dir, `.test-teams-empty-${Date.now()}-${Math.random().toString(36).slice(2)}`) TEMP_DIRS_TO_CLEANUP.push(dir) mockResponse([{ id: 'ch1', team_id: '111', name: 'General' }]) const client = await new TeamsClient(new TeamsCredentialManager(dir)).login({ token: 'skype-token', region: 'emea', }) // when const channels = await client.listChannels('111') // then expect(channels[0].name).toBe('General') expect(fetchCalls.map((call) => call.url)).toEqual([ 'https://teams.microsoft.com/api/csa/api/v1/teams/111/channels', ]) }) it('never acts as a different saved device-code identity than the supplied token', async () => { // given: a saved device-code account for another user const manager = await setupCredentialManager() mockResponse({ id: 'm1', content: 'Hi' }) const client = await new TeamsClient(manager).login({ token: 'other-user-skype-token', accountType: 'work', region: 'emea', }) // when await client.sendMessage('111', 'ch1', 'Hi') // then: the write goes through CSA with the supplied token, never Graph or token refresh expect(fetchCalls.map((call) => call.url)).toEqual([ 'https://teams.microsoft.com/api/csa/emea/api/v2/teams/111/channels/ch1/messages', ]) expect((fetchCalls[0].options?.headers as Record)['X-Skypetoken']).toBe('other-user-skype-token') }) const graphBearer = (oid: string): string => { const header = Buffer.from(JSON.stringify({ alg: 'none', typ: 'JWT' })).toString('base64url') const payload = Buffer.from( JSON.stringify({ aud: GRAPH_AUDIENCE, tid: SEARCH_TENANT_ID, oid, exp: Math.floor(Date.now() / 1000) + 3600 }), ).toString('base64url') return `${header}.${payload}.signature` } const saveWorkAccount = (manager: TeamsCredentialManager, token: string, aadRefreshToken?: string) => aadRefreshToken ? manager.setDeviceCodeAccount({ accountType: 'work', token, tokenExpiresAt: '2100-01-01T00:00:00Z', aadRefreshToken, aadClientId: 'client-id', teams: {}, currentTeam: null, }) : manager.setToken(token, 'work', '2100-01-01T00:00:00Z') it('keeps an extract-only client on CSA after the saved account is replaced by another user', async () => { // given: an extract-only saved account A that this client logged in as const manager = await setupCredentialManager() await saveWorkAccount(manager, 'user-a-skype-token') const client = await new TeamsClient(manager).login({ token: 'user-a-skype-token', accountType: 'work', region: 'emea', }) mockResponse([{ id: 'ch1', team_id: '111', name: 'General' }]) await client.listChannels('111') // when: the saved work account is replaced by device-code user B, without re-login await saveWorkAccount(manager, 'user-b-skype-token', 'user-b-refresh') mockResponse({ id: 'm1', content: 'Hi' }) await client.sendMessage('111', 'ch1', 'Hi') // then: the write stays on CSA as A, with no token refresh for B expect(fetchCalls.map((call) => call.url)).toEqual([ 'https://teams.microsoft.com/api/csa/api/v1/teams/111/channels', 'https://teams.microsoft.com/api/csa/emea/api/v2/teams/111/channels/ch1/messages', ]) expect((fetchCalls[1].options?.headers as Record)['X-Skypetoken']).toBe('user-a-skype-token') }) it('mints a fresh Graph bearer for the new user after re-login instead of reusing the cached one', async () => { // given: a client that warmed the Graph bearer cache as device-code user A const manager = await setupCredentialManager() await saveWorkAccount(manager, 'user-a-skype-token', 'user-a-refresh') const client = await new TeamsClient(manager).login({ token: 'user-a-skype-token', accountType: 'work', region: 'emea', }) mockResponse({ access_token: graphBearer('user-a'), expires_in: 3600 }) mockResponse({ value: [{ id: 'ch1', displayName: 'General' }] }) await client.listChannels('111') // when: the saved account becomes user B and the client logs in as B await saveWorkAccount(manager, 'user-b-skype-token', 'user-b-refresh') await client.login({ token: 'user-b-skype-token', accountType: 'work', region: 'emea' }) mockResponse({ access_token: graphBearer('user-b'), expires_in: 3600 }) mockResponse({ id: 'm1', body: { contentType: 'html', content: 'Hi' } }) await client.sendMessage('111', 'ch1', 'Hi') // then: B's refresh token is used and the mutation carries B's bearer, never A's expect(String(fetchCalls[2].options?.body)).toContain('refresh_token=user-b-refresh') expect(fetchCalls[3].url).toBe('https://graph.microsoft.com/v1.0/teams/111/channels/ch1/messages') expect((fetchCalls[3].options?.headers as Record).Authorization).toBe( `Bearer ${graphBearer('user-b')}`, ) }) it('does not fall back to CSA when Graph fails for another reason', async () => { mockResponse({ error: { code: 'Forbidden', message: 'Denied' } }, 403) const client = await graphClient() await expect(client.listChannels('111')).rejects.toThrow('Denied') expect(fetchCalls).toHaveLength(1) }) }) describe('Graph message history', () => { it('exposes html and attachment files for rich channel messages', async () => { const attachmentUrl = 'https://contoso.sharepoint.com/sites/team/Shared%20Documents/plan.docx' mockResponse({ value: [ { ...graphMessage(), body: { contentType: 'html', content: '

Plan attached

' }, attachments: [{ id: 'a1', contentType: 'reference', contentUrl: attachmentUrl, name: 'plan.docx' }], }, ], }) const client = await graphClient() const [message] = await client.getMessages('111', 'ch1') expect(message.content).toBe('Plan attached') expect(message.html).toBe('

Plan attached

') expect(message.files).toEqual([attachmentUrl]) }) it('reads root history, empty history, and replies with parent linkage', async () => { mockResponse({ value: [graphMessage()] }) mockResponse({ value: [] }) mockResponse({ value: [graphMessage('reply1', 'Reply', 'root1')] }) const client = await graphClient() const [root] = await client.getMessages('111', 'ch1', 30) expect(root.content).toBe('Hello') expect(root.is_thread_reply).toBeUndefined() expect(await client.getMessages('111', 'ch1')).toEqual([]) const [reply] = await client.getThreadReplies('111', 'ch1', 'root1', 20) expect(reply.root_message_id).toBe('root1') expect(fetchCalls[0].url).toBe('https://graph.microsoft.com/v1.0/teams/111/channels/ch1/messages?$top=30') expect(fetchCalls[2].url).toBe( 'https://graph.microsoft.com/v1.0/teams/111/channels/ch1/messages/root1/replies?$top=20', ) }) }) describe('searchMessages', () => { it('posts to Substrate search with bearer token, anchor mailbox, and parses nested results', async () => { const manager = await setupCredentialManager() const searchJwt = createSearchJwt() mockResponse({ access_token: searchJwt, refresh_token: 'rotated-refresh', expires_in: 3600 }) mockResponse({ EntitySets: [ { ResultSets: [ { Results: [ { Id: 'msg-1', Content: '

Deploy complete

', Author: { Id: 'author-1', DisplayName: 'Alice' }, ChannelId: 'channel-1', ThreadId: 'thread-1', TeamName: 'Team One', ChannelName: 'General', DateTimeSent: '2024-01-01T00:00:00.000Z', WebUrl: 'https://teams.microsoft.com/l/message/msg-1', }, ], }, ], }, ], }) const client = await new TeamsClient(manager).login({ token: 'skype-token', region: 'emea' }) const results = await client.searchMessages('deploy', { limit: 10, from: 5 }) expect(fetchCalls[1].url).toBe('https://substrate.office.com/searchservice/api/v2/query') expect(fetchCalls[1].options?.method).toBe('POST') expect(headerValue(fetchCalls[1].options, 'Authorization')).toBe(`Bearer ${searchJwt}`) expect(headerValue(fetchCalls[1].options, 'x-anchormailbox')).toBe(`Oid:${SEARCH_USER_ID}@${SEARCH_TENANT_ID}`) const payload = JSON.parse(String(fetchCalls[1].options?.body)) as { entityRequests: Array<{ entityType: string; contentSources: string[]; from: number; size: number }> } expect(payload.entityRequests[0]).toMatchObject({ entityType: 'Message', contentSources: ['Teams'], from: 5, size: 10, }) expect(results).toMatchObject([ { id: 'msg-1', content: 'Deploy complete', author: { id: 'author-1', displayName: 'Alice' }, channel_id: 'channel-1', thread_id: 'thread-1', team_name: 'Team One', channel_name: 'General', timestamp: '2024-01-01T00:00:00.000Z', permalink: 'https://teams.microsoft.com/l/message/msg-1', }, ]) }) it('returns an empty array when Substrate has no nested results', async () => { const manager = await setupCredentialManager() mockResponse({ access_token: createSearchJwt(), refresh_token: 'rotated-refresh', expires_in: 3600 }) mockResponse({ EntitySets: [] }) const client = await new TeamsClient(manager).login({ token: 'skype-token', region: 'emea' }) const results = await client.searchMessages('zzimprobablequery_xyz') expect(results).toMatchObject([]) }) it('uses the logged-in account refresh token when current account differs', async () => { const manager = await setupCredentialManager() await manager.setDeviceCodeAccount({ accountType: 'personal', token: 'personal-skype-token', tokenExpiresAt: '2100-01-01T00:00:00Z', aadRefreshToken: 'personal-refresh-token', aadClientId: 'personal-client-id', teams: {}, currentTeam: null, }) const searchJwt = createSearchJwt() mockResponse({ access_token: searchJwt, refresh_token: 'work-rotated-refresh', expires_in: 3600 }) mockResponse({ EntitySets: [] }) const client = await new TeamsClient(manager).login({ token: 'skype-token', accountType: 'work', region: 'emea' }) await client.searchMessages('deploy') const tokenRequestBody = new URLSearchParams(String(fetchCalls[0].options?.body)) expect(tokenRequestBody.get('refresh_token')).toBe('refresh-token') expect(tokenRequestBody.get('client_id')).toBe('client-id') }) it('rejects invalid pagination options', async () => { const manager = await setupCredentialManager() const client = await new TeamsClient(manager).login({ token: 'skype-token', region: 'emea' }) await expect(client.searchMessages('deploy', { limit: Number.NaN })).rejects.toThrow('positive integer') await expect(client.searchMessages('deploy', { limit: 0 })).rejects.toThrow('positive integer') await expect(client.searchMessages('deploy', { limit: -1 })).rejects.toThrow('positive integer') await expect(client.searchMessages('deploy', { from: -1 })).rejects.toThrow('non-negative integer') await expect(client.searchMessages('deploy', { from: 1.5 })).rejects.toThrow('non-negative integer') expect(fetchCalls).toHaveLength(0) }) }) describe('Graph message mutations and directory', () => { it('resolves a message, deletes through Chat Service and uses Graph reactions', async () => { mockResponse(graphMessage()) mockResponse(graphMessage()) mockResponse(null, 204) mockResponse(null, 204) mockResponse(null, 204) const client = await graphClient() expect((await client.getMessage('111', 'ch1', 'm1')).content).toBe('Hello') await client.deleteMessage('111', 'ch1', 'm1') await client.addReaction('111', 'ch1', 'm1', 'like') await client.removeReaction('111', 'ch1', 'm1', 'like') expect(fetchCalls.map((c) => [c.url.split('/').pop(), c.options?.method])).toEqual([ ['m1', 'GET'], ['m1', 'GET'], ['m1', 'DELETE'], ['setReaction', 'POST'], ['unsetReaction', 'POST'], ]) expect(fetchCalls[2].url).toBe( 'https://emea.ng.msg.teams.microsoft.com/v1/users/ME/conversations/ch1/messages/m1', ) expect(headerValue(fetchCalls[2].options, 'X-Skypetoken')).toBe('skype-token') expect(JSON.parse(String(fetchCalls[3].options?.body))).toEqual({ reactionType: '👍' }) }) it('maps membership userId and directory mail', async () => { mockResponse({ value: [{ id: 'member-id', userId: 'u1', displayName: 'Test User', email: 'test@example.test' }] }) mockResponse({ id: 'u1', displayName: 'Test User', mail: 'test@example.test' }) const client = await graphClient() expect((await client.listUsers('111'))[0].id).toBe('u1') expect((await client.getUser('u1')).email).toBe('test@example.test') expect(fetchCalls[0].url).toBe('https://graph.microsoft.com/v1.0/teams/111/members') expect(fetchCalls[1].url).toBe('https://graph.microsoft.com/v1.0/users/u1') }) }) describe('Graph channel drive', () => { const folder = { id: 'folder1', parentReference: { driveId: 'drive1' } } it('uploads bytes using PUT to the channel folder', async () => { await Bun.write('/tmp/test-teams-upload.txt', 'test content') mockResponse(folder) mockResponse({ id: 'file1', name: 'test-teams-upload.txt', size: 12, webUrl: 'https://example.sharepoint.com/file1', }) const client = await graphClient() expect((await client.uploadFile('111', 'ch1', '/tmp/test-teams-upload.txt')).drive_id).toBe('drive1') expect(fetchCalls[1].url).toBe( 'https://graph.microsoft.com/v1.0/drives/drive1/items/folder1:/test-teams-upload.txt:/content', ) expect(fetchCalls[1].options?.method).toBe('PUT') expect(new TextDecoder().decode(fetchCalls[1].options?.body as Uint8Array)).toBe('test content') }) it('lists files and folders without treating a folder as downloadable', async () => { mockResponse(folder) mockResponse({ value: [ { id: 'file1', name: 'doc.pdf', size: 1024, webUrl: 'https://example.sharepoint.com/doc.pdf' }, { id: 'subfolder', name: 'Subfolder', folder: { childCount: 1 } }, ], }) const client = await graphClient() const files = await client.listFiles('111', 'ch1') expect(files).toHaveLength(2) expect(files[1].is_folder).toBe(true) client.listFiles = async () => files await expect(client.downloadFile('111', 'ch1', 'subfolder')).rejects.toThrow('folder') expect(fetchCalls).toHaveLength(2) }) }) describe('downloadFile', () => { it('downloads SharePoint files through Graph shares with base64url share id', async () => { const manager = await setupCredentialManager() const shareUrl = 'https://contoso.sharepoint.com/sites/team/Shared%20Documents/report.docx' const listedFiles = [ { id: 'file1', name: 'report.docx', size: 11, url: shareUrl, contentType: 'application/vnd.ms-word' }, ] mockResponse({ access_token: createGraphJwt(), refresh_token: 'rotated-refresh', expires_in: 3600 }) mockBinaryResponse('graph-bytes') const client = await new TeamsClient(manager).login({ token: 'skype-token', region: 'emea' }) client.listFiles = async () => listedFiles const result = await client.downloadFile('111', 'ch1', 'file1') const shareId = `u!${Buffer.from(shareUrl).toString('base64url').replace(/=+$/, '')}` expect(fetchCalls[1].url).toBe(`https://graph.microsoft.com/v1.0/shares/${shareId}/driveItem/content`) expect(headerValue(fetchCalls[1].options, 'Authorization')).toBe(`Bearer ${createGraphJwt()}`) expect(Buffer.from(result.buffer).toString()).toBe('graph-bytes') expect(result.file.id).toBe('file1') }) it('downloads inline object URLs with the Skype token', async () => { const listedFiles = [ { id: 'file2', name: 'image.png', size: 10, url: 'https://teams.microsoft.com/files/image.png', object_url: 'https://us-api.asm.skype.com/v1/objects/0-weu-d1/image.png', contentType: 'image/png', }, ] mockBinaryResponse('image-bytes') const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) client.listFiles = async () => listedFiles const result = await client.downloadFile('111', 'ch1', 'file2') expect(fetchCalls[0].url).toBe('https://us-api.asm.skype.com/v1/objects/0-weu-d1/image.png') expect(headerValue(fetchCalls[0].options, 'Authorization')).toBe('Bearer skype-token') expect(headerValue(fetchCalls[0].options, 'X-Skypetoken')).toBe('skype-token') expect(Buffer.from(result.buffer).toString()).toBe('image-bytes') }) it('throws TeamsAuthCapabilityError for SharePoint files with cookie-only credentials', async () => { const dir = join( import.meta.dir, `.test-teams-client-cookie-${Date.now()}-${Math.random().toString(36).slice(2)}`, ) TEMP_DIRS_TO_CLEANUP.push(dir) const manager = new TeamsCredentialManager(dir) await manager.setToken('skype-token', 'work', '2100-01-01T00:00:00Z') const listedFiles = [ { id: 'file3', name: 'deck.pptx', size: 10, url: 'https://contoso.sharepoint.com/sites/team/Shared%20Documents/deck.pptx', }, ] const client = await new TeamsClient(manager).login({ token: 'skype-token', region: 'emea' }) client.listFiles = async () => listedFiles await expect(client.downloadFile('111', 'ch1', 'file3')).rejects.toThrow('Requires `agent-teams auth login`') expect(fetchCalls).toHaveLength(0) }) it('refuses to send the Skype token to an untrusted host', async () => { const listedFiles = [ { id: 'file4', name: 'evil.bin', size: 10, url: 'https://evil.example.com/steal', object_url: 'https://evil.example.com/steal', }, ] const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) client.listFiles = async () => listedFiles await expect(client.downloadFile('111', 'ch1', 'file4')).rejects.toThrow('untrusted host') // No credentialed download fetch reaches the untrusted host. expect(fetchCalls).toHaveLength(0) }) }) describe('downloadContent', () => { it('downloads inline images with the skype_token authorization scheme', async () => { const url = 'https://kr-prod.asyncgw.teams.microsoft.com/v1/objects/0-skr-d1-abc/views/imgo' fetchResponses.push(new Response('image-bytes', { status: 200, headers: { 'Content-Type': 'image/jpeg' } })) const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) const result = await client.downloadContent(url) expect(fetchCalls[0].url).toBe(url) expect(headerValue(fetchCalls[0].options, 'Authorization')).toBe('skype_token skype-token') expect(headerValue(fetchCalls[0].options, 'X-Skypetoken')).toBeUndefined() expect(result.data.toString()).toBe('image-bytes') expect(result.filename).toBe('0-skr-d1-abc.jpg') expect(result.contentType).toBe('image/jpeg') }) it('downloads SharePoint attachments through Graph shares', async () => { const manager = await setupCredentialManager() const url = 'https://contoso-my.sharepoint.com/personal/alice/Documents/Q3%20report.pdf' const graphJwt = createGraphJwt() mockResponse({ access_token: graphJwt, refresh_token: 'rotated-refresh', expires_in: 3600 }) fetchResponses.push(new Response('pdf-bytes', { status: 200, headers: { 'Content-Type': 'application/pdf' } })) const client = await new TeamsClient(manager).login({ token: 'skype-token', region: 'emea' }) const result = await client.downloadContent(url) const shareId = `u!${Buffer.from(url).toString('base64url').replace(/=+$/, '')}` expect(fetchCalls[1].url).toBe(`https://graph.microsoft.com/v1.0/shares/${shareId}/driveItem/content`) expect(headerValue(fetchCalls[1].options, 'Authorization')).toBe(`Bearer ${graphJwt}`) expect(result.data.toString()).toBe('pdf-bytes') expect(result.filename).toBe('Q3 report.pdf') }) it('prefers the Content-Disposition filename', async () => { fetchResponses.push( new Response('bytes', { status: 200, headers: { 'Content-Disposition': "attachment; filename*=UTF-8''%EB%B3%B4%EA%B3%A0%EC%84%9C.png" }, }), ) const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) const result = await client.downloadContent('https://us-api.asm.skype.com/v1/objects/0-weu-d1-x/views/imgo') expect(result.filename).toBe('보고서.png') }) it('refuses to send the Skype token to an untrusted host', async () => { const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) await expect(client.downloadContent('https://evil.example.com/steal')).rejects.toThrow('untrusted host') expect(fetchCalls).toHaveLength(0) }) it('refuses to send the Skype token over plain HTTP, even to a trusted host', async () => { const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) await expect( client.downloadContent('http://us-api.asm.skype.com/v1/objects/0-weu-d1-x/views/imgo'), ).rejects.toThrow('untrusted host') expect(fetchCalls).toHaveLength(0) }) it('fails on a non-2xx response', async () => { fetchResponses.push(new Response('nope', { status: 401 })) const client = await new TeamsClient().login({ token: 'skype-token', region: 'emea' }) await expect( client.downloadContent('https://us-api.asm.skype.com/v1/objects/0-weu-d1-x/views/imgo'), ).rejects.toThrow('HTTP 401') }) }) describe('rate limiting', () => { it('waits when bucket is exhausted before making request', async () => { mockResponse({ userDetails: JSON.stringify({ name: 'User 1' }), locale: 'en-us' }, 200, { 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': String(Date.now() / 1000 + 0.1), }) mockResponse({ userDetails: JSON.stringify({ name: 'User 2' }), locale: 'en-us' }, 200, { 'X-RateLimit-Remaining': '10', 'X-RateLimit-Reset': String(Date.now() / 1000 + 60), }) const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) await client.testAuth() const startTime = Date.now() await client.testAuth() const elapsed = Date.now() - startTime expect(elapsed).toBeGreaterThanOrEqual(50) expect(fetchCalls.length).toBe(2) }) it('retries on 429 with Retry-After header', async () => { mockResponse({ message: 'Rate limited' }, 429, { 'Retry-After': '0.1' }) mockResponse({ userDetails: JSON.stringify({ name: 'User' }), locale: 'en-us' }) const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) const user = await client.testAuth() expect(user.id).toBe('ME') expect(fetchCalls.length).toBe(2) }) it('throws after max retries exceeded', async () => { for (let i = 0; i <= 3; i++) { mockResponse({ message: 'Rate limited' }, 429, { 'Retry-After': '0.01' }) } const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) await expect(client.testAuth()).rejects.toThrow(TeamsError) expect(fetchCalls.length).toBeLessThanOrEqual(4) }) }) describe('retry logic', () => { it('retries on 500 server error', async () => { mockResponse({ message: 'Internal Server Error' }, 500) mockResponse({ userDetails: JSON.stringify({ name: 'User' }), locale: 'en-us' }) const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) const user = await client.testAuth() expect(user.id).toBe('ME') expect(fetchCalls.length).toBe(2) }) it('does not retry on 4xx client errors (except 429)', async () => { mockResponse({ message: 'Not Found' }, 404) const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) await expect(client.testAuth()).rejects.toThrow(TeamsError) expect(fetchCalls.length).toBe(1) }) it('exponential backoff increases delay', async () => { mockResponse({ message: 'Error' }, 500) mockResponse({ message: 'Error' }, 500) mockResponse({ userDetails: JSON.stringify({ name: 'User' }), locale: 'en-us' }) const client = await new TeamsClient().login({ token: 'test-token', region: 'emea' }) const startTime = Date.now() await client.testAuth() const elapsed = Date.now() - startTime expect(elapsed).toBeGreaterThanOrEqual(150) expect(fetchCalls.length).toBe(3) }) }) describe('route isolation', () => { it('encodes IDs and sends requests to the correct channel', async () => { mockResponse({ value: [] }) mockResponse({ value: [] }) const client = await graphClient() await client.getMessages('team/1', 'ch/1') await client.getMessages('team2', 'ch2') expect(fetchCalls[0].url).toContain('/teams/team%2F1/channels/ch%2F1/') expect(fetchCalls[1].url).toContain('/teams/team2/channels/ch2/') }) }) })