|
| 1 | +import { describe, it, expect, beforeEach } from "vitest"; |
| 2 | +import { ScriptDAO, SCRIPT_TYPE_NORMAL, SCRIPT_STATUS_ENABLE, SCRIPT_RUN_STATUS_COMPLETE } from "@App/app/repo/scripts"; |
| 3 | +import type { Script } from "@App/app/repo/scripts"; |
| 4 | +import { SubscribeDAO, SUBSCRIBE_STATUS_ENABLE } from "@App/app/repo/subscribe"; |
| 5 | +import type { Subscribe } from "@App/app/repo/subscribe"; |
| 6 | +import type { SCMetadata } from "@App/app/repo/metadata"; |
| 7 | +import GMApi, { MockGMExternalDependencies } from "./gm_api"; |
| 8 | +import { initTestEnv } from "@Tests/utils"; |
| 9 | +import { MockMessage } from "@Packages/message/mock_message"; |
| 10 | +import { Server } from "@Packages/message/server"; |
| 11 | +import EventEmitter from "eventemitter3"; |
| 12 | +import { MessageQueue } from "@Packages/message/message_queue"; |
| 13 | +import { SystemConfig } from "@App/pkg/config/config"; |
| 14 | +import PermissionVerify from "../permission_verify"; |
| 15 | + |
| 16 | +initTestEnv(); |
| 17 | + |
| 18 | +function makeScript(uuid: string, connect?: string[], subscribeUrl?: string): Script { |
| 19 | + const metadata: SCMetadata = {}; |
| 20 | + if (connect) metadata.connect = connect; |
| 21 | + return { |
| 22 | + uuid, |
| 23 | + name: "test-script", |
| 24 | + namespace: "test", |
| 25 | + metadata, |
| 26 | + type: SCRIPT_TYPE_NORMAL, |
| 27 | + status: SCRIPT_STATUS_ENABLE, |
| 28 | + sort: 0, |
| 29 | + runStatus: SCRIPT_RUN_STATUS_COMPLETE, |
| 30 | + createtime: Date.now(), |
| 31 | + checktime: Date.now(), |
| 32 | + subscribeUrl, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function makeSubscribe(url: string, connect?: string[]): Subscribe { |
| 37 | + const metadata: SCMetadata = {}; |
| 38 | + if (connect) metadata.connect = connect; |
| 39 | + return { |
| 40 | + url, |
| 41 | + name: "test-subscribe", |
| 42 | + code: "", |
| 43 | + author: "test", |
| 44 | + scripts: {}, |
| 45 | + metadata, |
| 46 | + status: SUBSCRIBE_STATUS_ENABLE, |
| 47 | + createtime: Date.now(), |
| 48 | + checktime: Date.now(), |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +function createGMApi(): GMApi { |
| 53 | + const ee = new EventEmitter<string, any>(); |
| 54 | + const message = new MockMessage(ee); |
| 55 | + const messageQueue = new MessageQueue(); |
| 56 | + const systemConfig = new SystemConfig(messageQueue); |
| 57 | + const server = new Server("serviceWorker", message); |
| 58 | + const permissionVerify = new PermissionVerify(server.group("permissionVerify"), messageQueue); |
| 59 | + return new GMApi( |
| 60 | + systemConfig, |
| 61 | + permissionVerify, |
| 62 | + server.group("runtime"), |
| 63 | + message, |
| 64 | + messageQueue, |
| 65 | + {} as any, |
| 66 | + new MockGMExternalDependencies() |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +describe("parseRequest 订阅脚本 connect 覆盖", () => { |
| 71 | + let scriptDAO: ScriptDAO; |
| 72 | + let subscribeDAO: SubscribeDAO; |
| 73 | + let gmApi: GMApi; |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + scriptDAO = new ScriptDAO(); |
| 77 | + subscribeDAO = new SubscribeDAO(); |
| 78 | + gmApi = createGMApi(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("订阅脚本的 connect 应被订阅的 connect 覆盖", async () => { |
| 82 | + const subscribeUrl = "https://example.com/test.sub.js"; |
| 83 | + const script = makeScript("uuid-1", ["script-domain.com"], subscribeUrl); |
| 84 | + const subscribe = makeSubscribe(subscribeUrl, ["subscribe-domain.com", "api.example.com"]); |
| 85 | + |
| 86 | + await scriptDAO.save(script); |
| 87 | + await subscribeDAO.save(subscribe); |
| 88 | + |
| 89 | + const req = await gmApi.parseRequest({ uuid: "uuid-1", api: "test", runFlag: "", params: [] }); |
| 90 | + // connect 应该被订阅的覆盖,而不是脚本自身的 |
| 91 | + expect(req.script.metadata.connect).toEqual(["subscribe-domain.com", "api.example.com"]); |
| 92 | + }); |
| 93 | + |
| 94 | + it("普通脚本(无 subscribeUrl)的 connect 保持不变", async () => { |
| 95 | + const script = makeScript("uuid-2", ["my-domain.com"]); |
| 96 | + await scriptDAO.save(script); |
| 97 | + |
| 98 | + const req = await gmApi.parseRequest({ uuid: "uuid-2", api: "test", runFlag: "", params: [] }); |
| 99 | + expect(req.script.metadata.connect).toEqual(["my-domain.com"]); |
| 100 | + }); |
| 101 | + |
| 102 | + it("订阅不存在时脚本 connect 保持不变", async () => { |
| 103 | + const script = makeScript("uuid-3", ["my-domain.com"], "https://gone.com/deleted.sub.js"); |
| 104 | + await scriptDAO.save(script); |
| 105 | + |
| 106 | + const req = await gmApi.parseRequest({ uuid: "uuid-3", api: "test", runFlag: "", params: [] }); |
| 107 | + expect(req.script.metadata.connect).toEqual(["my-domain.com"]); |
| 108 | + }); |
| 109 | + |
| 110 | + it("订阅没有声明 connect 时脚本 connect 保持不变", async () => { |
| 111 | + const subscribeUrl = "https://example.com/no-connect.sub.js"; |
| 112 | + const script = makeScript("uuid-4", ["my-domain.com"], subscribeUrl); |
| 113 | + const subscribe = makeSubscribe(subscribeUrl); // 无 connect |
| 114 | + |
| 115 | + await scriptDAO.save(script); |
| 116 | + await subscribeDAO.save(subscribe); |
| 117 | + |
| 118 | + const req = await gmApi.parseRequest({ uuid: "uuid-4", api: "test", runFlag: "", params: [] }); |
| 119 | + expect(req.script.metadata.connect).toEqual(["my-domain.com"]); |
| 120 | + }); |
| 121 | +}); |
0 commit comments