test(slack): add 40 tests for Slack approval integration — 0% to full coverage#565
test(slack): add 40 tests for Slack approval integration — 0% to full coverage#565agents-squads[bot] merged 1 commit intodevelopfrom
Conversation
Closes #555 Add 40 tests covering all exported functions in src/lib/slack.ts (previously 0% coverage): - slackApi: auth header, POST body, error handling - isSlackConfigured: env var check - getApprovalTier: YAML parsing for auto/notify/approve/confirm tiers - getSquadChannelId: channel lookup and error cases - postNotification: message posting with blocks - postApprovalRequest: approval buttons and notify tier - waitForApproval: reaction polling, context parsing, timeout - requestApprovalAndWait: tier routing and fallback behavior - createSquadChannel: creation, topic setting, name_taken handling - notifyTonightStart/Complete: session notification formatting Co-Authored-By: engineering/issue-solver <engineering-issue-solver@agents-squads.com> Agent: engineering/issue-solver Squad: engineering Model: claude-opus-4-6
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness and reliability of the Slack integration by introducing a thorough suite of unit tests. By covering all key functions and mocking external dependencies, it ensures that the Slack module behaves correctly under various conditions, from API interactions to approval workflows, without relying on live external services. This foundational testing effort will prevent regressions and facilitate future development with greater confidence. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive test suite for the src/lib/slack.ts module, significantly improving code coverage and reliability. The tests are well-structured and cover a wide range of scenarios for all exported functions. My review includes a couple of suggestions to enhance the robustness and efficiency of the tests, primarily by avoiding brittle assertions and using fake timers for timeout tests.
| const postCall = mockFetch.mock.calls[1]; | ||
| expect(postCall[0]).toBe('https://slack.com/api/chat.postMessage'); | ||
| const body = JSON.parse(postCall[1].body); | ||
| expect(body.channel).toBe('C123'); | ||
| expect(body.blocks).toHaveLength(2); // section + context | ||
| expect(body.blocks[0].text.text).toContain(':rocket:'); | ||
| expect(body.blocks[1].elements[0].text).toBe('v1.0.0'); |
There was a problem hiding this comment.
Accessing mock calls by index (e.g., mockFetch.mock.calls[1]) can make tests brittle. If the number or order of preceding API calls changes in the function under test (or its dependencies), this test will break even if the chat.postMessage call itself is correct. A more robust approach is to find the specific call you want to inspect by its endpoint.
This pattern of accessing calls by index is also present in other tests within this file (e.g., for postApprovalRequest and notifyTonightComplete). Consider applying a similar fix there for improved test robustness.
const postCall = mockFetch.mock.calls.find(
(call) => call[0] === 'https://slack.com/api/chat.postMessage'
);
expect(postCall).toBeDefined();
const body = JSON.parse(postCall![1].body);
expect(body.channel).toBe('C123');
expect(body.blocks).toHaveLength(2); // section + context
expect(body.blocks[0].text.text).toContain(':rocket:');
expect(body.blocks[1].elements[0].text).toBe('v1.0.0');| it('throws on timeout', async () => { | ||
| process.env.SLACK_BOT_TOKEN = 'xoxb-test-token'; | ||
| // Return pending state (actions still present, no reactions) | ||
| mockFetch.mockResolvedValue({ | ||
| json: async () => ({ | ||
| ok: true, | ||
| messages: [ | ||
| { | ||
| ts: '1234.5678', | ||
| text: 'Approval', | ||
| blocks: [{ type: 'actions' }], | ||
| reactions: [], | ||
| }, | ||
| ], | ||
| }), | ||
| }); | ||
|
|
||
| const { waitForApproval } = await import('../src/lib/slack'); | ||
| // Use very short timeout (100ms) to avoid long test | ||
| await expect(waitForApproval('C123', '1234.5678', 100)).rejects.toThrow( | ||
| 'Approval timeout' | ||
| ); | ||
| }); |
There was a problem hiding this comment.
This timeout test will take approximately 3 seconds to run due to the hardcoded pollInterval of 3000ms in the waitForApproval function, despite the test's timeout being set to 100ms. This makes the test unnecessarily slow and the comment // Use very short timeout (100ms) to avoid long test misleading.
You can use vi.useFakeTimers() to control the passage of time and make the test execute instantly while still correctly testing the timeout logic.
it('throws on timeout', async () => {
vi.useFakeTimers();
process.env.SLACK_BOT_TOKEN = 'xoxb-test-token';
// Return pending state (actions still present, no reactions)
mockFetch.mockResolvedValue({
json: async () => ({
ok: true,
messages: [
{
ts: '1234.5678',
text: 'Approval',
blocks: [{ type: 'actions' }],
reactions: [],
},
],
}),
});
const { waitForApproval } = await import('../src/lib/slack');
const promise = waitForApproval('C123', '1234.5678', 100);
// Advance timers to trigger the timeout logic in the polling loop
await vi.advanceTimersByTimeAsync(3000);
await expect(promise).rejects.toThrow('Approval timeout');
vi.useRealTimers();
});
Summary
src/lib/slack.ts(previously 0% coverage)Changes
test/slack.test.ts— new test fileTest Coverage
slackApiisSlackConfiguredgetApprovalTiergetSquadChannelIdpostNotificationpostApprovalRequestwaitForApprovalrequestApprovalAndWaitcreateSquadChannelnotifyTonightStart/CompleteTesting
npx vitest run test/slack.test.ts— 40/40 passnpm run build— passesCloses #555
🤖 Generated with Agents Squads