|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + resolveWhatsAppAccountPolicy, |
| 4 | + resolveWhatsAppDirectTargetAuthorization, |
| 5 | +} from "./account-policy.js"; |
| 6 | + |
| 7 | +describe("resolveWhatsAppAccountPolicy", () => { |
| 8 | + it("prefers explicit accountId over configured defaultAccount", () => { |
| 9 | + const policy = resolveWhatsAppAccountPolicy({ |
| 10 | + cfg: { |
| 11 | + channels: { |
| 12 | + whatsapp: { |
| 13 | + defaultAccount: "work", |
| 14 | + accounts: { |
| 15 | + personal: { allowFrom: ["+15550000001"] }, |
| 16 | + work: { allowFrom: ["+15550000002"] }, |
| 17 | + }, |
| 18 | + }, |
| 19 | + }, |
| 20 | + } as Parameters<typeof resolveWhatsAppAccountPolicy>[0]["cfg"], |
| 21 | + accountId: "personal", |
| 22 | + }); |
| 23 | + |
| 24 | + expect(policy.accountId).toBe("personal"); |
| 25 | + expect(policy.configuredAllowFrom).toEqual(["+15550000001"]); |
| 26 | + }); |
| 27 | + |
| 28 | + it("uses configured defaultAccount when accountId is omitted", () => { |
| 29 | + const policy = resolveWhatsAppAccountPolicy({ |
| 30 | + cfg: { |
| 31 | + channels: { |
| 32 | + whatsapp: { |
| 33 | + defaultAccount: "work", |
| 34 | + accounts: { |
| 35 | + work: { allowFrom: ["+15550000002"] }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } as Parameters<typeof resolveWhatsAppAccountPolicy>[0]["cfg"], |
| 40 | + }); |
| 41 | + |
| 42 | + expect(policy.accountId).toBe("work"); |
| 43 | + expect(policy.configuredAllowFrom).toEqual(["+15550000002"]); |
| 44 | + }); |
| 45 | + |
| 46 | + it("falls back to the default account when no preferred account is configured", () => { |
| 47 | + const policy = resolveWhatsAppAccountPolicy({ |
| 48 | + cfg: { |
| 49 | + channels: { |
| 50 | + whatsapp: {}, |
| 51 | + }, |
| 52 | + } as Parameters<typeof resolveWhatsAppAccountPolicy>[0]["cfg"], |
| 53 | + }); |
| 54 | + |
| 55 | + expect(policy.accountId).toBe("default"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("inherits shared defaults from accounts.default for named accounts", () => { |
| 59 | + const policy = resolveWhatsAppAccountPolicy({ |
| 60 | + cfg: { |
| 61 | + channels: { |
| 62 | + whatsapp: { |
| 63 | + accounts: { |
| 64 | + default: { |
| 65 | + dmPolicy: "allowlist", |
| 66 | + allowFrom: [" +15550001111 "], |
| 67 | + groupPolicy: "open", |
| 68 | + groupAllowFrom: [" +15550002222 "], |
| 69 | + }, |
| 70 | + work: {}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } as Parameters<typeof resolveWhatsAppAccountPolicy>[0]["cfg"], |
| 75 | + accountId: "work", |
| 76 | + }); |
| 77 | + |
| 78 | + expect(policy.dmPolicy).toBe("allowlist"); |
| 79 | + expect(policy.groupPolicy).toBe("open"); |
| 80 | + expect(policy.configuredAllowFrom).toEqual(["+15550001111"]); |
| 81 | + expect(policy.groupAllowFrom).toEqual(["+15550002222"]); |
| 82 | + }); |
| 83 | + |
| 84 | + it("does not inherit default-only authDir or selfChatMode for named accounts", () => { |
| 85 | + const policy = resolveWhatsAppAccountPolicy({ |
| 86 | + cfg: { |
| 87 | + channels: { |
| 88 | + whatsapp: { |
| 89 | + accounts: { |
| 90 | + default: { |
| 91 | + authDir: "/tmp/default-auth", |
| 92 | + selfChatMode: true, |
| 93 | + }, |
| 94 | + work: {}, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } as Parameters<typeof resolveWhatsAppAccountPolicy>[0]["cfg"], |
| 99 | + accountId: "work", |
| 100 | + selfE164: "+15550009999", |
| 101 | + }); |
| 102 | + |
| 103 | + expect(policy.account.authDir).toMatch(/whatsapp[/\\]work$/); |
| 104 | + expect(policy.account.selfChatMode).toBeUndefined(); |
| 105 | + expect(policy.isSelfChat).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + it("falls back groupAllowFrom to allowFrom when unset", () => { |
| 109 | + const policy = resolveWhatsAppAccountPolicy({ |
| 110 | + cfg: { |
| 111 | + channels: { |
| 112 | + whatsapp: { |
| 113 | + allowFrom: [" +15550001111 ", "+15550002222"], |
| 114 | + }, |
| 115 | + }, |
| 116 | + } as Parameters<typeof resolveWhatsAppAccountPolicy>[0]["cfg"], |
| 117 | + }); |
| 118 | + |
| 119 | + expect(policy.configuredAllowFrom).toEqual(["+15550001111", "+15550002222"]); |
| 120 | + expect(policy.groupAllowFrom).toEqual(["+15550001111", "+15550002222"]); |
| 121 | + }); |
| 122 | + |
| 123 | + it("keeps self-chat classification inputs aligned with allowFrom and selfE164", () => { |
| 124 | + const policy = resolveWhatsAppAccountPolicy({ |
| 125 | + cfg: { |
| 126 | + channels: { |
| 127 | + whatsapp: { |
| 128 | + allowFrom: ["+15550009999"], |
| 129 | + }, |
| 130 | + }, |
| 131 | + } as Parameters<typeof resolveWhatsAppAccountPolicy>[0]["cfg"], |
| 132 | + selfE164: "+15550009999", |
| 133 | + }); |
| 134 | + |
| 135 | + expect(policy.dmAllowFrom).toEqual(["+15550009999"]); |
| 136 | + expect(policy.isSamePhone("+15550009999")).toBe(true); |
| 137 | + expect(policy.isDmSenderAllowed(policy.dmAllowFrom, "+15550009999")).toBe(true); |
| 138 | + expect(policy.isSelfChat).toBe(true); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe("resolveWhatsAppDirectTargetAuthorization", () => { |
| 143 | + it("allows wildcard direct targets", () => { |
| 144 | + const authorized = resolveWhatsAppDirectTargetAuthorization({ |
| 145 | + cfg: { |
| 146 | + channels: { |
| 147 | + whatsapp: { |
| 148 | + allowFrom: ["*"], |
| 149 | + }, |
| 150 | + }, |
| 151 | + } as Parameters<typeof resolveWhatsAppDirectTargetAuthorization>[0]["cfg"], |
| 152 | + to: "+15550007777", |
| 153 | + }); |
| 154 | + |
| 155 | + expect(authorized.accountId).toBe("default"); |
| 156 | + expect(authorized.resolution).toEqual({ ok: true, to: "+15550007777" }); |
| 157 | + }); |
| 158 | + |
| 159 | + it("allows direct targets when allowFrom is empty", () => { |
| 160 | + const authorized = resolveWhatsAppDirectTargetAuthorization({ |
| 161 | + cfg: { |
| 162 | + channels: { |
| 163 | + whatsapp: {}, |
| 164 | + }, |
| 165 | + } as Parameters<typeof resolveWhatsAppDirectTargetAuthorization>[0]["cfg"], |
| 166 | + to: "+15550007777", |
| 167 | + }); |
| 168 | + |
| 169 | + expect(authorized.resolution).toEqual({ ok: true, to: "+15550007777" }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("blocks non-allowlisted direct targets", () => { |
| 173 | + const authorized = resolveWhatsAppDirectTargetAuthorization({ |
| 174 | + cfg: { |
| 175 | + channels: { |
| 176 | + whatsapp: { |
| 177 | + allowFrom: ["+15550001111"], |
| 178 | + }, |
| 179 | + }, |
| 180 | + } as Parameters<typeof resolveWhatsAppDirectTargetAuthorization>[0]["cfg"], |
| 181 | + to: "+15550007777", |
| 182 | + }); |
| 183 | + |
| 184 | + expect(authorized.resolution.ok).toBe(false); |
| 185 | + }); |
| 186 | + |
| 187 | + it("always allows group JIDs", () => { |
| 188 | + const authorized = resolveWhatsAppDirectTargetAuthorization({ |
| 189 | + cfg: { |
| 190 | + channels: { |
| 191 | + whatsapp: { |
| 192 | + allowFrom: ["+15550001111"], |
| 193 | + }, |
| 194 | + }, |
| 195 | + } as Parameters<typeof resolveWhatsAppDirectTargetAuthorization>[0]["cfg"], |
| 196 | + |
| 197 | + }); |
| 198 | + |
| 199 | + expect(authorized.resolution).toEqual({ ok: true, to: "[email protected]" }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments