|
| 1 | +/** |
| 2 | + * Tests for approval delay functionality |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect, afterEach } from "vitest" |
| 6 | +import { createStore } from "jotai" |
| 7 | +import { |
| 8 | + setPendingApprovalAtom, |
| 9 | + isApprovalPendingAtom, |
| 10 | + approvalSetTimestampAtom, |
| 11 | + clearPendingApprovalAtom, |
| 12 | +} from "../approval.js" |
| 13 | +import type { ExtensionChatMessage } from "../../../types/messages.js" |
| 14 | + |
| 15 | +describe("approval delay", () => { |
| 16 | + afterEach(() => { |
| 17 | + // Cleanup if needed |
| 18 | + }) |
| 19 | + |
| 20 | + it("should set timestamp when pending approval is set", () => { |
| 21 | + const store = createStore() |
| 22 | + const message: ExtensionChatMessage = { |
| 23 | + ts: Date.now(), |
| 24 | + type: "ask", |
| 25 | + ask: "tool", |
| 26 | + text: '{"tool":"readFile"}', |
| 27 | + } |
| 28 | + |
| 29 | + store.set(setPendingApprovalAtom, message) |
| 30 | + |
| 31 | + const timestamp = store.get(approvalSetTimestampAtom) |
| 32 | + expect(timestamp).not.toBeNull() |
| 33 | + expect(typeof timestamp).toBe("number") |
| 34 | + }) |
| 35 | + |
| 36 | + it("should clear timestamp when pending approval is cleared", () => { |
| 37 | + const store = createStore() |
| 38 | + const message: ExtensionChatMessage = { |
| 39 | + ts: Date.now(), |
| 40 | + type: "ask", |
| 41 | + ask: "tool", |
| 42 | + text: '{"tool":"readFile"}', |
| 43 | + } |
| 44 | + |
| 45 | + store.set(setPendingApprovalAtom, message) |
| 46 | + expect(store.get(approvalSetTimestampAtom)).not.toBeNull() |
| 47 | + |
| 48 | + store.set(clearPendingApprovalAtom) |
| 49 | + expect(store.get(approvalSetTimestampAtom)).toBeNull() |
| 50 | + }) |
| 51 | + |
| 52 | + it("isApprovalPendingAtom should return true immediately", () => { |
| 53 | + const store = createStore() |
| 54 | + const message: ExtensionChatMessage = { |
| 55 | + ts: Date.now(), |
| 56 | + type: "ask", |
| 57 | + ask: "tool", |
| 58 | + text: '{"tool":"readFile"}', |
| 59 | + } |
| 60 | + |
| 61 | + store.set(setPendingApprovalAtom, message) |
| 62 | + |
| 63 | + // Immediate check should return true |
| 64 | + expect(store.get(isApprovalPendingAtom)).toBe(true) |
| 65 | + }) |
| 66 | + |
| 67 | + it("should handle timestamp and approval state together", () => { |
| 68 | + const store = createStore() |
| 69 | + const message: ExtensionChatMessage = { |
| 70 | + ts: Date.now(), |
| 71 | + type: "ask", |
| 72 | + ask: "tool", |
| 73 | + text: '{"tool":"readFile"}', |
| 74 | + } |
| 75 | + |
| 76 | + store.set(setPendingApprovalAtom, message) |
| 77 | + |
| 78 | + // Both timestamp and approval should be set |
| 79 | + expect(store.get(approvalSetTimestampAtom)).not.toBeNull() |
| 80 | + expect(store.get(isApprovalPendingAtom)).toBe(true) |
| 81 | + |
| 82 | + // Clear approval |
| 83 | + store.set(clearPendingApprovalAtom) |
| 84 | + |
| 85 | + // Both should be cleared |
| 86 | + expect(store.get(approvalSetTimestampAtom)).toBeNull() |
| 87 | + expect(store.get(isApprovalPendingAtom)).toBe(false) |
| 88 | + }) |
| 89 | +}) |
0 commit comments