diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index a227ea4..c916595 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -15,7 +15,6 @@ import { homedir } from "node:os" import { isAbsolute, join } from "node:path" import { gzipSync } from "node:zlib" -const APP_VERSION = "2.0.0" // the version WorkBuddy's sign-in page is opened with const POLL_MS = 1000 const SIGN_IN_MS = 5 * 60 * 1000 const EARLY_MS = 60 * 1000 // a token this close to its end is refreshed before a request @@ -1755,7 +1754,12 @@ async function browserSignIn(site) { if (!state?.state || !state?.authUrl) throw new Error(`${site.name} gave no sign-in page`) const url = new URL(state.authUrl) if (url.protocol !== "https:") throw new Error(`${site.name}'s sign-in page isn't https`) - url.searchParams.set("version", APP_VERSION) + // the desktop app's own version, as WorkBuddy desktop opens it + // (openAuthUrl: version = CLIENT_INFO_PLUGIN_VERSION, the app's version). + // The China build's page hides 企业微信 (WeCom) for a version under 5.8.0 + // (its signin-callback chunk: platform workbuddy and version >= "5.8.0"), + // so the 2.0.0 sent before offered no WeCom sign-in (shenyebuwu on Discord) + url.searchParams.set("version", site.app) url.searchParams.set("loginSessionId", hex()) return { url: url.toString(), diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index d5e01ad..64f3e0b 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.20", + "version": "0.2.21", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./opencode.mjs", diff --git a/packages/workbuddy/wecom.test.mjs b/packages/workbuddy/wecom.test.mjs new file mode 100644 index 0000000..7c67266 --- /dev/null +++ b/packages/workbuddy/wecom.test.mjs @@ -0,0 +1,42 @@ +// The sign-in page is opened with the desktop app's version, as WorkBuddy +// desktop opens it. WorkBuddy's China login page (www.workbuddy.cn/login) +// shows 企业微信 (WeCom) only to platform workbuddy at version 5.8.0 or +// later, so a lower version leaves a WeCom user no way to sign in +// (shenyebuwu on Discord). +import { afterEach, expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => (globalThis.fetch = real)) + +const opened = async (id) => { + globalThis.fetch = async (url) => { + const u = new URL(String(url)) + const data = u.pathname.endsWith("/auth/state") + ? { state: "s", authUrl: `https://www.workbuddy.cn/login?platform=${id}&state=s` } + : {} + return new Response(JSON.stringify({ code: 0, data })) + } + return new URL((await _internal.browserSignIn(_internal.SITES[id])).url) +} + +// the page's own check (signin-callback: en(version, "5.8.0")) +const atLeast = (v, min) => { + const a = v.split(".").map(Number), b = min.split(".").map(Number) + for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] > b[i] + return true +} + +for (const id of ["workbuddy", "workbuddy-ai"]) { + test(`${id}'s sign-in page is opened with the desktop app's version`, async () => { + const url = await opened(id) + expect(url.searchParams.get("version")).toBe(_internal.SITES[id].app) + expect(url.searchParams.get("platform")).toBe(id) + expect(url.searchParams.get("state")).toBe("s") + }) +} + +test("the China build's page is opened at a version that shows WeCom", async () => { + const url = await opened("workbuddy") + expect(atLeast(url.searchParams.get("version"), "5.8.0")).toBe(true) +})