mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 02:30:43 +01:00
fix: expire incoming call approval confirmation
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
787d9ee763
commit
81f82320b2
3 files changed
+54
-4
No files matched your search
@@ -5,8 +5,8 @@ import {MemoryRouter} from "react-router-dom";
|
||||
import {QueryClient, QueryClientProvider} from "@tanstack/react-query";
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
|
||||
import {SpekoIncomingCalls} from "./SpekoIncomingCalls";
|
||||
const mocks = vi.hoisted(() => ({incoming: vi.fn(), list: vi.fn()}));
|
||||
vi.mock("@/api/voicePhone", () => ({voicePhoneApi: {incoming: mocks.incoming}}));
|
||||
const mocks = vi.hoisted(() => ({incoming: vi.fn(), list: vi.fn(), decide: vi.fn()}));
|
||||
vi.mock("@/api/voicePhone", () => ({voicePhoneApi: {incoming: mocks.incoming, decide: mocks.decide}}));
|
||||
vi.mock("@/api/issues", () => ({issuesApi: {list: mocks.list}}));
|
||||
describe("incoming call task selection", () => {
|
||||
let container: HTMLDivElement, root: Root, client: QueryClient;
|
||||
@@ -16,7 +16,7 @@ describe("incoming call task selection", () => {
|
||||
root = createRoot(container); client = new QueryClient({defaultOptions: {queries: {retry: false}}});
|
||||
mocks.incoming.mockResolvedValue([{id: "call", state: "awaiting_approval", approvalCode: "123456"}]);
|
||||
});
|
||||
afterEach(async () => {await act(async () => root.unmount()); client.clear(); container.remove();});
|
||||
afterEach(async () => {await act(async () => root.unmount()); client.clear(); container.remove(); vi.useRealTimers();});
|
||||
async function render() {await act(async () => root.render(<MemoryRouter><QueryClientProvider client={client}><SpekoIncomingCalls companyId="company" endpointId="endpoint" agentId="agent" /></QueryClientProvider></MemoryRouter>));}
|
||||
it("shows task request failures and lets callers retry the existing task inventory", async () => {
|
||||
mocks.list.mockRejectedValueOnce(new Error("Offline")).mockResolvedValue(tasks); await render();
|
||||
@@ -36,4 +36,22 @@ describe("incoming call task selection", () => {
|
||||
await vi.waitFor(() => expect(container.textContent).toContain("Task list could not be loaded"));
|
||||
expect(container.querySelector('option[value="task"]')).not.toBeNull();
|
||||
});
|
||||
it("expires a saved approval confirmation when the pending-call list omits it", async () => {
|
||||
mocks.list.mockResolvedValue(tasks);
|
||||
mocks.decide.mockResolvedValue({id: "call", state: "approved", approvalCode: "123456"});
|
||||
await render();
|
||||
await vi.waitFor(() => expect(container.querySelector('input')).not.toBeNull());
|
||||
mocks.incoming.mockResolvedValue([]);
|
||||
const input = container.querySelector('input')!;
|
||||
await act(async () => {
|
||||
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, '123456');
|
||||
input.dispatchEvent(new Event('input', {bubbles: true}));
|
||||
});
|
||||
await act(async () => container.querySelector('form')!.dispatchEvent(new Event('submit', {bubbles: true, cancelable: true})));
|
||||
await vi.waitFor(() => expect(container.textContent).toContain('Call approved'));
|
||||
expect(container.textContent).not.toContain('You can now give instructions');
|
||||
await act(async () => { await new Promise(resolve => setTimeout(resolve, 5100)); });
|
||||
expect(container.textContent).not.toContain('Call approved');
|
||||
}, 10000);
|
||||
|
||||
});
|
||||
@@ -29,12 +29,19 @@ export function SpekoIncomingCallCard({call, tasks = [], busy, error, onDecide}:
|
||||
{invalid && <p role="alert" className="text-sm text-destructive">That code does not match this live call. Ask the phone agent to repeat it.</p>}
|
||||
{error && <p role="alert" className="text-sm text-destructive">{error}</p>}
|
||||
<div className="flex items-center justify-between gap-3"><Button type="button" variant="ghost" disabled={busy} onClick={() => onDecide({approve: false, approvalCode: call.approvalCode})}>Deny call</Button><Button type="submit" disabled={busy}>{busy ? "Confirming…" : "Approve live call"}</Button></div>
|
||||
</form> : <p className="text-sm text-muted-foreground">{call.state === "approved" ? "You can now give instructions on the phone. Earlier instructions were discarded." : "This call cannot access your private tasks."}</p>}
|
||||
</form> : <p className="text-sm text-muted-foreground">{call.state === "approved" ? "Approval was sent for this call. Earlier instructions were discarded. Follow the call in Activity." : "This call cannot access your private tasks."}</p>}
|
||||
</section>;
|
||||
}
|
||||
export function SpekoIncomingCalls({companyId, endpointId, agentId}: {companyId: string; endpointId: string; agentId: string}) {
|
||||
const [decision, setDecision] = useState<VoiceInboundCall>();
|
||||
const host = useRef<HTMLDivElement>(null), restoreTaskFocus = useRef(false);
|
||||
useEffect(() => {
|
||||
if (!decision) return;
|
||||
// The pending-call API omits approved calls; this is a brief confirmation,
|
||||
// not a live status indicator after the caller hangs up.
|
||||
const timeout = setTimeout(() => setDecision(undefined), 5000);
|
||||
return () => clearTimeout(timeout);
|
||||
}, [decision]);
|
||||
useEffect(() => { setDecision(undefined); restoreTaskFocus.current = false; }, [companyId, endpointId]);
|
||||
const query = useQuery({queryKey: ["speko-incoming", companyId, endpointId], queryFn: () => voicePhoneApi.incoming(companyId, endpointId), refetchInterval: 2000, retry: false});
|
||||
const needsTaskSelection = Boolean(query.data?.some(call => ["awaiting_approval", "approving"].includes(call.state)));
|
||||
|
||||
@@ -55,3 +55,28 @@ export const TaskListFailureAndRecovery: Story = {
|
||||
await waitFor(() => expect(canvas.getByRole("combobox", {name: "Conversation task"})).toHaveFocus());
|
||||
},
|
||||
};
|
||||
|
||||
function ApprovalConfirmation() {
|
||||
const [ready, setReady] = useState(false);
|
||||
useLayoutEffect(() => {
|
||||
const originalIncoming = voicePhoneApi.incoming, originalDecide = voicePhoneApi.decide, originalTasks = issuesApi.list;
|
||||
let approved = false;
|
||||
voicePhoneApi.incoming = async () => approved ? [] : [call];
|
||||
voicePhoneApi.decide = async () => { approved = true; return {...call, state: "approved"}; };
|
||||
issuesApi.list = async () => [];
|
||||
setReady(true);
|
||||
return () => { voicePhoneApi.incoming = originalIncoming; voicePhoneApi.decide = originalDecide; issuesApi.list = originalTasks; };
|
||||
}, []);
|
||||
return ready ? <SpekoIncomingCalls companyId={spekoCompanyId} endpointId={spekoEndpoint.id} agentId={spekoAgent.id} /> : null;
|
||||
}
|
||||
export const ApprovalConfirmationExpires: Story = {
|
||||
render: () => <SpekoQueryFixture><ApprovalConfirmation /></SpekoQueryFixture>,
|
||||
play: async ({canvasElement}) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.type(await canvas.findByRole("textbox", {name: "Code spoken on your call"}), "123456");
|
||||
await userEvent.click(canvas.getByRole("button", {name: "Approve live call"}));
|
||||
await expect(await canvas.findByRole("heading", {name: "Call approved"})).toHaveFocus();
|
||||
await expect(canvas.getByText(/Approval was sent/)).toBeVisible();
|
||||
await waitFor(() => expect(canvas.queryByRole("heading", {name: "Call approved"})).not.toBeInTheDocument(), {timeout: 6500});
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user