|
1 | 1 | import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { RpcStub as NativeRpcStub } from "cloudflare:workers"; |
2 | 3 | import { DEFAULT_ADMIN_CONFIG, serializeAdminConfig } from "../src/admin-config.js"; |
3 | 4 | import { OverseerDurableObject } from "../src/overseer.js"; |
4 | 5 |
|
@@ -87,3 +88,63 @@ describe("OverseerDurableObject.startHook", () => { |
87 | 88 | await expect(overseer.startHook(1)).rejects.toThrow("Hook has been deleted or disabled."); |
88 | 89 | }); |
89 | 90 | }); |
| 91 | + |
| 92 | +async function makeTargetOverseer(gadgetId?: number) { |
| 93 | + let controllerEnable = vi.fn(async (_initiator: object, _target: object) => {}); |
| 94 | + let record = { |
| 95 | + id: 4, |
| 96 | + actionId: 12, |
| 97 | + gatekeeperId: 1, |
| 98 | + gadgetId, |
| 99 | + controller: {enable: controllerEnable}, |
| 100 | + callback: {}, |
| 101 | + description: {title: "Incoming email", description: "Receives email"}, |
| 102 | + enabled: false, |
| 103 | + }; |
| 104 | + let overseer = { |
| 105 | + open: OverseerDurableObject.prototype.open, |
| 106 | + impl: { |
| 107 | + ownerId: "user-id", |
| 108 | + ensureAmbientCapsules: async () => {}, |
| 109 | + joinPresence: () => () => {}, |
| 110 | + users: { |
| 111 | + idFromString: (id: string) => id, |
| 112 | + get: () => ({ |
| 113 | + whoami: async () => ({id: "profile-id", name: "Test User"}), |
| 114 | + }), |
| 115 | + }, |
| 116 | + ctx: { |
| 117 | + id: {toString: () => "workspace-id"}, |
| 118 | + exports: {GatekeeperHookLoopback: ({props}: {props: object}) => props}, |
| 119 | + }, |
| 120 | + storage: { |
| 121 | + prohibitAllSharing: {get: () => false}, |
| 122 | + boundHooks: {get: () => record, put: vi.fn()}, |
| 123 | + actions: {get: () => undefined, put: vi.fn()}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } satisfies Pick<OverseerDurableObject, "open"> & {impl: object}; |
| 127 | + let notifyClosed = new NativeRpcStub<() => void>(() => {}); |
| 128 | + let client = await overseer.open("user-id", "profile-id", notifyClosed); |
| 129 | + return {client, controllerEnable}; |
| 130 | +} |
| 131 | + |
| 132 | +describe("hook target", () => { |
| 133 | + |
| 134 | + it("passes the workspace and gadget IDs to enable()", async () => { |
| 135 | + let {client, controllerEnable} = await makeTargetOverseer(17); |
| 136 | + |
| 137 | + await client.enableHook(4); |
| 138 | + |
| 139 | + expect(controllerEnable).toHaveBeenCalledTimes(1); |
| 140 | + expect(controllerEnable.mock.calls[0][1]).toEqual({workspaceId: "workspace-id", gadgetId: 17}); |
| 141 | + }); |
| 142 | + |
| 143 | + it("omits the gadget ID for a hook that is not pinned to one", async () => { |
| 144 | + let {client, controllerEnable} = await makeTargetOverseer(); |
| 145 | + |
| 146 | + await client.enableHook(4); |
| 147 | + |
| 148 | + expect(controllerEnable.mock.calls[0][1]).toEqual({workspaceId: "workspace-id"}); |
| 149 | + }); |
| 150 | +}); |
0 commit comments