|
| 1 | +import { cookieParams } from "./cookie_params"; |
| 2 | + |
| 3 | +// 用于模拟 Firefox 环境的辅助函数 |
| 4 | +const mockFirefox = () => { |
| 5 | + (globalThis as any).mozInnerScreenX = 0; |
| 6 | +}; |
| 7 | + |
| 8 | +const mockNonFirefox = () => { |
| 9 | + delete (globalThis as any).mozInnerScreenX; |
| 10 | +}; |
| 11 | + |
| 12 | +describe("cookieParams", () => { |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + mockNonFirefox(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe("undefined 过滤", () => { |
| 19 | + it("从参数中移除 undefined 值", () => { |
| 20 | + const result = cookieParams({ domain: "example.com", name: undefined }); |
| 21 | + expect(result).toEqual({ domain: "example.com" }); |
| 22 | + }); |
| 23 | + |
| 24 | + it("保留 null 和 false 值", () => { |
| 25 | + const result = cookieParams({ domain: null, secure: false, name: undefined }); |
| 26 | + expect(result).toEqual({ domain: null, secure: false }); |
| 27 | + }); |
| 28 | + |
| 29 | + it("保留空字符串值", () => { |
| 30 | + const result = cookieParams({ domain: "", name: "test" }); |
| 31 | + expect(result).toEqual({ domain: "", name: "test" }); |
| 32 | + }); |
| 33 | + |
| 34 | + it("当所有值都是 undefined 时返回空对象", () => { |
| 35 | + const result = cookieParams({ domain: undefined, name: undefined }); |
| 36 | + expect(result).toEqual({}); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("过滤掉 undefined 属性", () => { |
| 41 | + const result = cookieParams({ |
| 42 | + url: "https://example.com", |
| 43 | + domain: undefined, |
| 44 | + name: undefined, |
| 45 | + path: "/", |
| 46 | + secure: undefined, |
| 47 | + }); |
| 48 | + expect(result).toEqual({ url: "https://example.com", path: "/" }); |
| 49 | + expect("domain" in result).toBe(false); |
| 50 | + expect("name" in result).toBe(false); |
| 51 | + expect("secure" in result).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it("保留所有非 undefined 的属性(包括 null 和 false)", () => { |
| 55 | + const result = cookieParams({ |
| 56 | + url: "https://example.com", |
| 57 | + secure: false, |
| 58 | + session: false, |
| 59 | + storeId: null as unknown as string, |
| 60 | + }); |
| 61 | + expect(result).toEqual({ |
| 62 | + url: "https://example.com", |
| 63 | + secure: false, |
| 64 | + session: false, |
| 65 | + storeId: null, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe("Firefox:注入 firstPartyDomain", () => { |
| 70 | + it("在 Firefox 中注入 firstPartyDomain: null(情况1)", () => { |
| 71 | + mockFirefox(); |
| 72 | + const result = cookieParams({ domain: "example.com" }); |
| 73 | + expect(result).toEqual({ domain: "example.com", firstPartyDomain: null }); |
| 74 | + }); |
| 75 | + |
| 76 | + it("在 Firefox 中注入 firstPartyDomain: null(情况2)", () => { |
| 77 | + mockFirefox(); |
| 78 | + const result = cookieParams({ url: "https://example.com" }); |
| 79 | + expect(result).toEqual({ url: "https://example.com", firstPartyDomain: null }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("在 Firefox 中覆盖已有的 firstPartyDomain(情况1)", () => { |
| 83 | + mockFirefox(); |
| 84 | + const result = cookieParams({ domain: "example.com", firstPartyDomain: "other.com" }); |
| 85 | + expect(result.firstPartyDomain).toBeNull(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("在 Firefox 中覆盖已有的 firstPartyDomain(情况2)", () => { |
| 89 | + mockFirefox(); |
| 90 | + const result = cookieParams({ url: "https://example.com", firstPartyDomain: "other.com" }); |
| 91 | + expect(result.firstPartyDomain).toBeNull(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("在非 Firefox 环境下不注入 firstPartyDomain(情况1)", () => { |
| 95 | + mockNonFirefox(); |
| 96 | + const result = cookieParams({ domain: "example.com" }); |
| 97 | + expect(result).not.toHaveProperty("firstPartyDomain"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("在非 Firefox 环境下不注入 firstPartyDomain(情况2)", () => { |
| 101 | + mockNonFirefox(); |
| 102 | + const result = cookieParams({ url: "https://example.com" }); |
| 103 | + expect(result).not.toHaveProperty("firstPartyDomain"); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("真实 GM_cookie 使用场景", () => { |
| 108 | + it("处理带有可选字段的列表查询", () => { |
| 109 | + const result = cookieParams({ |
| 110 | + domain: "example.com", |
| 111 | + name: undefined, |
| 112 | + path: undefined, |
| 113 | + secure: true, |
| 114 | + session: undefined, |
| 115 | + url: undefined, |
| 116 | + storeId: undefined, |
| 117 | + }); |
| 118 | + expect(result).toEqual({ domain: "example.com", secure: true }); |
| 119 | + }); |
| 120 | + |
| 121 | + it("处理删除查询", () => { |
| 122 | + const result = cookieParams({ |
| 123 | + name: "session", |
| 124 | + url: "https://example.com", |
| 125 | + storeId: undefined, |
| 126 | + }); |
| 127 | + expect(result).toEqual({ name: "session", url: "https://example.com" }); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | +}); |
0 commit comments