|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + buildMatrixApprovalReactionHint, |
| 4 | + clearMatrixApprovalReactionTargetsForTest, |
| 5 | + listMatrixApprovalReactionBindings, |
| 6 | + registerMatrixApprovalReactionTarget, |
| 7 | + resolveMatrixApprovalReactionTarget, |
| 8 | + unregisterMatrixApprovalReactionTarget, |
| 9 | +} from "./approval-reactions.js"; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + clearMatrixApprovalReactionTargetsForTest(); |
| 13 | +}); |
| 14 | + |
| 15 | +describe("matrix approval reactions", () => { |
| 16 | + it("lists reactions in stable decision order", () => { |
| 17 | + expect(listMatrixApprovalReactionBindings(["allow-once", "deny", "allow-always"])).toEqual([ |
| 18 | + { decision: "allow-once", emoji: "✅", label: "Allow once" }, |
| 19 | + { decision: "allow-always", emoji: "♾️", label: "Allow always" }, |
| 20 | + { decision: "deny", emoji: "❌", label: "Deny" }, |
| 21 | + ]); |
| 22 | + }); |
| 23 | + |
| 24 | + it("builds a compact reaction hint", () => { |
| 25 | + expect(buildMatrixApprovalReactionHint(["allow-once", "deny"])).toBe( |
| 26 | + "React here: ✅ Allow once, ❌ Deny", |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it("resolves a registered approval anchor event back to an approval decision", () => { |
| 31 | + registerMatrixApprovalReactionTarget({ |
| 32 | + roomId: "!ops:example.org", |
| 33 | + eventId: "$approval-msg", |
| 34 | + approvalId: "req-123", |
| 35 | + allowedDecisions: ["allow-once", "allow-always", "deny"], |
| 36 | + }); |
| 37 | + |
| 38 | + expect( |
| 39 | + resolveMatrixApprovalReactionTarget({ |
| 40 | + roomId: "!ops:example.org", |
| 41 | + eventId: "$approval-msg", |
| 42 | + reactionKey: "✅", |
| 43 | + }), |
| 44 | + ).toEqual({ |
| 45 | + approvalId: "req-123", |
| 46 | + decision: "allow-once", |
| 47 | + }); |
| 48 | + expect( |
| 49 | + resolveMatrixApprovalReactionTarget({ |
| 50 | + roomId: "!ops:example.org", |
| 51 | + eventId: "$approval-msg", |
| 52 | + reactionKey: "♾️", |
| 53 | + }), |
| 54 | + ).toEqual({ |
| 55 | + approvalId: "req-123", |
| 56 | + decision: "allow-always", |
| 57 | + }); |
| 58 | + expect( |
| 59 | + resolveMatrixApprovalReactionTarget({ |
| 60 | + roomId: "!ops:example.org", |
| 61 | + eventId: "$approval-msg", |
| 62 | + reactionKey: "❌", |
| 63 | + }), |
| 64 | + ).toEqual({ |
| 65 | + approvalId: "req-123", |
| 66 | + decision: "deny", |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("ignores reactions that are not allowed on the registered approval anchor event", () => { |
| 71 | + registerMatrixApprovalReactionTarget({ |
| 72 | + roomId: "!ops:example.org", |
| 73 | + eventId: "$approval-msg", |
| 74 | + approvalId: "req-123", |
| 75 | + allowedDecisions: ["allow-once", "deny"], |
| 76 | + }); |
| 77 | + |
| 78 | + expect( |
| 79 | + resolveMatrixApprovalReactionTarget({ |
| 80 | + roomId: "!ops:example.org", |
| 81 | + eventId: "$approval-msg", |
| 82 | + reactionKey: "♾️", |
| 83 | + }), |
| 84 | + ).toBeNull(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("stops resolving reactions after the approval anchor event is unregistered", () => { |
| 88 | + registerMatrixApprovalReactionTarget({ |
| 89 | + roomId: "!ops:example.org", |
| 90 | + eventId: "$approval-msg", |
| 91 | + approvalId: "req-123", |
| 92 | + allowedDecisions: ["allow-once", "allow-always", "deny"], |
| 93 | + }); |
| 94 | + unregisterMatrixApprovalReactionTarget({ |
| 95 | + roomId: "!ops:example.org", |
| 96 | + eventId: "$approval-msg", |
| 97 | + }); |
| 98 | + |
| 99 | + expect( |
| 100 | + resolveMatrixApprovalReactionTarget({ |
| 101 | + roomId: "!ops:example.org", |
| 102 | + eventId: "$approval-msg", |
| 103 | + reactionKey: "✅", |
| 104 | + }), |
| 105 | + ).toBeNull(); |
| 106 | + }); |
| 107 | +}); |
0 commit comments