|
| 1 | +// Preserve module setup before modules that consume it. |
| 2 | +// oxfmt-ignore |
| 3 | +import { |
| 4 | + getPreparedModelRuntimeMocks, |
| 5 | + resetPreparedModelRuntimeHarness, |
| 6 | +} from "./prepared-model-runtime.test-harness.js"; |
| 7 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 8 | +import { createDeferred } from "../../test/helpers/promise.js"; |
| 9 | +import { |
| 10 | + getPreparedModelRuntimeAuthMaterializations, |
| 11 | + getPreparedModelRuntimeAuthStore, |
| 12 | + loadPreparedModelRuntimeAuth, |
| 13 | + setPreparedModelRuntimeAuthMaterializations, |
| 14 | +} from "./prepared-model-runtime-auth.js"; |
| 15 | +import { |
| 16 | + advancePreparedModelRuntimeConfig, |
| 17 | + loadPublishedGatewayReplyDispatchRuntime, |
| 18 | + prepareModelRuntimeSnapshot, |
| 19 | + refreshPreparedModelRuntimeSnapshots, |
| 20 | +} from "./prepared-model-runtime.js"; |
| 21 | + |
| 22 | +const mocks = getPreparedModelRuntimeMocks(); |
| 23 | + |
| 24 | +describe("prepared model runtime config stamps", () => { |
| 25 | + beforeEach(() => { |
| 26 | + resetPreparedModelRuntimeHarness(); |
| 27 | + mocks.configuredAgentIds = ["default"]; |
| 28 | + }); |
| 29 | + |
| 30 | + it("advances without rebuilding or mutating existing readers", async () => { |
| 31 | + const initialConfig = {}; |
| 32 | + const nextConfig = { gateway: { reload: { mode: "hot" as const } } }; |
| 33 | + await refreshPreparedModelRuntimeSnapshots(initialConfig, { gatewayLifecycle: true }); |
| 34 | + const input = { |
| 35 | + agentId: "default", |
| 36 | + agentDir: "/tmp/unused-agent", |
| 37 | + inheritedAuthDir: "/tmp/unused-agent", |
| 38 | + config: initialConfig, |
| 39 | + }; |
| 40 | + const existingReader = await prepareModelRuntimeSnapshot(input); |
| 41 | + const materializations = [ |
| 42 | + { |
| 43 | + provider: "test", |
| 44 | + modelId: "model", |
| 45 | + modelApi: "responses", |
| 46 | + modelBaseUrl: "https://example.test", |
| 47 | + requestTransportOverrides: "none" as const, |
| 48 | + authMode: "api_key", |
| 49 | + runtimeOwnerId: "test-owner", |
| 50 | + }, |
| 51 | + ]; |
| 52 | + setPreparedModelRuntimeAuthMaterializations(existingReader, materializations); |
| 53 | + const authStore = getPreparedModelRuntimeAuthStore(existingReader); |
| 54 | + const loadedAuth = await loadPreparedModelRuntimeAuth(existingReader, { providerIds: [] }); |
| 55 | + |
| 56 | + advancePreparedModelRuntimeConfig(nextConfig); |
| 57 | + |
| 58 | + const advanced = await prepareModelRuntimeSnapshot({ ...input, config: nextConfig }); |
| 59 | + expect(advanced).not.toBe(existingReader); |
| 60 | + expect(advanced.config).toBe(nextConfig); |
| 61 | + expect(existingReader.config).toBe(initialConfig); |
| 62 | + expect(getPreparedModelRuntimeAuthStore(advanced)).toBe(authStore); |
| 63 | + expect(getPreparedModelRuntimeAuthMaterializations(advanced)).toBe(materializations); |
| 64 | + await expect(loadPreparedModelRuntimeAuth(advanced, { providerIds: [] })).resolves.toEqual( |
| 65 | + loadedAuth, |
| 66 | + ); |
| 67 | + expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce(); |
| 68 | + await expect( |
| 69 | + loadPublishedGatewayReplyDispatchRuntime({ agentId: "default" }), |
| 70 | + ).resolves.toMatchObject({ config: nextConfig }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("resolves startup config inside the serialized publication", async () => { |
| 74 | + const initialConfig = {}; |
| 75 | + const nextConfig = { gateway: { reload: { mode: "hot" as const } } }; |
| 76 | + let currentConfig = initialConfig; |
| 77 | + |
| 78 | + const publication = refreshPreparedModelRuntimeSnapshots(() => currentConfig, { |
| 79 | + gatewayLifecycle: true, |
| 80 | + }); |
| 81 | + currentConfig = nextConfig; |
| 82 | + advancePreparedModelRuntimeConfig(nextConfig); |
| 83 | + await publication; |
| 84 | + |
| 85 | + await expect( |
| 86 | + prepareModelRuntimeSnapshot({ |
| 87 | + agentId: "default", |
| 88 | + agentDir: "/tmp/unused-agent", |
| 89 | + inheritedAuthDir: "/tmp/unused-agent", |
| 90 | + config: nextConfig, |
| 91 | + }), |
| 92 | + ).resolves.toMatchObject({ config: nextConfig }); |
| 93 | + await expect( |
| 94 | + loadPublishedGatewayReplyDispatchRuntime({ agentId: "default" }), |
| 95 | + ).resolves.toMatchObject({ config: nextConfig }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("drops an async startup config supplier superseded before it resolves", async () => { |
| 99 | + const staleConfig = {}; |
| 100 | + const nextConfig = { gateway: { reload: { mode: "hot" as const } } }; |
| 101 | + const supplierReady = createDeferred(); |
| 102 | + const stalePublication = refreshPreparedModelRuntimeSnapshots(async () => { |
| 103 | + await supplierReady.promise; |
| 104 | + return staleConfig; |
| 105 | + }); |
| 106 | + const nextPublication = refreshPreparedModelRuntimeSnapshots(nextConfig, { |
| 107 | + gatewayLifecycle: true, |
| 108 | + }); |
| 109 | + |
| 110 | + supplierReady.resolve(); |
| 111 | + await Promise.all([stalePublication, nextPublication]); |
| 112 | + |
| 113 | + expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce(); |
| 114 | + await expect( |
| 115 | + prepareModelRuntimeSnapshot({ |
| 116 | + agentId: "default", |
| 117 | + agentDir: "/tmp/unused-agent", |
| 118 | + inheritedAuthDir: "/tmp/unused-agent", |
| 119 | + config: nextConfig, |
| 120 | + }), |
| 121 | + ).resolves.toMatchObject({ config: nextConfig }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("drops a publication whose lifecycle claim is lost during async config resolution", async () => { |
| 125 | + const initialConfig = {}; |
| 126 | + const staleConfig = { gateway: { reload: { mode: "off" as const } } }; |
| 127 | + const nextConfig = { gateway: { reload: { mode: "hot" as const } } }; |
| 128 | + await refreshPreparedModelRuntimeSnapshots(initialConfig, { gatewayLifecycle: true }); |
| 129 | + const supplierStarted = createDeferred(); |
| 130 | + const releaseSupplier = createDeferred(); |
| 131 | + let claimCurrent = true; |
| 132 | + const stalePublication = refreshPreparedModelRuntimeSnapshots( |
| 133 | + async () => { |
| 134 | + supplierStarted.resolve(); |
| 135 | + await releaseSupplier.promise; |
| 136 | + return staleConfig; |
| 137 | + }, |
| 138 | + { |
| 139 | + gatewayLifecycle: true, |
| 140 | + isPublicationCurrent: () => claimCurrent, |
| 141 | + }, |
| 142 | + ); |
| 143 | + |
| 144 | + await supplierStarted.promise; |
| 145 | + claimCurrent = false; |
| 146 | + releaseSupplier.resolve(); |
| 147 | + await stalePublication; |
| 148 | + await refreshPreparedModelRuntimeSnapshots(nextConfig, { gatewayLifecycle: true }); |
| 149 | + |
| 150 | + expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2); |
| 151 | + await expect( |
| 152 | + prepareModelRuntimeSnapshot({ |
| 153 | + agentId: "default", |
| 154 | + agentDir: "/tmp/unused-agent", |
| 155 | + inheritedAuthDir: "/tmp/unused-agent", |
| 156 | + config: nextConfig, |
| 157 | + }), |
| 158 | + ).resolves.toMatchObject({ config: nextConfig }); |
| 159 | + await expect( |
| 160 | + loadPublishedGatewayReplyDispatchRuntime({ agentId: "default" }), |
| 161 | + ).resolves.toMatchObject({ config: nextConfig }); |
| 162 | + }); |
| 163 | + |
| 164 | + it("keeps an in-flight auth publication on the advanced stamp", async () => { |
| 165 | + const initialConfig = {}; |
| 166 | + const nextConfig = { gateway: { reload: { mode: "hot" as const } } }; |
| 167 | + await refreshPreparedModelRuntimeSnapshots(initialConfig, { gatewayLifecycle: true }); |
| 168 | + let finishAuthRefresh: (() => void) | undefined; |
| 169 | + mocks.ensureOpenClawModelsJson.mockImplementationOnce( |
| 170 | + async () => |
| 171 | + await new Promise<{ agentDir: string; wrote: false }>((resolve) => { |
| 172 | + finishAuthRefresh = () => resolve({ agentDir: "/tmp/unused-agent", wrote: false }); |
| 173 | + }), |
| 174 | + ); |
| 175 | + |
| 176 | + mocks.mutationListener?.({ affectsInheritedStores: true }); |
| 177 | + await vi.waitFor(() => expect(finishAuthRefresh).toBeDefined()); |
| 178 | + advancePreparedModelRuntimeConfig(nextConfig); |
| 179 | + finishAuthRefresh?.(); |
| 180 | + |
| 181 | + await expect( |
| 182 | + prepareModelRuntimeSnapshot({ |
| 183 | + agentId: "default", |
| 184 | + agentDir: "/tmp/unused-agent", |
| 185 | + inheritedAuthDir: "/tmp/unused-agent", |
| 186 | + config: nextConfig, |
| 187 | + }), |
| 188 | + ).resolves.toMatchObject({ config: nextConfig }); |
| 189 | + expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2); |
| 190 | + }); |
| 191 | +}); |
0 commit comments