Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/bots/slack/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { contractTestBot } from '@profullstack/sh1pt-core/testing';
import { request, type Server } from 'node:http';
import { describe, expect, it } from 'vitest';
import bot, { slackSignature, type FetchLike } from './index.js';
import bot, { slackSignature, slackTimestamp, type FetchLike } from './index.js';
import type { BotCtx, BotEvent, BotHandler } from '@profullstack/sh1pt-core';

contractTestBot(bot, { sampleConfig: {}, sampleChannel: 'C0123456789' });
Expand Down Expand Up @@ -75,6 +75,10 @@ function serverPort(server: Server): number {
}

describe('Slack bot adapter', () => {
it('falls back for out-of-range event timestamps', () => {
expect(slackTimestamp('1e20')).toBe('1970-01-01T00:00:00.000Z');
});

it('posts proactive messages with Slack Web API JSON and bearer auth', async () => {
const { calls, fetcher } = captureFetch([{ ok: true, ts: '1700000000.123456' }]);

Expand Down
6 changes: 4 additions & 2 deletions packages/bots/slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,12 @@ function firstHeader(value: string | string[] | undefined): string | undefined {
return Array.isArray(value) ? value[0] : value;
}

function slackTimestamp(value: string | undefined): string {
export function slackTimestamp(value: string | undefined): string {
if (!value) return new Date().toISOString();
const timestamp = Number(value);
return Number.isFinite(timestamp) ? new Date(timestamp * 1000).toISOString() : new Date().toISOString();
if (!Number.isFinite(timestamp)) return new Date().toISOString();
const date = new Date(timestamp * 1000);
return Number.isNaN(date.getTime()) ? new Date(0).toISOString() : date.toISOString();
}

async function readBody(req: IncomingMessage): Promise<string> {
Expand Down
Loading