import { describe, expect, it } from 'bun:test' import { type SlackBotConfig, SlackBotConfigSchema, type SlackBotCredentials, SlackBotCredentialsSchema, SlackBotError, SlackMessageSchema, } from './types' describe('SlackBotError', () => { it('creates error with message and code', () => { const error = new SlackBotError('Token is invalid', 'invalid_auth') expect(error.message).toBe('Token is invalid') expect(error.code).toBe('invalid_auth') expect(error.name).toBe('SlackBotError') expect(error instanceof Error).toBe(true) }) }) describe('SlackBotCredentialsSchema', () => { it('validates correct bot token credentials', () => { const creds: SlackBotCredentials = { token: 'xoxb-123456789-abcdef', workspace_id: 'T12345678', workspace_name: 'test-workspace', bot_id: 'deploy', bot_name: 'Deploy Bot', } const result = SlackBotCredentialsSchema.safeParse(creds) expect(result.success).toBe(true) }) it('rejects user tokens (xoxp-)', () => { const creds = { token: 'xoxp-123456789-abcdef', workspace_id: 'T12345678', workspace_name: 'test-workspace', bot_id: 'deploy', bot_name: 'Deploy Bot', } const result = SlackBotCredentialsSchema.safeParse(creds) expect(result.success).toBe(false) }) it('rejects missing fields', () => { const creds = { token: 'xoxb-123456789-abcdef', } const result = SlackBotCredentialsSchema.safeParse(creds) expect(result.success).toBe(false) }) }) describe('SlackMessageSchema', () => { it('keeps attachments and blocks of an empty-text integration message', () => { // given const message = { ts: '1234567890.000002', text: '', type: 'message', attachments: [{ title: '[FIRING:1] HighLoad', text: 'load is 42' }], blocks: [{ type: 'section', text: { type: 'mrkdwn', text: 'details' } }], } // when const parsed = SlackMessageSchema.parse(message) // then expect(parsed.attachments).toEqual(message.attachments) expect(parsed.blocks).toEqual(message.blocks) }) }) describe('SlackBotConfigSchema', () => { it('validates correct config', () => { const config: SlackBotConfig = { current: { workspace_id: 'T12345678', bot_id: 'deploy' }, workspaces: { T12345678: { workspace_id: 'T12345678', workspace_name: 'test-workspace', bots: { deploy: { bot_id: 'deploy', bot_name: 'Deploy Bot', token: 'xoxb-123456789-abcdef', }, }, }, }, } const result = SlackBotConfigSchema.safeParse(config) expect(result.success).toBe(true) }) it('validates config with null current', () => { const config: SlackBotConfig = { current: null, workspaces: {}, } const result = SlackBotConfigSchema.safeParse(config) expect(result.success).toBe(true) }) })