diff --git a/packages/opendesign/index.mjs b/packages/opendesign/index.mjs index 8063a0b..23bb1e1 100644 --- a/packages/opendesign/index.mjs +++ b/packages/opendesign/index.mjs @@ -121,10 +121,16 @@ async function api(base, path, { key, body, method = "GET", signal } = {}) { // startDevice opens a device authorization: a code, the page that approves // it, and the secret the approval is polled with. -async function startDevice({ signal } = {}) { +// host is this computer's name, as `vela login` sends it (os.Hostname()); +// when it can't be read the CLI stops with "resolve hostname", and so does +// this, rather than send a name of its own. +async function startDevice({ signal, host = hostname } = {}) { + let name = "" + try { name = String(host() ?? "").trim() } catch {} + if (!name) throw new Error("Open Design sign-in: this computer's name couldn't be read, and the sign-in sends it") const r = await api(CONTROL, "/api/v1/cli/device-authorizations", { method: "POST", - body: { cliVersion: CLI_VERSION, host: hostname() || "magpie", profile: "prod" }, + body: { cliVersion: CLI_VERSION, host: name, profile: "prod" }, signal, }) const d = r.body @@ -499,4 +505,4 @@ export const OpenDesignAuthPlugin = async () => { // _internal is what the tests read through; magpie calls every exported // function as a plugin, so only the plugin itself is exported bare. -export const _internal = { authOf, allowances, controlKeyOf, credits, modelsOf, money, runtimeKeyOf, said, thePlan, usedPercent, waitForDevice, windowName, when, workspaceOf } +export const _internal = { authOf, allowances, controlKeyOf, credits, modelsOf, money, runtimeKeyOf, said, startDevice, thePlan, usedPercent, waitForDevice, windowName, when, workspaceOf } diff --git a/packages/opendesign/no-magpie.test.mjs b/packages/opendesign/no-magpie.test.mjs new file mode 100644 index 0000000..1d36bbd --- /dev/null +++ b/packages/opendesign/no-magpie.test.mjs @@ -0,0 +1,36 @@ +// The device authorization names this computer, never magpie (#357): +// `vela login` 0.1.7 sends {cliVersion, host: os.Hostname(), profile} and +// stops when the name can't be read, so the plugin does the same. +import { test, expect, afterEach } from "bun:test" +import { hostname } from "node:os" +import { _internal as I } from "./index.mjs" + +const realFetch = globalThis.fetch +afterEach(() => { globalThis.fetch = realFetch }) + +function record() { + const sent = [] + globalThis.fetch = async (input, init) => { + const headers = [...new Headers(init?.headers)].map(([k, v]) => `${k}: ${v}`).join("\n") + sent.push({ text: `${input}\n${headers}\n${String(init?.body ?? "")}`, body: JSON.parse(String(init?.body ?? "{}")) }) + return Response.json({ deviceId: "d1", deviceSecret: "s1", userCode: "ABC123", activationUrl: "https://open-design.ai/cloud/cli/activate?deviceId=d1", pollIntervalSeconds: 2 }, { status: 201 }) + } + return sent +} + +test("the device authorization sends this computer's name and nothing naming magpie", async () => { + expect(I.startDevice).toBeFunction() + const sent = record() + await I.startDevice() + expect(sent.length).toBe(1) + expect(sent[0].body.host).toBe(hostname()) + expect(sent[0].text.toLowerCase()).not.toContain("magpie") +}) + +test("a computer whose name can't be read stops, as the CLI does, and sends nothing", async () => { + for (const host of [() => "", () => { throw new Error("no name") }]) { + const sent = record() + await expect(I.startDevice({ host })).rejects.toThrow("computer's name") + expect(sent.length).toBe(0) + } +}) diff --git a/packages/opendesign/package.json b/packages/opendesign/package.json index e520900..a4e66ad 100644 --- a/packages/opendesign/package.json +++ b/packages/opendesign/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-opendesign-auth", - "version": "0.1.6", + "version": "0.1.7", "description": "Open Design Cloud (Design Plan) in OpenCode and magpie: the account's device sign-in, its models, and the plan's 5-hour and 7-day allowance", "type": "module", "main": "./index.mjs",