|
| 1 | +import { describe, expect, it, beforeEach } from "vitest"; |
| 2 | +import { SystemConfig } from "./config"; |
| 3 | +import { MessageQueue } from "@Packages/message/message_queue"; |
| 4 | + |
| 5 | +describe("SystemConfig 双 storage 与懒迁移", () => { |
| 6 | + let mq: MessageQueue; |
| 7 | + let config: SystemConfig; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + // 清空 storage 数据 |
| 11 | + chrome.storage.sync.clear(); |
| 12 | + chrome.storage.local.clear(); |
| 13 | + mq = new MessageQueue(); |
| 14 | + config = new SystemConfig(mq); |
| 15 | + }); |
| 16 | + |
| 17 | + describe("local key 读写", () => { |
| 18 | + it("cloud_sync 应写入 local storage 而非 sync", async () => { |
| 19 | + const cloudSync = { |
| 20 | + enable: true, |
| 21 | + syncDelete: false, |
| 22 | + syncStatus: true, |
| 23 | + filesystem: "onedrive" as const, |
| 24 | + params: { token: "test" }, |
| 25 | + }; |
| 26 | + config.setCloudSync(cloudSync); |
| 27 | + |
| 28 | + const result = await config.getCloudSync(); |
| 29 | + expect(result).toEqual(cloudSync); |
| 30 | + |
| 31 | + // 验证值在 local 中 |
| 32 | + const localData = await chrome.storage.local.get("system_cloud_sync"); |
| 33 | + expect(localData["system_cloud_sync"]).toEqual(cloudSync); |
| 34 | + |
| 35 | + // 验证值不在 sync 中 |
| 36 | + const syncData = await chrome.storage.sync.get("system_cloud_sync"); |
| 37 | + expect(syncData["system_cloud_sync"]).toBeUndefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("language 应写入 local storage", async () => { |
| 41 | + config.setLanguage("zh-CN"); |
| 42 | + |
| 43 | + // 通过 storage 验证写入位置 |
| 44 | + const localData = await chrome.storage.local.get("system_language"); |
| 45 | + expect(localData["system_language"]).toBe("zh-CN"); |
| 46 | + |
| 47 | + const syncData = await chrome.storage.sync.get("system_language"); |
| 48 | + expect(syncData["system_language"]).toBeUndefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("vscode_url 应写入 local storage", async () => { |
| 52 | + config.setVscodeUrl("ws://localhost:9999"); |
| 53 | + |
| 54 | + const localData = await chrome.storage.local.get("system_vscode_url"); |
| 55 | + expect(localData["system_vscode_url"]).toBe("ws://localhost:9999"); |
| 56 | + |
| 57 | + const syncData = await chrome.storage.sync.get("system_vscode_url"); |
| 58 | + expect(syncData["system_vscode_url"]).toBeUndefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("enable_script 应写入 local storage", async () => { |
| 62 | + config.setEnableScript(false); |
| 63 | + |
| 64 | + const localData = await chrome.storage.local.get("system_enable_script"); |
| 65 | + expect(localData["system_enable_script"]).toBe(false); |
| 66 | + |
| 67 | + const syncData = await chrome.storage.sync.get("system_enable_script"); |
| 68 | + expect(syncData["system_enable_script"]).toBeUndefined(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("sync key 读写", () => { |
| 73 | + it("check_script_update_cycle 应写入 sync storage", async () => { |
| 74 | + config.setCheckScriptUpdateCycle(3600); |
| 75 | + |
| 76 | + const syncData = await chrome.storage.sync.get("system_check_script_update_cycle"); |
| 77 | + expect(syncData["system_check_script_update_cycle"]).toBe(3600); |
| 78 | + |
| 79 | + const localData = await chrome.storage.local.get("system_check_script_update_cycle"); |
| 80 | + expect(localData["system_check_script_update_cycle"]).toBeUndefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("enable_eslint 应写入 sync storage", async () => { |
| 84 | + config.setEnableEslint(false); |
| 85 | + |
| 86 | + const syncData = await chrome.storage.sync.get("system_enable_eslint"); |
| 87 | + expect(syncData["system_enable_eslint"]).toBe(false); |
| 88 | + |
| 89 | + const localData = await chrome.storage.local.get("system_enable_eslint"); |
| 90 | + expect(localData["system_enable_eslint"]).toBeUndefined(); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe("懒迁移:sync → local", () => { |
| 95 | + it("local key 的旧数据应从 sync 迁移到 local", async () => { |
| 96 | + // 模拟旧版本数据在 sync 中 |
| 97 | + const oldCloudSync = { |
| 98 | + enable: true, |
| 99 | + syncDelete: false, |
| 100 | + syncStatus: true, |
| 101 | + filesystem: "webdav" as const, |
| 102 | + params: { url: "https://example.com" }, |
| 103 | + }; |
| 104 | + await chrome.storage.sync.set({ system_cloud_sync: oldCloudSync }); |
| 105 | + |
| 106 | + // 读取时应自动迁移 |
| 107 | + const result = await config.getCloudSync(); |
| 108 | + expect(result).toEqual(oldCloudSync); |
| 109 | + |
| 110 | + // 验证已迁移到 local |
| 111 | + const localData = await chrome.storage.local.get("system_cloud_sync"); |
| 112 | + expect(localData["system_cloud_sync"]).toEqual(oldCloudSync); |
| 113 | + |
| 114 | + // 验证已从 sync 中删除 |
| 115 | + const syncData = await chrome.storage.sync.get("system_cloud_sync"); |
| 116 | + expect(syncData["system_cloud_sync"]).toBeUndefined(); |
| 117 | + }); |
| 118 | + |
| 119 | + it("local 有值时不应回退到 sync", async () => { |
| 120 | + const localValue = "zh-CN"; |
| 121 | + const syncValue = "en-US"; |
| 122 | + await chrome.storage.local.set({ system_language: localValue }); |
| 123 | + await chrome.storage.sync.set({ system_language: syncValue }); |
| 124 | + |
| 125 | + const result = await config.getLanguage(); |
| 126 | + expect(result).toBe(localValue); |
| 127 | + |
| 128 | + // sync 中的值不应被删除(因为 local 有值,不触发迁移) |
| 129 | + const syncData = await chrome.storage.sync.get("system_language"); |
| 130 | + expect(syncData["system_language"]).toBe(syncValue); |
| 131 | + }); |
| 132 | + |
| 133 | + it("sync 和 local 都没有值时返回默认值", async () => { |
| 134 | + const result = await config.getCloudSync(); |
| 135 | + expect(result).toEqual({ |
| 136 | + enable: false, |
| 137 | + syncDelete: false, |
| 138 | + syncStatus: true, |
| 139 | + filesystem: "webdav", |
| 140 | + params: {}, |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it("迁移后再次读取应走缓存", async () => { |
| 145 | + await chrome.storage.sync.set({ system_vscode_url: "ws://old:8642" }); |
| 146 | + |
| 147 | + // 第一次读取触发迁移 |
| 148 | + const first = await config.getVscodeUrl(); |
| 149 | + expect(first).toBe("ws://old:8642"); |
| 150 | + |
| 151 | + // 修改 local storage(模拟外部写入),验证缓存生效 |
| 152 | + await chrome.storage.local.set({ system_vscode_url: "ws://new:9999" }); |
| 153 | + |
| 154 | + // 第二次读取应返回缓存值 |
| 155 | + const second = await config.getVscodeUrl(); |
| 156 | + expect(second).toBe("ws://old:8642"); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments