GeezerVison is showing github.com — test(server): harden HttpApi exercise coverage (#26425) · huer0625/opencode@0745162
File treeExpand file treeCollapse file tree Open diff view settings test/server/httpapi-exercise
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/workflows/test.yml +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 68 | 68 | env: | 69 | 69 | OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: ${{ runner.os == 'Windows' && 'true' || 'false' }} | 70 | 70 |
| | 71 | + - name: Run HttpApi exerciser gates | | 72 | + if: runner.os == 'Linux' | | 73 | + working-directory: packages/opencode | | 74 | + run: bun run test:httpapi | | 75 | + | 71 | 76 | - name: Publish unit reports | 72 | 77 | if: always() | 73 | 78 | uses: mikepenz/action-junit-report@v6 | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | "typecheck": "tsgo --noEmit", | 10 | 10 | "test": "bun test --timeout 30000", | 11 | 11 | "test:ci": "mkdir -p .artifacts/unit && bun test --timeout 30000 --reporter=junit --reporter-outfile=.artifacts/unit/junit.xml", | | 12 | + "test:httpapi": "bun run script/httpapi-exercise.ts --mode coverage --fail-on-missing --fail-on-skip && bun run script/httpapi-exercise.ts --mode auth --fail-on-missing --fail-on-skip", | 12 | 13 | "build": "bun run script/build.ts", | 13 | 14 | "fix-node-pty": "bun run script/fix-node-pty.ts", | 14 | 15 | "dev": "bun run --conditions=browser ./src/index.ts", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/server/httpapi-exercise/backend.ts +44-11Lines changed: 44 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,13 +23,31 @@ export function call( | 23 | 23 | ) | 24 | 24 | } | 25 | 25 |
| 26 | | -export function callAuthProbe(backend: Backend, scenario: ActiveScenario) { | 27 | | - return Effect.promise(async () => | 28 | | - capture( | 29 | | - await app(await runtime(), backend, { auth: { password: "secret" } }).request(toAuthProbeRequest(scenario)), | 30 | | - scenario.capture, | 31 | | - ), | 32 | | - ) | | 26 | +export function callAuthProbe( | | 27 | + backend: Backend, | | 28 | + scenario: ActiveScenario, | | 29 | + credentials: "missing" | "valid" = "missing", | | 30 | +) { | | 31 | + return Effect.promise(async () => { | | 32 | + const controller = new AbortController() | | 33 | + return Promise.race([ | | 34 | + Promise.resolve( | | 35 | + app(await runtime(), backend, { auth: { password: "secret" } }).request( | | 36 | + toAuthProbeRequest(scenario, credentials, controller.signal), | | 37 | + ), | | 38 | + ).then((response) => capture(response, scenario.capture)), | | 39 | + Bun.sleep(1_000).then(() => { | | 40 | + controller.abort("auth probe timed out") | | 41 | + return { | | 42 | + status: 0, | | 43 | + contentType: "", | | 44 | + text: "auth probe timed out", | | 45 | + body: undefined, | | 46 | + timedOut: true, | | 47 | + } | | 48 | + }), | | 49 | + ]) | | 50 | + }) | 33 | 51 | } | 34 | 52 |
| 35 | 53 | const appCache: Partial<Record<string, BackendApp>> = {} | @@ -78,14 +96,28 @@ function toRequest(scenario: ActiveScenario, ctx: SeededContext<unknown>) { | 78 | 96 | }) | 79 | 97 | } | 80 | 98 |
| 81 | | -function toAuthProbeRequest(scenario: ActiveScenario) { | 82 | | - return new Request(new URL(authProbePath(scenario.path), "http://localhost"), { | | 99 | +function toAuthProbeRequest(scenario: ActiveScenario, credentials: "missing" | "valid", signal: AbortSignal) { | | 100 | + const spec = scenario.authProbe ?? { | | 101 | + path: authProbePath(scenario.path), | | 102 | + body: scenario.method === "GET" ? undefined : {}, | | 103 | + } | | 104 | + const headers = { | | 105 | + ...(spec.body === undefined ? {} : { "content-type": "application/json" }), | | 106 | + ...spec.headers, | | 107 | + ...(credentials === "valid" ? { authorization: basic("opencode", "secret") } : {}), | | 108 | + } | | 109 | + return new Request(new URL(spec.path, "http://localhost"), { | 83 | 110 | method: scenario.method, | 84 | | - headers: scenario.method === "GET" ? undefined : { "content-type": "application/json" }, | 85 | | - body: scenario.method === "GET" ? undefined : JSON.stringify({}), | | 111 | + headers, | | 112 | + body: spec.body === undefined ? undefined : JSON.stringify(spec.body), | | 113 | + signal, | 86 | 114 | }) | 87 | 115 | } | 88 | 116 |
| | 117 | +function basic(username: string, password: string) { | | 118 | + return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}` | | 119 | +} | | 120 | + | 89 | 121 | function authProbePath(path: string) { | 90 | 122 | return path | 91 | 123 | .replace(/\{([^}]+)\}/g, (_match, key: string) => `auth_${key}`) | @@ -99,6 +131,7 @@ async function capture(response: Response, mode: CaptureMode): Promise<CallResul | 99 | 131 | contentType: response.headers.get("content-type") ?? "", | 100 | 132 | text, | 101 | 133 | body: parse(text), | | 134 | + timedOut: false, | 102 | 135 | } | 103 | 136 | } | 104 | 137 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/server/httpapi-exercise/dsl.ts +23-9Lines changed: 23 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,7 @@ import type { | 8 | 8 | Comparison, | 9 | 9 | Method, | 10 | 10 | ProjectOptions, | | 11 | + RequestSpec, | 11 | 12 | ScenarioContext, | 12 | 13 | SeededContext, | 13 | 14 | TodoScenario, | @@ -16,7 +17,7 @@ import type { | 16 | 17 | class ScenarioBuilder<S = undefined> { | 17 | 18 | private readonly state: BuilderState<S> | 18 | 19 |
| 19 | | - constructor(method: Method, path: string, name: string) { | | 20 | + constructor(method: Method, path: string, name: string, auth: AuthPolicy) { | 20 | 21 | this.state = { | 21 | 22 | method, | 22 | 23 | path, | @@ -25,10 +26,11 @@ class ScenarioBuilder<S = undefined> { | 25 | 26 | // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- The unseeded builder state is intentionally undefined until `.seeded(...)` narrows it. | 26 | 27 | seed: () => Effect.succeed(undefined as S), | 27 | 28 | request: (ctx) => ({ path, headers: ctx.headers() }), | | 29 | + authProbe: undefined, | 28 | 30 | capture: "full", | 29 | 31 | mutates: false, | 30 | 32 | reset: true, | 31 | | - auth: "protected", | | 33 | + auth, | 32 | 34 | } | 33 | 35 | } | 34 | 36 |
| @@ -48,6 +50,10 @@ class ScenarioBuilder<S = undefined> { | 48 | 50 | return this.clone({ request }) | 49 | 51 | } | 50 | 52 |
| | 53 | + probe(authProbe: RequestSpec) { | | 54 | + return this.clone({ authProbe }) | | 55 | + } | | 56 | + | 51 | 57 | mutating() { | 52 | 58 | return this.clone({ mutates: true }) | 53 | 59 | } | @@ -124,7 +130,7 @@ class ScenarioBuilder<S = undefined> { | 124 | 130 | } | 125 | 131 |
| 126 | 132 | private clone(next: Partial<BuilderState<S>>) { | 127 | | - const builder = new ScenarioBuilder<S>(this.state.method, this.state.path, this.state.name) | | 133 | + const builder = new ScenarioBuilder<S>(this.state.method, this.state.path, this.state.name, this.state.auth) | 128 | 134 | Object.assign(builder.state, this.state, next) | 129 | 135 | return builder | 130 | 136 | } | @@ -134,7 +140,7 @@ class ScenarioBuilder<S = undefined> { | 134 | 140 | * for `.at(...)` and assertions, giving stateful route tests type-safe setup. | 135 | 141 | */ | 136 | 142 | seeded<Next>(seed: (ctx: ScenarioContext) => Effect.Effect<Next>) { | 137 | | - const builder = new ScenarioBuilder<Next>(this.state.method, this.state.path, this.state.name) | | 143 | + const builder = new ScenarioBuilder<Next>(this.state.method, this.state.path, this.state.name, this.state.auth) | 138 | 144 | Object.assign(builder.state, this.state, { seed }) | 139 | 145 | return builder | 140 | 146 | } | @@ -151,6 +157,7 @@ class ScenarioBuilder<S = undefined> { | 151 | 157 | name: state.name, | 152 | 158 | project: state.project, | 153 | 159 | seed: state.seed, | | 160 | + authProbe: state.authProbe, | 154 | 161 | // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- `.seeded(...)` preserves the paired request/state type inside the builder. | 155 | 162 | request: (ctx, seeded) => state.request({ ...ctx, state: seeded as S }), | 156 | 163 | // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- `.seeded(...)` preserves the paired assertion/state type inside the builder. | @@ -164,12 +171,19 @@ class ScenarioBuilder<S = undefined> { | 164 | 171 | } | 165 | 172 | } | 166 | 173 |
| | 174 | +const routes = (auth: AuthPolicy) => ({ | | 175 | + get: (path: string, name: string) => new ScenarioBuilder("GET", path, name, auth), | | 176 | + post: (path: string, name: string) => new ScenarioBuilder("POST", path, name, auth), | | 177 | + put: (path: string, name: string) => new ScenarioBuilder("PUT", path, name, auth), | | 178 | + patch: (path: string, name: string) => new ScenarioBuilder("PATCH", path, name, auth), | | 179 | + delete: (path: string, name: string) => new ScenarioBuilder("DELETE", path, name, auth), | | 180 | +}) | | 181 | + | 167 | 182 | export const http = { | 168 | | - get: (path: string, name: string) => new ScenarioBuilder("GET", path, name), | 169 | | - post: (path: string, name: string) => new ScenarioBuilder("POST", path, name), | 170 | | - put: (path: string, name: string) => new ScenarioBuilder("PUT", path, name), | 171 | | - patch: (path: string, name: string) => new ScenarioBuilder("PATCH", path, name), | 172 | | - delete: (path: string, name: string) => new ScenarioBuilder("DELETE", path, name), | | 183 | + protected: routes("protected"), | | 184 | + public: routes("public"), | | 185 | + publicBypass: routes("public-bypass"), | | 186 | + ticketBypass: routes("ticket-bypass"), | 173 | 187 | } | 174 | 188 |
| 175 | 189 | export const pending = (method: Method, path: string, name: string, reason: string): TodoScenario => ({ | |
|