|
1 | 1 | // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
3 | 3 | import { describe, it, expect } from 'vitest'; |
4 | | -import { LiteKernel } from '@objectstack/core'; |
| 4 | +import { LiteKernel, type PluginContext } from '@objectstack/core'; |
5 | 5 | import { AutomationServicePlugin, type AutomationEngine } from '@objectstack/service-automation'; |
6 | 6 | import { ConnectorSlackPlugin } from './connector-slack-plugin.js'; |
7 | 7 | import type { SlackClientService } from './slack-connector.js'; |
@@ -99,3 +99,88 @@ describe('ConnectorSlackPlugin — the `slack` client service', () => { |
99 | 99 | await kernel.shutdown(); |
100 | 100 | }); |
101 | 101 | }); |
| 102 | + |
| 103 | +describe('ConnectorSlackPlugin — composed without a static bot token', () => { |
| 104 | + it('registers only the client service: no flow connector, and the client still sends with the caller\'s token', async () => { |
| 105 | + const { impl, calls } = stubSlack(); |
| 106 | + const kernel = new LiteKernel(); |
| 107 | + kernel.use(new AutomationServicePlugin()); |
| 108 | + kernel.use(new ConnectorSlackPlugin({ fetchImpl: impl })); |
| 109 | + await kernel.bootstrap(); |
| 110 | + |
| 111 | + const engine = kernel.getService<AutomationEngine>('automation'); |
| 112 | + // The automation engine is up (positive control), yet no Slack connector was registered. |
| 113 | + expect(engine.getRegisteredNodeTypes()).toContain('connector_action'); |
| 114 | + expect(engine.getRegisteredConnectors()).not.toContain('slack'); |
| 115 | + |
| 116 | + const slack = kernel.getService<SlackClientService>('slack'); |
| 117 | + const out = await slack.client('xoxb-organization-token').call({ method: 'chat.postMessage', params: { channel: 'D1', text: 'hi' } }); |
| 118 | + expect(out.ok).toBe(true); |
| 119 | + expect(calls).toHaveLength(1); |
| 120 | + expect((calls[0].init.headers as Record<string, string>).Authorization).toBe('Bearer xoxb-organization-token'); |
| 121 | + |
| 122 | + await kernel.shutdown(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('composes with no options at all', async () => { |
| 126 | + const kernel = new LiteKernel(); |
| 127 | + kernel.use(new AutomationServicePlugin()); |
| 128 | + kernel.use(new ConnectorSlackPlugin()); |
| 129 | + await kernel.bootstrap(); |
| 130 | + |
| 131 | + expect(kernel.getService<AutomationEngine>('automation').getRegisteredConnectors()).not.toContain('slack'); |
| 132 | + expect(typeof kernel.getService<SlackClientService>('slack').client).toBe('function'); |
| 133 | + |
| 134 | + await kernel.shutdown(); |
| 135 | + }); |
| 136 | + |
| 137 | + it.each([ |
| 138 | + ['omitted', undefined], |
| 139 | + ['empty', ''], |
| 140 | + ])('says so once at boot and never touches the connector registry (token %s)', async (_label, token) => { |
| 141 | + const infos: string[] = []; |
| 142 | + const registered: string[] = []; |
| 143 | + const services = new Map<string, unknown>(); |
| 144 | + services.set('automation', { |
| 145 | + registerConnector: (def: { name: string }) => { registered.push(def.name); }, |
| 146 | + unregisterConnector: () => {}, |
| 147 | + }); |
| 148 | + const ctx = { |
| 149 | + registerService: (name: string, svc: unknown) => { services.set(name, svc); }, |
| 150 | + getService: (name: string) => services.get(name), |
| 151 | + logger: { info: (msg: string) => { infos.push(msg); }, warn: () => {}, error: () => {}, debug: () => {} }, |
| 152 | + } as unknown as PluginContext; |
| 153 | + |
| 154 | + const plugin = new ConnectorSlackPlugin(token === undefined ? {} : { token }); |
| 155 | + await plugin.init(ctx); |
| 156 | + await plugin.start(ctx); |
| 157 | + |
| 158 | + expect(registered).toEqual([]); |
| 159 | + expect(services.has('slack')).toBe(true); |
| 160 | + const tokenLines = infos.filter((m) => m.includes('no static bot token')); |
| 161 | + expect(tokenLines).toHaveLength(1); |
| 162 | + expect(infos).toHaveLength(1); |
| 163 | + |
| 164 | + // Teardown with nothing registered is a no-op, not a throw. |
| 165 | + await plugin.destroy(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('with a token, the same harness registers the connector (control for the skip)', async () => { |
| 169 | + const registered: string[] = []; |
| 170 | + const services = new Map<string, unknown>([[ |
| 171 | + 'automation', |
| 172 | + { registerConnector: (def: { name: string }) => { registered.push(def.name); }, unregisterConnector: () => {} }, |
| 173 | + ]]); |
| 174 | + const ctx = { |
| 175 | + registerService: (name: string, svc: unknown) => { services.set(name, svc); }, |
| 176 | + getService: (name: string) => services.get(name), |
| 177 | + logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }, |
| 178 | + } as unknown as PluginContext; |
| 179 | + |
| 180 | + const plugin = new ConnectorSlackPlugin({ token: 'xoxb-flow-token' }); |
| 181 | + await plugin.init(ctx); |
| 182 | + await plugin.start(ctx); |
| 183 | + |
| 184 | + expect(registered).toEqual(['slack']); |
| 185 | + }); |
| 186 | +}); |
0 commit comments