|
1 | 1 | import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
2 | 2 | import { ClientConfigSchema } from "@/client/config"; |
3 | | -import { HEADER_RIVET_TOKEN } from "@/common/actor-router-consts"; |
| 3 | +import { |
| 4 | + HEADER_RIVET_ACTOR, |
| 5 | + HEADER_RIVET_BYPASS_CONNECTABLE, |
| 6 | + HEADER_RIVET_TARGET, |
| 7 | + HEADER_RIVET_TOKEN, |
| 8 | +} from "@/common/actor-router-consts"; |
| 9 | +import { createClient } from "@/client/mod"; |
4 | 10 | import { RemoteEngineControlClient } from "@/engine-client/mod"; |
5 | 11 |
|
6 | 12 | describe.sequential("RemoteEngineControlClient public token usage", () => { |
@@ -76,6 +82,75 @@ describe.sequential("RemoteEngineControlClient public token usage", () => { |
76 | 82 | expect(actorRequest?.headers.get("x-user-header")).toBe("present"); |
77 | 83 | }); |
78 | 84 |
|
| 85 | + test("sets bypass connectable header for actor HTTP gateway requests", async () => { |
| 86 | + const fetchCalls: Request[] = []; |
| 87 | + const fetchMock = vi.fn(async (input: Request | URL | string) => { |
| 88 | + const request = normalizeRequest(input); |
| 89 | + fetchCalls.push(request); |
| 90 | + return new Response("ok"); |
| 91 | + }); |
| 92 | + vi.stubGlobal("fetch", fetchMock); |
| 93 | + |
| 94 | + const driver = new RemoteEngineControlClient( |
| 95 | + ClientConfigSchema.parse({ |
| 96 | + endpoint: "https://api.rivet.dev", |
| 97 | + disableMetadataLookup: true, |
| 98 | + }), |
| 99 | + ); |
| 100 | + |
| 101 | + const response = await driver.sendRequest( |
| 102 | + { directId: "actor-http-bypass" }, |
| 103 | + new Request("http://actor/request/bypass"), |
| 104 | + { bypassConnectable: true }, |
| 105 | + ); |
| 106 | + |
| 107 | + expect(response.status).toBe(200); |
| 108 | + expect(fetchCalls).toHaveLength(1); |
| 109 | + |
| 110 | + const actorRequest = fetchCalls[0]; |
| 111 | + expect(actorRequest?.url).toBe("https://api.rivet.dev/request/bypass"); |
| 112 | + expect(actorRequest?.headers.get(HEADER_RIVET_TARGET)).toBe("actor"); |
| 113 | + expect(actorRequest?.headers.get(HEADER_RIVET_ACTOR)).toBe( |
| 114 | + "actor-http-bypass", |
| 115 | + ); |
| 116 | + expect(actorRequest?.headers.get(HEADER_RIVET_BYPASS_CONNECTABLE)).toBe( |
| 117 | + "1", |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + test("handle fetch forwards bypass connectable to browser request", async () => { |
| 122 | + const fetchCalls: Request[] = []; |
| 123 | + const fetchMock = vi.fn(async (input: Request | URL | string) => { |
| 124 | + const request = normalizeRequest(input); |
| 125 | + fetchCalls.push(request); |
| 126 | + return new Response("ok"); |
| 127 | + }); |
| 128 | + vi.stubGlobal("fetch", fetchMock); |
| 129 | + |
| 130 | + const client = createClient({ |
| 131 | + endpoint: "https://api.rivet.dev", |
| 132 | + disableMetadataLookup: true, |
| 133 | + }); |
| 134 | + const handle = client.getForId("mockAgenticLoop", "actor-http-bypass"); |
| 135 | + |
| 136 | + const response = await handle.fetch("/bypass", { |
| 137 | + gateway: { bypassConnectable: true }, |
| 138 | + }); |
| 139 | + |
| 140 | + expect(response.status).toBe(200); |
| 141 | + expect(fetchCalls).toHaveLength(1); |
| 142 | + |
| 143 | + const actorRequest = fetchCalls[0]; |
| 144 | + expect(actorRequest?.url).toBe("https://api.rivet.dev/request/bypass"); |
| 145 | + expect(actorRequest?.headers.get(HEADER_RIVET_TARGET)).toBe("actor"); |
| 146 | + expect(actorRequest?.headers.get(HEADER_RIVET_ACTOR)).toBe( |
| 147 | + "actor-http-bypass", |
| 148 | + ); |
| 149 | + expect(actorRequest?.headers.get(HEADER_RIVET_BYPASS_CONNECTABLE)).toBe( |
| 150 | + "1", |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
79 | 154 | test("uses metadata clientToken for actor websocket gateway requests", async () => { |
80 | 155 | const fetchMock = vi.fn(async (input: Request | URL | string) => { |
81 | 156 | const request = normalizeRequest(input); |
|
0 commit comments