diff --git a/src/app/service/service_worker/popup.ts b/src/app/service/service_worker/popup.ts index 100408485..04cb9b529 100644 --- a/src/app/service/service_worker/popup.ts +++ b/src/app/service/service_worker/popup.ts @@ -340,7 +340,7 @@ export class PopupService { async getPopupData(req: GetPopupDataReq): Promise { const { url, tabId } = req; const [matchingResult, runScripts, backScriptList] = await Promise.all([ - this.runtime.getPageScriptMatchingResultByUrl(url, true, true), + this.runtime.getPopupPageScriptMatchingResultByUrl(url), this.getScriptMenu(tabId), this.getScriptMenu(-1), ]); diff --git a/src/app/service/service_worker/resource.test.ts b/src/app/service/service_worker/resource.test.ts index eb279f485..40aa9fb7c 100644 --- a/src/app/service/service_worker/resource.test.ts +++ b/src/app/service/service_worker/resource.test.ts @@ -3,6 +3,7 @@ import { ResourceService } from "./resource"; import { vi, describe, it, expect, beforeEach } from "vitest"; import type { Group } from "@Packages/message/server"; import type { IMessageQueue } from "@Packages/message/message_queue"; +import type { Resource } from "@App/app/repo/resource"; initTestEnv(); @@ -96,3 +97,53 @@ describe("ResourceService - loadByUrl", () => { expect(res.contentType).toBe("application/octet-stream"); }); }); + +describe("ResourceService - getScriptResources", () => { + let service: ResourceService; + + const createResource = (url: string): Resource => ({ + url, + content: "local content", + contentType: "text/plain", + hash: { + md5: "", + sha1: "", + sha256: "", + sha384: "", + sha512: "sha512", + }, + base64: "", + link: {}, + type: "resource", + createtime: 1, + updatetime: 1, + }); + + beforeEach(() => { + vi.clearAllMocks(); + const mockGroup = {} as Group; + const mockMQ = {} as IMessageQueue; + service = new ResourceService(mockGroup, mockMQ); + }); + + it("命名 @resource 使用 file:/// 时应立即刷新本地文件", async () => { + const url = "file:///tmp/local.txt"; + const resource = createResource(url); + const updateResource = vi.spyOn(service, "updateResource").mockResolvedValue(resource); + const getResource = vi.spyOn(service, "getResource"); + + const result = await service.getScriptResources( + { + uuid: "script-uuid", + metadata: { + resource: [`asset ${url}`], + }, + } as any, + false + ); + + expect(updateResource).toHaveBeenCalledWith("script-uuid", url, "resource"); + expect(getResource).not.toHaveBeenCalled(); + expect(result.asset).toBe(resource); + }); +}); diff --git a/src/app/service/service_worker/resource.ts b/src/app/service/service_worker/resource.ts index f78639f12..b7b1fb4af 100644 --- a/src/app/service/service_worker/resource.ts +++ b/src/app/service/service_worker/resource.ts @@ -98,7 +98,7 @@ export class ResourceService { } } if (path) { - if (uri.startsWith("file:///")) { + if (path.startsWith("file:///")) { // 如果是file://协议,则每次请求更新一下文件 const res = await this.updateResource(script.uuid, path, type); ret[resourceKey] = res; diff --git a/src/app/service/service_worker/runtime.test.ts b/src/app/service/service_worker/runtime.test.ts index 0e29cc5bb..942de46f3 100644 --- a/src/app/service/service_worker/runtime.test.ts +++ b/src/app/service/service_worker/runtime.test.ts @@ -3,7 +3,12 @@ import { RuntimeService } from "./runtime"; import { vi, describe, it, expect, beforeEach, type MockedFunction } from "vitest"; import { randomUUID } from "crypto"; import type { Script, ScriptRunResource } from "@App/app/repo/scripts"; -import { SCRIPT_STATUS_DISABLE, SCRIPT_STATUS_ENABLE, SCRIPT_TYPE_NORMAL } from "@App/app/repo/scripts"; +import { + SCRIPT_STATUS_DISABLE, + SCRIPT_STATUS_ENABLE, + SCRIPT_TYPE_BACKGROUND, + SCRIPT_TYPE_NORMAL, +} from "@App/app/repo/scripts"; import { getCombinedMeta } from "./utils"; import type { SystemConfig } from "@App/pkg/config/config"; import type { Group } from "@Packages/message/server"; @@ -16,10 +21,11 @@ import type { ScriptDAO } from "@App/app/repo/scripts"; import { LocalStorageDAO } from "@App/app/repo/localStorage"; import type { MessageConnect, TMessage } from "@Packages/message/types"; import { obtainBlackList } from "@App/pkg/utils/utils"; +import type { CompiledResource, Resource } from "@App/app/repo/resource"; initTestEnv(); -describe.concurrent("RuntimeService - getPageScriptMatchingResultByUrl 脚本匹配", () => { +describe("RuntimeService - getPageScriptMatchingResultByUrl 脚本匹配", () => { let runtime: RuntimeService; let mockSystemConfig: { getBlacklist: MockedFunction<() => string>; @@ -27,6 +33,25 @@ describe.concurrent("RuntimeService - getPageScriptMatchingResultByUrl 脚本匹 let mockScriptService: { buildScriptRunResource: MockedFunction<(script: Script, scriptFlag?: string) => ScriptRunResource>; }; + let mockValueService: { + getScriptValue: MockedFunction<(script: Script) => Promise>>; + }; + let mockResourceService: { + getScriptResources: MockedFunction<(script: Script, load: boolean) => Promise>>; + updateResource: MockedFunction<(uuid: string, url: string, type: any) => Promise>; + }; + let mockScriptDAO: { + all: MockedFunction<() => Promise>; + gets: MockedFunction<(uuids: string[]) => Promise<(Script | undefined)[]>>; + scriptCodeDAO: { + get: MockedFunction<(uuid: string) => Promise<{ uuid: string; code: string } | undefined>>; + }; + }; + + const updateMockScripts = async (scripts: Script[]) => { + mockScriptDAO.all.mockResolvedValue(scripts); + runtime.scriptMatchDisable = await runtime.createPopupDisabledScriptMatch(); + }; // 测试数据创建工具函数 const createMockScript = (overrides: Partial