|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { REDIRECT_URI } from "../lib/auth/auth.js"; |
| 6 | +import { transformRequestBody } from "../lib/request/request-transformer.js"; |
| 7 | +import { |
| 8 | + createDeactivatedWorkspaceError, |
| 9 | + createUsageRequestTimeoutError, |
| 10 | + DEACTIVATED_WORKSPACE_ERROR_CODE, |
| 11 | + isDeactivatedWorkspaceErrorMessage, |
| 12 | + OAUTH_CALLBACK_BIND_URL, |
| 13 | + OAUTH_CALLBACK_PATH, |
| 14 | + OAUTH_CALLBACK_PORT, |
| 15 | + USAGE_REQUEST_TIMEOUT_MESSAGE, |
| 16 | +} from "../lib/runtime-contracts.js"; |
| 17 | +import type { RequestBody, UserConfig } from "../lib/types.js"; |
| 18 | + |
| 19 | +const testDir = path.dirname(fileURLToPath(import.meta.url)); |
| 20 | + |
| 21 | +function readRepoFile(relativePath: string): string { |
| 22 | + return readFileSync(path.resolve(testDir, "..", relativePath), "utf8"); |
| 23 | +} |
| 24 | + |
| 25 | +describe("runtime contracts", () => { |
| 26 | + it("creates stable sentinel errors for workspace deactivation and usage timeouts", () => { |
| 27 | + const deactivatedWorkspaceError = createDeactivatedWorkspaceError(); |
| 28 | + const usageTimeoutError = createUsageRequestTimeoutError(); |
| 29 | + |
| 30 | + expect(deactivatedWorkspaceError.message).toBe(DEACTIVATED_WORKSPACE_ERROR_CODE); |
| 31 | + expect(isDeactivatedWorkspaceErrorMessage(deactivatedWorkspaceError.message)).toBe(true); |
| 32 | + expect(isDeactivatedWorkspaceErrorMessage("workspace-deactivated")).toBe(false); |
| 33 | + |
| 34 | + expect(usageTimeoutError.message).toBe(USAGE_REQUEST_TIMEOUT_MESSAGE); |
| 35 | + }); |
| 36 | + |
| 37 | + it("keeps the OAuth callback runtime values aligned", () => { |
| 38 | + expect(OAUTH_CALLBACK_PORT).toBe(1455); |
| 39 | + expect(OAUTH_CALLBACK_PATH).toBe("/auth/callback"); |
| 40 | + expect(OAUTH_CALLBACK_BIND_URL).toBe(`http://127.0.0.1:${OAUTH_CALLBACK_PORT}`); |
| 41 | + expect(REDIRECT_URI).toBe(`http://localhost:${OAUTH_CALLBACK_PORT}${OAUTH_CALLBACK_PATH}`); |
| 42 | + }); |
| 43 | + |
| 44 | + it("keeps the documented stateless request contract aligned with the runtime transform", async () => { |
| 45 | + const requestBody: RequestBody = { |
| 46 | + model: "gpt-5", |
| 47 | + input: [ |
| 48 | + { |
| 49 | + type: "message", |
| 50 | + role: "user", |
| 51 | + content: [{ type: "input_text", text: "quota ping" }], |
| 52 | + }, |
| 53 | + ], |
| 54 | + }; |
| 55 | + const userConfig: UserConfig = { global: {}, models: {} }; |
| 56 | + |
| 57 | + const transformedBody = await transformRequestBody(requestBody, "test instructions", userConfig); |
| 58 | + |
| 59 | + expect(transformedBody.store).toBe(false); |
| 60 | + expect(transformedBody.include).toContain("reasoning.encrypted_content"); |
| 61 | + |
| 62 | + const docsExpectations: Array<[string, string[]]> = [ |
| 63 | + [ |
| 64 | + "docs/getting-started.md", |
| 65 | + [ |
| 66 | + `http://127.0.0.1:${OAUTH_CALLBACK_PORT}${OAUTH_CALLBACK_PATH}`, |
| 67 | + "`store: false`", |
| 68 | + "`reasoning.encrypted_content`", |
| 69 | + ], |
| 70 | + ], |
| 71 | + [ |
| 72 | + "docs/configuration.md", |
| 73 | + [ |
| 74 | + "`reasoning.encrypted_content`", |
| 75 | + "store\": false", |
| 76 | + ], |
| 77 | + ], |
| 78 | + [ |
| 79 | + "docs/development/ARCHITECTURE.md", |
| 80 | + [ |
| 81 | + "`store: false`", |
| 82 | + "`reasoning.encrypted_content`", |
| 83 | + ], |
| 84 | + ], |
| 85 | + [ |
| 86 | + "docs/troubleshooting.md", |
| 87 | + [ |
| 88 | + "1455", |
| 89 | + "`reasoning.encrypted_content`", |
| 90 | + ], |
| 91 | + ], |
| 92 | + [ |
| 93 | + "docs/faq.md", |
| 94 | + [ |
| 95 | + "`1455`", |
| 96 | + ], |
| 97 | + ], |
| 98 | + ]; |
| 99 | + |
| 100 | + for (const [relativePath, fragments] of docsExpectations) { |
| 101 | + const fileContents = readRepoFile(relativePath); |
| 102 | + for (const fragment of fragments) { |
| 103 | + expect(fileContents).toContain(fragment); |
| 104 | + } |
| 105 | + } |
| 106 | + }); |
| 107 | +}); |
0 commit comments