Files
plugins/packages/sap-aicore/test/revision.test.js
T
caikn 0daf0c7421 sap-aicore 0.3.0: a new provider for SAP AI Core's GPT Responses and Claude Messages deployments (caikn, #651)
A new provider package, @magpie-community/opencode-sap-aicore-auth 0.3.0
(packages/sap-aicore). It signs in from a local SAP AI Core client-secret
credential file, discovers the account's running GPT (Responses) and Claude
(Messages, Bedrock) deployments per resource group, and sends requests only to
https://*.authentication.*.hana.ondemand.com (token) and
https://api.ai.*.hana.ondemand.com (inference). Image input is gated per model
on the catalog's inputTypes. Also: the root README row, a registry.json entry,
.DS_Store in .gitignore, and scripts/check.mjs accepts an empty static model
list for a package whose provider.models hook is a function with the same id
(npm and the loader are still required).

Reviewed at head b44b3e1b68889c2a12bcaa37ad5d546a398bdde0, with main 9bba5fed
merged in locally, on a Mac mini (macOS, Bun 1.4.2), no real SAP account:
- bun test packages/sap-aicore scripts: 351 pass, 0 fail.
- bun scripts/check.mjs: every package ok, sap-aicore included.
- The two tests that reached the network at e955194 now use inputs root()
  really rejects (/oauth, /v1), and a new test stubs fetch and checks that
  /oauth/token and /v2 are accepted and the token is posted to
  <authUrl>/oauth/token once. plugin.test.js fails on any unstubbed fetch.
- Whole package under a preload that records every non-loopback fetch, connect
  and DNS lookup: 0 attempts (the preload caught a deliberate fetch to
  example.invalid, so it does record).
- Broken by hand: putting the e955194 inputs back turns the test red through
  the new guard; making root() keep the given path turns the new
  /oauth/token + /v2 test red; letting root() accept any path turns 3 URL
  tests red.
- Not tried: a real SAP AI Core account (the author's run record covers one
  GPT and one Claude turn, plain and with a tool call; no image turn).

The first version goes out through the dist branch (npm doesn't have the name).
2026-11-04 22:19:43 +08:00

81 lines
3.2 KiB
JavaScript

import { describe, it, afterEach, jest, spyOn } from "bun:test"
import assert from "node:assert/strict"
import fs from "node:fs/promises"
import path from "node:path"
import os from "node:os"
import { pathToFileURL } from "node:url"
import { SAPAICorePlugin } from "../src/index.js"
const cleanups = []
afterEach(async () => {
for (const fn of cleanups.splice(0).reverse()) await fn()
jest.useRealTimers()
})
const version = JSON.parse(await fs.readFile(new URL("../package.json", import.meta.url), "utf8")).version
async function fixture() {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "aicore-revision-"))
cleanups.push(() => fs.rm(dir, { recursive: true, force: true }))
const root = path.join(dir, "plugin")
await fs.mkdir(root)
await fs.cp(new URL("../src", import.meta.url), path.join(root, "src"), { recursive: true })
await fs.writeFile(path.join(root, "package.json"), JSON.stringify({ type: "module", version }))
return { dir, root }
}
async function fakeGit(dir, command) {
const bin = path.join(dir, "bin")
await fs.mkdir(bin)
await fs.writeFile(path.join(bin, "git"), `#!/bin/sh\n${command}\n`, { mode: 0o755 })
const original = process.env.PATH
process.env.PATH = bin
cleanups.push(() => { process.env.PATH = original })
}
describe("package version at startup", () => {
it("does not execute Git even when a local package has Git metadata", async () => {
const { dir, root } = await fixture()
await fs.mkdir(path.join(root, ".git"))
const invoked = path.join(dir, "git-invoked")
await fakeGit(dir, `printf invoked > ${JSON.stringify(invoked)}\nprintf unexpected-git-revision`)
const { SAPAICorePlugin } = await import(pathToFileURL(path.join(root, "src/index.js")).href)
let finish
const recorded = new Promise(resolve => { finish = resolve })
const app = { log(options) { assert.equal(this, app); finish(options) } }
await SAPAICorePlugin({ client: { app } })
const record = await recorded
assert.ok(record.body.message.includes(version))
await assert.rejects(fs.access(invoked), { code: "ENOENT" })
}, 15000)
})
describe("startup host logging", () => {
for (const [name, log] of [
["synchronous", () => {}],
["throwing", () => { throw new Error("sync log failure") }],
["rejecting", () => Promise.reject(new Error("async log failure"))],
["pending", () => new Promise(() => {})],
]) {
it(`does not let a ${name} logger prevent initialization`, async () => {
const hooks = await SAPAICorePlugin({ client: { app: { log } } })
assert.equal(typeof hooks.config, "function")
await new Promise(resolve => setImmediate(resolve))
})
}
it("stays silent without a host logger", async () => {
const logged = []
const logSpy = spyOn(console, "log").mockImplementation((...args) => logged.push(args))
cleanups.push(() => logSpy.mockRestore())
const errorSpy = spyOn(console, "error").mockImplementation((...args) => logged.push(args))
cleanups.push(() => errorSpy.mockRestore())
for (const input of [undefined, {}, { client: {} }, { client: { app: {} } }]) {
assert.equal(typeof (await SAPAICorePlugin(input)).config, "function")
}
await new Promise(resolve => setImmediate(resolve))
assert.deepEqual(logged, [])
})
})