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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ mock.module("@superset/db/client", () => ({
},
}));
mock.module("@/env", () => ({
env: { NEXT_PUBLIC_WEB_URL: "https://app.superset.sh" },
env: {
NEXT_PUBLIC_WEB_URL: "https://app.superset.sh",
NEXT_PUBLIC_API_URL: "https://api.test",
QSTASH_TOKEN: "qstash-token",
},
}));
mock.module("@/lib/analytics", () => ({ posthog: { capture: () => {} } }));
mock.module("../../lib/find-slack-user-link", () => ({
Expand Down Expand Up @@ -82,25 +86,30 @@ const beginThread = mock(
async (
_args: unknown,
): Promise<
{ status: "running"; session: typeof session } | { status: "queued" }
| { status: "running"; session: typeof session }
| { status: "queued" }
| { status: "covered" }
> => ({ status: "running", session }),
);
const finishThread = mock(async (_args: unknown) => {});
const setQuiet = mock(async (_args: unknown) => {});
const followUpsEnabled = mock(async (_teamId: string) => true);
const readQueued = mock(
const takeQueued = mock(
async (
_id: string,
_handoff: string,
): Promise<{ ts: string; user: string; text: string }[]> => [],
);
const clearQueued = mock(async (_id: string, _ts: string) => {});
const completeHandoff = mock(async (_id: string, _handoff: string) => {});
const abandonHandoff = mock(async (_id: string, _handoff: string) => {});
mock.module("../utils/thread-sessions", () => ({
beginThreadRun: beginThread,
finishThreadRun: finishThread,
setThreadQuiet: setQuiet,
threadFollowUpsEnabled: followUpsEnabled,
readQueuedEvents: readQueued,
clearQueuedEventsThrough: clearQueued,
takeQueuedEvents: takeQueued,
completeHandBack: completeHandoff,
abandonHandBack: abandonHandoff,
parseThreadCommand: (text: string) => {
const t = text
.replace(/<@[A-Z0-9]+>/g, "")
Expand Down Expand Up @@ -180,15 +189,16 @@ beforeEach(() => {
followUpsEnabled.mockImplementation(async () => true);
release.mockClear();
publishJSON.mockClear();
readQueued.mockReset();
readQueued.mockImplementation(async () => []);
clearQueued.mockClear();
takeQueued.mockReset();
takeQueued.mockImplementation(async () => []);
completeHandoff.mockClear();
abandonHandoff.mockClear();
});

test("with the flag off, nothing is queued and nothing is handed back", async () => {
followUpsEnabled.mockImplementationOnce(async () => false);
await processAgentMessage(params);
expect(readQueued).not.toHaveBeenCalled();
expect(takeQueued).not.toHaveBeenCalled();
expect(publishJSON).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -255,18 +265,18 @@ test("a reply that arrives mid-turn is queued: no run, claim released, reaction
});

test("after a turn, queued replies are handed back by re-delivering the newest one", async () => {
readQueued.mockImplementationOnce(async () => [
takeQueued.mockImplementationOnce(async () => [
{ ts: "11.0", user: "U2", text: "first" },
{ ts: "12.0", user: "U3", text: "second" },
]);
await processAgentMessage(params);
expect(publishJSON).toHaveBeenCalledTimes(1);
expect(publishJSON.mock.calls[0]?.[0]).toMatchObject({
url: expect.stringContaining("/jobs/process-mention"),
deduplicationId: "queued:T1:12.0:10.0",
url: "https://api.test/api/integrations/slack/jobs/process-mention",
deduplicationId: "queued-T1-10-0",
body: {
teamId: "T1",
eventId: "queued:T1:12.0:10.0",
eventId: "queued-T1-10-0",
event: {
channel_type: "channel",
ts: "12.0",
Expand All @@ -278,7 +288,17 @@ test("after a turn, queued replies are handed back by re-delivering the newest o
},
});
expect(publishJSON.mock.calls[0]?.[0]).not.toHaveProperty("body.event.files");
expect(clearQueued).toHaveBeenCalledWith("thread-session", "12.0");
expect(takeQueued).toHaveBeenCalledWith("thread-session", "queued-T1-10-0");
// QStash rejects ":" in a deduplication id with a 400.
expect(
(publishJSON.mock.calls[0]?.[0] as { deduplicationId: string })
.deduplicationId,
).not.toMatch(/:/);
expect(completeHandoff).toHaveBeenCalledWith(
"thread-session",
"queued-T1-10-0",
);
expect(abandonHandoff).not.toHaveBeenCalled();
});

test("a reply queued mid-turn keeps its attachments through the hand-back", async () => {
Expand All @@ -291,7 +311,7 @@ test("a reply queued mid-turn keeps its attachments through the hand-back", asyn
expect(beginThread.mock.calls[0]?.[0]).toMatchObject({
event: { ts: "10.0", files: [file] },
});
readQueued.mockImplementationOnce(async () => [
takeQueued.mockImplementationOnce(async () => [
{ ts: "11.0", user: "U2", text: "see this", files: [file] },
{ ts: "12.0", user: "U3", text: "and this" },
]);
Expand All @@ -304,7 +324,7 @@ test("a reply queued mid-turn keeps its attachments through the hand-back", asyn
test("a handed-back reply claims a delivery of its own, so a second hand-back can still run it", async () => {
await processAgentMessage({
...params,
eventId: "queued:T1:10.0:9.0",
eventId: "queued-T1-9-0",
event: {
...params.event,
type: "message",
Expand All @@ -314,34 +334,79 @@ test("a handed-back reply claims a delivery of its own, so a second hand-back ca
});
expect(claim.mock.calls[0]?.[0]).toMatchObject({
messageTs: "10.0",
handoff: "queued:T1:10.0:9.0",
handoff: "queued-T1-9-0",
});
await processAgentMessage(params);
expect(claim.mock.calls[1]?.[0]).toMatchObject({ handoff: undefined });
});

test("a failed hand-back leaves the queue for the next turn", async () => {
readQueued.mockImplementationOnce(async () => [
takeQueued.mockImplementationOnce(async () => [
{ ts: "11.0", user: "U2", text: "first" },
]);
publishJSON.mockImplementationOnce(async () => {
throw new Error("qstash down");
});
await processAgentMessage(params);
expect(clearQueued).not.toHaveBeenCalled();
expect(abandonHandoff).toHaveBeenCalledWith(
"thread-session",
"queued-T1-10-0",
);
expect(completeHandoff).not.toHaveBeenCalled();
expect(postMessage.mock.calls.at(-1)?.[0].text).toBe("**Completed**");
});

test("a handed-back reply a finished turn already covered stands down and clears its eyes", async () => {
beginThread.mockImplementationOnce(async () => ({ status: "covered" }));
await processAgentMessage({
...params,
eventId: "queued-T1-9-0",
event: {
...params.event,
type: "message",
channel_type: "channel",
queued_ts: ["8.0"],
},
});
expect(beginThread.mock.calls[0]?.[0]).toMatchObject({ handBack: true });
expect(runAgent).not.toHaveBeenCalled();
expect(finish).toHaveBeenCalledWith("delivery", true);
expect(
removeReaction.mock.calls.map(
([a]) => (a as { timestamp: string }).timestamp,
),
).toEqual(["10.0", "8.0"]);
expect(
postMessage.mock.calls.every(([a]) => a.text !== "**Completed**"),
).toBe(true);
});

test("a handed-back reply does not re-ask the flag", async () => {
followUpsEnabled.mockImplementationOnce(async () => false);
await processAgentMessage({
...params,
eventId: "queued-T1-9-0",
event: {
...params.event,
type: "message",
channel_type: "channel",
queued_ts: [],
},
});
expect(followUpsEnabled).not.toHaveBeenCalled();
expect(runAgent).toHaveBeenCalledTimes(1);
});

test("a DM's queued replies go back through the assistant job as a DM", async () => {
readQueued.mockImplementationOnce(async () => [
takeQueued.mockImplementationOnce(async () => [
{ ts: "11.0", user: "U2", text: "more" },
]);
await processAgentMessage({
...params,
event: { ...params.event, type: "message", channel_type: "im" },
});
expect(publishJSON.mock.calls[0]?.[0]).toMatchObject({
url: expect.stringContaining("/jobs/process-assistant-message"),
url: "https://api.test/api/integrations/slack/jobs/process-assistant-message",
body: { event: { channel_type: "im", ts: "11.0", queued_ts: [] } },
});
});
Expand Down Expand Up @@ -378,14 +443,16 @@ test("a run opens the thread session, hands its memory to the agent, and records
],
}));
await processAgentMessage(params);
expect(beginThread).toHaveBeenCalledWith({
organizationId: "org",
teamId: "T1",
channelId: "C1",
threadTs: "1.0",
userId: "linked-user",
event: { ts: "10.0", user: "U1", text: "Help" },
});
expect(beginThread).toHaveBeenCalledWith(
expect.objectContaining({
organizationId: "org",
teamId: "T1",
channelId: "C1",
threadTs: "1.0",
userId: "linked-user",
event: { ts: "10.0", user: "U1", text: "Help" },
}),
);
expect(runAgent.mock.calls[0]?.[0]).toMatchObject({
threadMemory: "fix-login (feat/login)",
});
Expand All @@ -399,7 +466,7 @@ test("a run opens the thread session, hands its memory to the agent, and records
],
lastContextTs: "10.0",
});
expect(readQueued).toHaveBeenCalledWith("thread-session");
expect(takeQueued).toHaveBeenCalledWith("thread-session", "queued-T1-10-0");
expect(publishJSON).not.toHaveBeenCalled();
});

Expand Down
Loading
Loading