|
| 1 | +import { expect, test, vi } from "vitest"; |
| 2 | +import { Context } from "@deepseek-ai/cordis"; |
| 3 | +import { AgentSecurityService, type SecurityHost } from "./security"; |
| 4 | + |
| 5 | +test("plugin scope revokes a late native registration and cannot leave an enabled provider", async () => { |
| 6 | + const ctx = new Context(); |
| 7 | + let complete!: (v: { lease: string }) => void; |
| 8 | + const pending = new Promise<{ lease: string }>((r) => { |
| 9 | + complete = r; |
| 10 | + }); |
| 11 | + const requests: Record<string, unknown>[] = []; |
| 12 | + const host: SecurityHost = async <T>(request: Record<string, unknown>) => { |
| 13 | + requests.push(request); |
| 14 | + return (request.kind === "register" ? await pending : {}) as T; |
| 15 | + }; |
| 16 | + new AgentSecurityService(ctx, host); |
| 17 | + let registration: Promise<unknown> | undefined; |
| 18 | + const plugin = ctx |
| 19 | + .extend({ pluginOwner: { id: "fixture.security", revision: "1" } }) |
| 20 | + .plugin({ |
| 21 | + inject: ["agentSecurity"], |
| 22 | + apply(scope: Context) { |
| 23 | + registration = scope.agentSecurity.register("/fixture/launcher"); |
| 24 | + void registration.catch(() => {}); |
| 25 | + }, |
| 26 | + }); |
| 27 | + await vi.waitFor(() => expect(requests).toHaveLength(1)); |
| 28 | + const disposal = plugin.dispose(); |
| 29 | + complete({ lease: "lease-one" }); |
| 30 | + await disposal; |
| 31 | + await expect(registration).rejects.toThrow("disabled"); |
| 32 | + expect(requests).toContainEqual({ |
| 33 | + kind: "unregister", |
| 34 | + provider: "fixture.security", |
| 35 | + lease: "lease-one", |
| 36 | + }); |
| 37 | + await ctx.fiber.dispose(); |
| 38 | +}); |
| 39 | + |
| 40 | +test("a retained service cannot register after its plugin scope is disposed", async () => { |
| 41 | + const ctx = new Context(); |
| 42 | + const host = vi.fn(async () => ({ lease: "unexpected" })); |
| 43 | + new AgentSecurityService(ctx, host as SecurityHost); |
| 44 | + let service: Context["agentSecurity"] | undefined; |
| 45 | + const plugin = ctx |
| 46 | + .extend({ pluginOwner: { id: "fixture.security", revision: "1" } }) |
| 47 | + .plugin({ |
| 48 | + inject: ["agentSecurity"], |
| 49 | + apply(scope: Context) { |
| 50 | + service = scope.agentSecurity; |
| 51 | + }, |
| 52 | + }); |
| 53 | + await vi.waitFor(() => expect(service).toBeDefined()); |
| 54 | + await plugin.dispose(); |
| 55 | + if (!service) throw new Error("Plugin service was not initialized"); |
| 56 | + await expect(service.register("/fixture/launcher")).rejects.toThrow( |
| 57 | + "inactive context", |
| 58 | + ); |
| 59 | + expect(host).not.toHaveBeenCalled(); |
| 60 | + await ctx.fiber.dispose(); |
| 61 | + expect(host).not.toHaveBeenCalled(); |
| 62 | +}); |
| 63 | + |
| 64 | +test("explicit and scope disposal unregister a successful registration only once", async () => { |
| 65 | + const ctx = new Context(); |
| 66 | + const requests: Record<string, unknown>[] = []; |
| 67 | + new AgentSecurityService(ctx, async <T>(request: Record<string, unknown>) => { |
| 68 | + requests.push(request); |
| 69 | + return { lease: "lease-one" } as T; |
| 70 | + }); |
| 71 | + let registration: |
| 72 | + | ReturnType<Context["agentSecurity"]["register"]> |
| 73 | + | undefined; |
| 74 | + const plugin = ctx |
| 75 | + .extend({ pluginOwner: { id: "fixture.security", revision: "1" } }) |
| 76 | + .plugin({ |
| 77 | + inject: ["agentSecurity"], |
| 78 | + apply(scope: Context) { |
| 79 | + registration = scope.agentSecurity.register("/fixture/launcher"); |
| 80 | + }, |
| 81 | + }); |
| 82 | + await vi.waitFor(() => expect(registration).toBeDefined()); |
| 83 | + if (!registration) throw new Error("Plugin registration was not started"); |
| 84 | + const provider = await registration; |
| 85 | + await provider.dispose(); |
| 86 | + await provider.dispose(); |
| 87 | + await plugin.dispose(); |
| 88 | + await ctx.fiber.dispose(); |
| 89 | + expect(requests).toEqual([ |
| 90 | + { |
| 91 | + kind: "register", |
| 92 | + provider: "fixture.security", |
| 93 | + executable: "/fixture/launcher", |
| 94 | + }, |
| 95 | + { kind: "unregister", provider: "fixture.security", lease: "lease-one" }, |
| 96 | + ]); |
| 97 | +}); |
| 98 | + |
| 99 | +test("failed registration does not unregister a provider it never acquired", async () => { |
| 100 | + const ctx = new Context(); |
| 101 | + const host = vi.fn(async () => { |
| 102 | + throw new Error("launcher unavailable"); |
| 103 | + }); |
| 104 | + new AgentSecurityService(ctx, host); |
| 105 | + let registration: Promise<unknown> | undefined; |
| 106 | + const plugin = ctx |
| 107 | + .extend({ pluginOwner: { id: "fixture.security", revision: "1" } }) |
| 108 | + .plugin({ |
| 109 | + inject: ["agentSecurity"], |
| 110 | + apply(scope: Context) { |
| 111 | + registration = scope.agentSecurity.register("/fixture/launcher"); |
| 112 | + void registration.catch(() => {}); |
| 113 | + }, |
| 114 | + }); |
| 115 | + await vi.waitFor(() => expect(registration).toBeDefined()); |
| 116 | + await expect(registration).rejects.toThrow("launcher unavailable"); |
| 117 | + await plugin.dispose(); |
| 118 | + await ctx.fiber.dispose(); |
| 119 | + expect(host).toHaveBeenCalledTimes(1); |
| 120 | +}); |
0 commit comments