@lks347/dsh-oauth 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +155 -0
- package/README.md +101 -2
- package/app.js +1045 -0
- package/auth.html +80 -0
- package/client.js +31 -0
- package/cordis.patch.yml +4 -0
- package/docs/CONNECTIONS.md +77 -0
- package/docs/LIVE-VALIDATION-1.0.1.md +100 -0
- package/docs/RELEASE-1.0.1.md +48 -0
- package/docs/RELEASE-1.0.2.md +8 -0
- package/docs/VALIDATION.md +59 -0
- package/docs/evidence/live-1.0.1.json +135 -0
- package/docs/evidence/release-1.0.1.json +38 -0
- package/index.html +43 -0
- package/package.json +64 -4
- package/providers.js +73 -0
- package/scripts/check-upstreams.mjs +27 -0
- package/scripts/link-peers.js +38 -0
- package/scripts/verify-browser.mjs +297 -0
- package/scripts/verify-host.mjs +431 -0
- package/scripts/verify-live.mjs +268 -0
- package/scripts/verify-package.mjs +168 -0
- package/scripts/verify-persistence.mjs +307 -0
- package/scripts/verify-subscriptions-ui.mjs +321 -0
- package/src/account.js +85 -0
- package/src/account.test.js +52 -0
- package/src/authorization.js +199 -0
- package/src/authorization.test.js +332 -0
- package/src/catalog.js +180 -0
- package/src/catalog.test.js +146 -0
- package/src/chat-recovery.js +110 -0
- package/src/chat-recovery.test.js +261 -0
- package/src/connection-runtime.js +129 -0
- package/src/connection-runtime.test.js +244 -0
- package/src/connections.js +70 -0
- package/src/grants.js +104 -0
- package/src/grants.test.js +340 -0
- package/src/index.js +676 -0
- package/src/local-imports.js +263 -0
- package/src/local-imports.test.js +201 -0
- package/src/media.js +152 -0
- package/src/models.js +610 -0
- package/src/models.test.js +503 -0
- package/src/profile-store.js +51 -0
- package/src/pulls.js +100 -0
- package/src/pulls.test.js +156 -0
- package/src/qwen.js +141 -0
- package/src/qwen.test.js +35 -0
- package/src/request-errors.js +50 -0
- package/src/request-errors.test.js +131 -0
- package/src/routes.js +47 -0
- package/src/routes.test.js +136 -0
- package/src/subscription-models.test.js +160 -0
- package/src/usage/aggregators.js +292 -0
- package/src/usage/aggregators.test.js +316 -0
- package/src/usage/core.js +68 -0
- package/src/usage/index.js +93 -0
- package/src/usage/official.js +339 -0
- package/src/usage/official.test.js +273 -0
- package/src/usage/service.test.js +89 -0
- package/src/xai-browser.js +272 -0
- package/src/xai-browser.test.js +256 -0
- package/src/xai-login.js +121 -0
- package/src/xai-login.test.js +259 -0
- package/styles.css +364 -0
- package/theme.js +118 -0
- package/upstreams.json +10 -0
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lks347/dsh-oauth",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" },
|
|
5
|
+
"repository": { "type": "git", "url": "git+https://github.com/lakeofsky347/dsh-oauth.git" },
|
|
6
|
+
"description": "Subscription connections, model capabilities and provider-reported usage for DeepSeek Harness.",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "node --test src/*.test.js src/usage/*.test.js",
|
|
10
|
+
"link:peers": "node scripts/link-peers.js",
|
|
11
|
+
"verify:host": "node scripts/verify-host.mjs",
|
|
12
|
+
"verify:persistence": "node scripts/verify-persistence.mjs",
|
|
13
|
+
"verify:browser": "node scripts/verify-browser.mjs",
|
|
14
|
+
"verify:subscriptions": "node scripts/verify-subscriptions-ui.mjs",
|
|
15
|
+
"verify:live": "node scripts/verify-live.mjs",
|
|
16
|
+
"verify:package": "node scripts/verify-package.mjs",
|
|
17
|
+
"check:upstreams": "node scripts/check-upstreams.mjs"
|
|
18
|
+
},
|
|
19
|
+
"main": "./src/index.js",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./src/index.js",
|
|
22
|
+
"./client": "./client.js",
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src",
|
|
27
|
+
"client.js",
|
|
28
|
+
"cordis.patch.yml",
|
|
29
|
+
"index.html",
|
|
30
|
+
"auth.html",
|
|
31
|
+
"app.js",
|
|
32
|
+
"providers.js",
|
|
33
|
+
"styles.css",
|
|
34
|
+
"theme.js",
|
|
35
|
+
"scripts",
|
|
36
|
+
"upstreams.json",
|
|
37
|
+
"README.md",
|
|
38
|
+
"README.en.md",
|
|
39
|
+
"docs"
|
|
40
|
+
],
|
|
41
|
+
"dsh": {
|
|
42
|
+
"bundle": {
|
|
43
|
+
"patch": "./cordis.patch.yml"
|
|
44
|
+
},
|
|
45
|
+
"client": {
|
|
46
|
+
"platform": "web",
|
|
47
|
+
"inject": [
|
|
48
|
+
"@deepseek-ai/dsh-client-ui-slots",
|
|
49
|
+
"@deepseek-ai/dsh-client-ui-settings",
|
|
50
|
+
"@deepseek-ai/dsh-client-ui-settings-general"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@earendil-works/pi-ai": ">=0.87.1 <2.0.0",
|
|
56
|
+
"@deepseek-ai/dsh": ">=0.2.0-rc.2 <0.3.0-0",
|
|
57
|
+
"@deepseek-ai/dsh-authorization": ">=0.2.0-rc.2 <0.3.0-0",
|
|
58
|
+
"@deepseek-ai/dsh-credentials": ">=0.2.0-rc.2 <0.3.0-0",
|
|
59
|
+
"@deepseek-ai/dsh-tools": ">=0.2.0-rc.2 <0.3.0-0"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"dsh": ">=0.2.0-rc.2 <0.3.0-0",
|
|
63
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
64
|
+
},
|
|
65
|
+
"license": "MIT"
|
|
66
|
+
}
|
package/providers.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Catalog for the first panel. Status is filled in by the page. */
|
|
2
|
+
const PROVIDERS = [
|
|
3
|
+
{
|
|
4
|
+
id: "openai-codex",
|
|
5
|
+
name: "ChatGPT / Codex",
|
|
6
|
+
flow: "pkce",
|
|
7
|
+
cli: "dsh oauth login openai-codex",
|
|
8
|
+
docs: "https://github.com/openai/codex",
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
id: "anthropic",
|
|
12
|
+
name: "Claude Pro/Max",
|
|
13
|
+
flow: "pkce",
|
|
14
|
+
cli: "dsh oauth login anthropic",
|
|
15
|
+
docs: "https://platform.claude.com",
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
id: "xai",
|
|
19
|
+
name: "xAI Grok",
|
|
20
|
+
flow: "pkce",
|
|
21
|
+
cli: "",
|
|
22
|
+
docs: "https://docs.x.ai",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "github-copilot",
|
|
26
|
+
name: "GitHub Copilot",
|
|
27
|
+
flow: "device_code",
|
|
28
|
+
cli: "dsh oauth login github-copilot",
|
|
29
|
+
docs: "https://docs.github.com/copilot",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "kimi-coding",
|
|
33
|
+
name: "Kimi Code",
|
|
34
|
+
flow: "device_code",
|
|
35
|
+
cli: "dsh oauth login kimi-coding",
|
|
36
|
+
docs: "https://www.kimi.com/code",
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: "openrouter",
|
|
40
|
+
name: "OpenRouter",
|
|
41
|
+
flow: "pkce",
|
|
42
|
+
cli: "dsh oauth login openrouter",
|
|
43
|
+
docs: "https://openrouter.ai",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: "qwen",
|
|
47
|
+
name: "通义千问",
|
|
48
|
+
flow: "pkce",
|
|
49
|
+
cli: "",
|
|
50
|
+
docs: "https://chat.qwen.ai",
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
id: "deepseek-account",
|
|
54
|
+
name: "DeepSeek 官方账号",
|
|
55
|
+
flow: "browser",
|
|
56
|
+
pullable: false,
|
|
57
|
+
cli: "",
|
|
58
|
+
docs: "https://platform.deepseek.com",
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: "google-antigravity",
|
|
62
|
+
name: "Google Antigravity",
|
|
63
|
+
flow: "pkce",
|
|
64
|
+
cli: "dsh oauth login google-antigravity",
|
|
65
|
+
docs: "https://antigravity.google",
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
const FLOW_LABEL = {
|
|
70
|
+
pkce: "浏览器登录",
|
|
71
|
+
device_code: "设备码登录",
|
|
72
|
+
external: "外部 CLI",
|
|
73
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Read public release metadata. This command never installs, logs in, or changes a profile. */
|
|
2
|
+
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
7
|
+
const baseline = JSON.parse(await readFile(resolve(root, "upstreams.json"), "utf8"));
|
|
8
|
+
const checks = await Promise.all(Object.entries(baseline.packages).map(async ([name, expected]) => {
|
|
9
|
+
const url = `https://registry.npmjs.org/${encodeURIComponent(name)}`;
|
|
10
|
+
const response = await fetch(url, { signal: AbortSignal.timeout(20000) });
|
|
11
|
+
if (!response.ok) throw new Error(`${name}: npm registry HTTP ${response.status}`);
|
|
12
|
+
const body = await response.json();
|
|
13
|
+
const tags = body["dist-tags"];
|
|
14
|
+
const changed = Object.entries(expected).filter(([tag, version]) => tags[tag] !== version)
|
|
15
|
+
.map(([tag, previous]) => ({ tag, previous, current: tags[tag] ?? null }));
|
|
16
|
+
return { name, url, tags, changed };
|
|
17
|
+
}));
|
|
18
|
+
const report = { checkedAt: new Date().toISOString(), baselineDate: baseline.checkedOn, checks, needsReview: checks.some((row) => row.changed.length > 0) };
|
|
19
|
+
const outFlag = process.argv.indexOf("--out");
|
|
20
|
+
if (outFlag >= 0) {
|
|
21
|
+
if (!process.argv[outFlag + 1]) throw new Error("--out 需要输出文件路径");
|
|
22
|
+
const destination = resolve(process.argv[outFlag + 1]);
|
|
23
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
24
|
+
await writeFile(destination, JSON.stringify(report, null, 2) + "\n");
|
|
25
|
+
}
|
|
26
|
+
console.log(JSON.stringify(report, null, 2));
|
|
27
|
+
if (report.needsReview) process.exitCode = 2;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Link development peers from an explicit DSH installation. */
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, rmSync, symlinkSync } from "node:fs";
|
|
3
|
+
import { createRequire, findPackageJSON } from "node:module";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { dirname, join, resolve } from "node:path";
|
|
6
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
7
|
+
|
|
8
|
+
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
9
|
+
const fromFlag = process.argv.indexOf("--from");
|
|
10
|
+
const source = resolve(fromFlag >= 0 ? process.argv[fromFlag + 1] : join(process.env.DSH_HOME || join(homedir(), ".dsh"), "profiles"));
|
|
11
|
+
const req = createRequire(join(source, "package.json"));
|
|
12
|
+
const names = ["@deepseek-ai/dsh-authorization", "@deepseek-ai/dsh-credentials", "@deepseek-ai/dsh-tools"];
|
|
13
|
+
const targets = names.map((name) => {
|
|
14
|
+
const path = dirname(req.resolve(`${name}/package.json`));
|
|
15
|
+
const version = JSON.parse(readFileSync(join(path, "package.json"), "utf8")).version;
|
|
16
|
+
if (/^0\.1\./.test(version)) throw new Error(`${name}@${version} 太旧;请 --from 指向 DSH >=0.2.0-rc.2 的安装目录`);
|
|
17
|
+
return { name, path, version };
|
|
18
|
+
});
|
|
19
|
+
let piManifest;
|
|
20
|
+
try {
|
|
21
|
+
piManifest = findPackageJSON("@earendil-works/pi-ai", pathToFileURL(join(source, "package.json")));
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (!["MODULE_NOT_FOUND", "ERR_MODULE_NOT_FOUND"].includes(error.code)) throw error;
|
|
24
|
+
piManifest = findPackageJSON("@earendil-works/pi-ai", pathToFileURL(req.resolve("@deepseek-ai/dsh-llm-pi-ai")));
|
|
25
|
+
}
|
|
26
|
+
const piRoot = dirname(piManifest);
|
|
27
|
+
targets.push({ name: "@earendil-works/pi-ai", path: piRoot, version: JSON.parse(readFileSync(join(piRoot, "package.json"), "utf8")).version });
|
|
28
|
+
for (const { name } of targets) {
|
|
29
|
+
const destination = join(root, "node_modules", name);
|
|
30
|
+
if (existsSync(destination) && !lstatSync(destination).isSymbolicLink()) throw new Error(`保留现有安装:${destination}`);
|
|
31
|
+
}
|
|
32
|
+
for (const { name, path, version } of targets) {
|
|
33
|
+
const destination = join(root, "node_modules", name);
|
|
34
|
+
mkdirSync(dirname(destination), { recursive: true });
|
|
35
|
+
rmSync(destination, { force: true });
|
|
36
|
+
symlinkSync(realpathSync(path), destination, process.platform === "win32" ? "junction" : "dir");
|
|
37
|
+
console.log(`${name}@${version} <- ${path}`);
|
|
38
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/** Browser smoke with local mock endpoints only. No provider credentials or network are used. */
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { dirname, join, resolve } from "node:path";
|
|
8
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
9
|
+
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
let playwright;
|
|
12
|
+
try { playwright = require("playwright"); }
|
|
13
|
+
catch {
|
|
14
|
+
const bundled = join(homedir(), ".cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/playwright");
|
|
15
|
+
playwright = require(process.env.DSH_PLAYWRIGHT_MODULE || bundled);
|
|
16
|
+
}
|
|
17
|
+
const root = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
18
|
+
const outIndex = process.argv.indexOf("--out-dir");
|
|
19
|
+
if (outIndex >= 0 && (!process.argv[outIndex + 1] || process.argv[outIndex + 1].startsWith("--"))) {
|
|
20
|
+
throw new Error("--out-dir requires an output directory");
|
|
21
|
+
}
|
|
22
|
+
const artifactDirectory = outIndex >= 0 ? resolve(process.argv[outIndex + 1]) : join(root, "artifacts");
|
|
23
|
+
const artifact = join(artifactDirectory, "oauth-login-mock.png");
|
|
24
|
+
const xaiArtifact = join(artifactDirectory, "oauth-xai-browser-mock.png");
|
|
25
|
+
const reportArtifact = join(artifactDirectory, "browser-pkce.json");
|
|
26
|
+
const xaiBrowserUrl = "https://oauth.example.test/oauth2/authorize?state=mock";
|
|
27
|
+
const xaiDeviceUrl = "https://oauth.example.test/device/approve?user_code=XAI-DEMO-5678";
|
|
28
|
+
let mode = "manual";
|
|
29
|
+
let loggedIn = false;
|
|
30
|
+
let snapshot;
|
|
31
|
+
const requests = [];
|
|
32
|
+
const loginRequests = [];
|
|
33
|
+
const cancelRequests = [];
|
|
34
|
+
const xaiChecks = {};
|
|
35
|
+
const reset = () => {
|
|
36
|
+
loggedIn = false;
|
|
37
|
+
snapshot = {
|
|
38
|
+
attemptId: "mock-attempt", status: "pending", pending: true,
|
|
39
|
+
url: "https://oauth.example.test/authorize", code: "DEMO-1234",
|
|
40
|
+
message: "在提供商页面完成授权后,请粘贴回调授权码。",
|
|
41
|
+
prompt: { id: "manual-code", kind: "text", message: "粘贴授权码或回调 URL", placeholder: "code#state" },
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
reset();
|
|
45
|
+
const server = createServer(async (req, res) => {
|
|
46
|
+
try {
|
|
47
|
+
const url = new URL(req.url, "http://localhost");
|
|
48
|
+
requests.push(url.pathname + url.search);
|
|
49
|
+
const json = (body, status = 200) => {
|
|
50
|
+
res.writeHead(status, { "content-type": "application/json" });
|
|
51
|
+
res.end(JSON.stringify(body));
|
|
52
|
+
};
|
|
53
|
+
if (url.pathname === "/oauth/api/providers") {
|
|
54
|
+
if (mode === "unavailable") return json({ error: "mock service unavailable" }, 503);
|
|
55
|
+
if (mode === "xai") return json({ providers: [{ id: "xai", name: "xAI Grok", flow: "pkce", loggedIn, pullable: false, loginMethods: ["browser", "device"], defaultLoginMethod: "browser" }] });
|
|
56
|
+
if (mode === "xai-legacy") return json({ providers: [{ id: "xai", name: "xAI Grok", flow: "device_code", loggedIn, pullable: false }] });
|
|
57
|
+
return json({ providers: [{ id: "anthropic", name: "Claude Pro/Max", flow: "pkce", loggedIn, pullable: false, tokenPreview: loggedIn ? "mock…only" : "" }] });
|
|
58
|
+
}
|
|
59
|
+
if (url.pathname === "/oauth/api/login") {
|
|
60
|
+
const chunks = [];
|
|
61
|
+
for await (const chunk of req) chunks.push(chunk);
|
|
62
|
+
const body = JSON.parse(Buffer.concat(chunks).toString());
|
|
63
|
+
loginRequests.push(body);
|
|
64
|
+
if (mode === "xai") {
|
|
65
|
+
assert.equal(body.provider, "xai");
|
|
66
|
+
assert.ok(["browser", "device"].includes(body.method));
|
|
67
|
+
snapshot = {
|
|
68
|
+
attemptId: `mock-xai-${body.method}`, status: "pending", pending: true,
|
|
69
|
+
url: body.method === "device" ? xaiDeviceUrl : xaiBrowserUrl,
|
|
70
|
+
code: body.method === "device" ? "XAI-DEMO-5678" : "",
|
|
71
|
+
message: body.method === "device" ? "在提供商审批页输入设备码。" : "请在浏览器中完成 xAI 授权。",
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return mode === "failure" ? json({ error: "mock provider offline" }, 400) : json(snapshot);
|
|
75
|
+
}
|
|
76
|
+
if (url.pathname === "/oauth/api/login/status") return json(snapshot);
|
|
77
|
+
if (url.pathname === "/oauth/api/login/answer") {
|
|
78
|
+
const chunks = [];
|
|
79
|
+
for await (const chunk of req) chunks.push(chunk);
|
|
80
|
+
const body = JSON.parse(Buffer.concat(chunks).toString());
|
|
81
|
+
assert.deepEqual(body, { provider: "anthropic", attemptId: "mock-attempt", promptId: "manual-code", value: "mock-callback-code" });
|
|
82
|
+
loggedIn = true;
|
|
83
|
+
snapshot = { attemptId: "mock-attempt", status: "authorized", pending: false };
|
|
84
|
+
return json(snapshot);
|
|
85
|
+
}
|
|
86
|
+
if (url.pathname === "/oauth/api/login/cancel") {
|
|
87
|
+
const chunks = [];
|
|
88
|
+
for await (const chunk of req) chunks.push(chunk);
|
|
89
|
+
const body = JSON.parse(Buffer.concat(chunks).toString());
|
|
90
|
+
cancelRequests.push(body);
|
|
91
|
+
snapshot = { attemptId: snapshot.attemptId, status: "cancelled", pending: false };
|
|
92
|
+
return json(snapshot);
|
|
93
|
+
}
|
|
94
|
+
const filename = url.pathname.replace(/^\/oauth\/?|^\//, "") || "index.html";
|
|
95
|
+
if (!["index.html", "app.js", "auth.html", "styles.css", "providers.js", "theme.js"].includes(filename)) {
|
|
96
|
+
res.writeHead(404); res.end(); return;
|
|
97
|
+
}
|
|
98
|
+
const type = { html: "text/html", js: "application/javascript", css: "text/css" }[filename.split(".").at(-1)];
|
|
99
|
+
res.writeHead(200, { "content-type": `${type}; charset=utf-8` });
|
|
100
|
+
res.end(await readFile(join(root, filename)));
|
|
101
|
+
} catch (error) {
|
|
102
|
+
res.writeHead(500, { "content-type": "application/json" });
|
|
103
|
+
res.end(JSON.stringify({ error: error.message }));
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
107
|
+
const browser = await playwright.chromium.launch({ headless: true });
|
|
108
|
+
const context = await browser.newContext({ viewport: { width: 1280, height: 950 } });
|
|
109
|
+
const errors = [];
|
|
110
|
+
const blockedRequests = [];
|
|
111
|
+
context.on("page", (page) => page.on("pageerror", (error) => errors.push(error.message)));
|
|
112
|
+
await context.route("**/*", (route) => {
|
|
113
|
+
const url = new URL(route.request().url());
|
|
114
|
+
if (url.hostname === "oauth.example.test") return route.fulfill({ contentType: "text/html", body: "<h1>Mock provider only</h1>" });
|
|
115
|
+
if (url.hostname === "127.0.0.1" || url.protocol === "file:") return route.continue();
|
|
116
|
+
blockedRequests.push(route.request().url());
|
|
117
|
+
return route.abort();
|
|
118
|
+
});
|
|
119
|
+
const origin = `http://127.0.0.1:${server.address().port}`;
|
|
120
|
+
await context.grantPermissions(["clipboard-read", "clipboard-write"], { origin });
|
|
121
|
+
const completed = [];
|
|
122
|
+
try {
|
|
123
|
+
let page = await context.newPage();
|
|
124
|
+
await page.goto(`${origin}/oauth/`);
|
|
125
|
+
await page.getByRole("button", { name: "登录", exact: true }).click();
|
|
126
|
+
await page.getByLabel("粘贴授权码或回调 URL").fill("mock-callback-code");
|
|
127
|
+
await page.getByText("DEMO-1234", { exact: true }).waitFor();
|
|
128
|
+
await mkdir(dirname(artifact), { recursive: true });
|
|
129
|
+
await page.screenshot({ path: artifact, fullPage: true });
|
|
130
|
+
await page.getByRole("button", { name: "提交授权输入" }).click();
|
|
131
|
+
await page.getByText("已连接 1/1 个提供商。凭据由本机服务保存。", { exact: true }).waitFor();
|
|
132
|
+
await page.getByRole("button", { name: "断开连接" }).waitFor();
|
|
133
|
+
completed.push("manual prompt, device code and credential-confirmed success");
|
|
134
|
+
await page.close();
|
|
135
|
+
|
|
136
|
+
const loginStartsBeforeReopen = requests.filter((path) => path === "/oauth/api/login").length;
|
|
137
|
+
page = await context.newPage();
|
|
138
|
+
await page.goto(`${origin}/oauth/`);
|
|
139
|
+
await page.getByText("已连接 1/1 个提供商。凭据由本机服务保存。", { exact: true }).waitFor();
|
|
140
|
+
await page.getByRole("button", { name: "断开连接" }).waitFor();
|
|
141
|
+
assert.equal(await page.getByRole("button", { name: "登录", exact: true }).count(), 0);
|
|
142
|
+
assert.equal(requests.filter((path) => path === "/oauth/api/login").length, loginStartsBeforeReopen);
|
|
143
|
+
assert.equal(requests.includes("/oauth/api/logout"), false);
|
|
144
|
+
completed.push("reopening the panel restores server-confirmed login without starting OAuth or logging out");
|
|
145
|
+
await page.close();
|
|
146
|
+
|
|
147
|
+
reset();
|
|
148
|
+
page = await context.newPage();
|
|
149
|
+
await page.goto(`${origin}/oauth/`);
|
|
150
|
+
await page.getByRole("button", { name: "登录", exact: true }).click();
|
|
151
|
+
await page.getByLabel("粘贴授权码或回调 URL").waitFor();
|
|
152
|
+
await page.getByRole("button", { name: "取消登录", exact: true }).click();
|
|
153
|
+
await page.getByText("本次登录已取消。", { exact: true }).waitFor();
|
|
154
|
+
assert.equal(snapshot.status, "cancelled");
|
|
155
|
+
completed.push("explicit cancel follows server outcome");
|
|
156
|
+
await page.close();
|
|
157
|
+
|
|
158
|
+
mode = "failure"; reset();
|
|
159
|
+
page = await context.newPage();
|
|
160
|
+
await page.goto(`${origin}/oauth/`);
|
|
161
|
+
await page.getByRole("button", { name: "登录", exact: true }).click();
|
|
162
|
+
await page.getByText("mock provider offline", { exact: true }).waitFor();
|
|
163
|
+
assert.equal(loggedIn, false);
|
|
164
|
+
assert.ok(!requests.some((url) => url.includes("preview=1")));
|
|
165
|
+
completed.push("API failure never falls back to sample authorization");
|
|
166
|
+
await page.close();
|
|
167
|
+
|
|
168
|
+
mode = "unavailable";
|
|
169
|
+
page = await context.newPage();
|
|
170
|
+
await page.goto(`${origin}/oauth/`);
|
|
171
|
+
await page.getByText(/本机 OAuth 服务不可用/).waitFor();
|
|
172
|
+
assert.equal(await page.getByRole("button", { name: "登录", exact: true }).first().isDisabled(), true);
|
|
173
|
+
completed.push("unavailable service disables real login");
|
|
174
|
+
await page.close();
|
|
175
|
+
|
|
176
|
+
mode = "manual";
|
|
177
|
+
page = await context.newPage();
|
|
178
|
+
await page.goto(pathToFileURL(join(root, "index.html")).href);
|
|
179
|
+
await page.getByText(/静态示例 ·/).waitFor();
|
|
180
|
+
const popupPromise = page.waitForEvent("popup");
|
|
181
|
+
await page.getByRole("button", { name: "演示登录", exact: true }).first().click();
|
|
182
|
+
const popup = await popupPromise;
|
|
183
|
+
await popup.getByRole("button", { name: "演示授权", exact: true }).click();
|
|
184
|
+
await page.getByText(/示例已连接,未登录真实账号/).waitFor();
|
|
185
|
+
completed.push("file preview labels sample state and supports sample popup");
|
|
186
|
+
await page.close();
|
|
187
|
+
|
|
188
|
+
mode = "xai"; reset();
|
|
189
|
+
page = await context.newPage();
|
|
190
|
+
await page.addInitScript(() => {
|
|
191
|
+
const open = window.open;
|
|
192
|
+
window.__oauthOpenCalls = [];
|
|
193
|
+
window.open = function (...args) {
|
|
194
|
+
window.__oauthOpenCalls.push(args);
|
|
195
|
+
return open.apply(this, args);
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
await page.goto(`${origin}/oauth/`);
|
|
199
|
+
await page.getByText("xAI Grok", { exact: true }).waitFor();
|
|
200
|
+
await page.getByRole("button", { name: "设备码备用", exact: true }).waitFor();
|
|
201
|
+
const browserPopupPromise = page.waitForEvent("popup");
|
|
202
|
+
await page.getByRole("button", { name: "浏览器登录", exact: true }).click();
|
|
203
|
+
const browserPopup = await browserPopupPromise;
|
|
204
|
+
await browserPopup.waitForURL(xaiBrowserUrl);
|
|
205
|
+
assert.deepEqual(loginRequests.at(-1), { provider: "xai", method: "browser" });
|
|
206
|
+
const browserOpenCalls = await page.evaluate(() => window.__oauthOpenCalls);
|
|
207
|
+
assert.equal(browserOpenCalls.length, 1);
|
|
208
|
+
assert.equal(browserOpenCalls[0].length, 2, "xAI opens a normal tab without a popup feature string");
|
|
209
|
+
assert.equal(browserOpenCalls[0][1], "_blank");
|
|
210
|
+
assert.equal(new URL(browserOpenCalls[0][0]).pathname, "/oauth/auth.html");
|
|
211
|
+
await page.getByText(/正在使用浏览器 PKCE 登录/).waitFor();
|
|
212
|
+
assert.equal(await page.getByText(/在提供商页面输入设备码/).count(), 0);
|
|
213
|
+
assert.equal(await page.locator("#authorization-answer").count(), 0);
|
|
214
|
+
assert.equal(await page.getByRole("link", { name: "打开提供商授权页", exact: true }).getAttribute("href"), xaiBrowserUrl);
|
|
215
|
+
xaiChecks.browser = { request: loginRequests.at(-1), url: browserPopup.url(), windowOpenArguments: browserOpenCalls[0], deviceCodeVisible: false, manualCodePromptVisible: false };
|
|
216
|
+
completed.push("xAI defaults to browser PKCE, sends the browser method, opens a normal tab and displays no device code");
|
|
217
|
+
|
|
218
|
+
await page.bringToFront();
|
|
219
|
+
await page.getByRole("button", { name: "复制授权链接", exact: true }).click();
|
|
220
|
+
await page.getByText("授权链接已复制,可在常用浏览器中打开", { exact: true }).waitFor();
|
|
221
|
+
const clipboard = await page.evaluate(() => navigator.clipboard.readText());
|
|
222
|
+
assert.equal(clipboard, xaiBrowserUrl);
|
|
223
|
+
xaiChecks.clipboard = { readBack: clipboard, matchesAuthorizationUrl: true };
|
|
224
|
+
await page.screenshot({ path: xaiArtifact, fullPage: true });
|
|
225
|
+
completed.push("xAI authorization link is copied through the real Chromium clipboard and read back exactly");
|
|
226
|
+
|
|
227
|
+
const browserClose = browserPopup.waitForEvent("close");
|
|
228
|
+
await page.getByRole("button", { name: "取消登录", exact: true }).click();
|
|
229
|
+
await page.getByText("本次登录已取消。", { exact: true }).waitFor();
|
|
230
|
+
await browserClose;
|
|
231
|
+
assert.deepEqual(cancelRequests.at(-1), { provider: "xai", attemptId: "mock-xai-browser" });
|
|
232
|
+
assert.equal(snapshot.status, "cancelled");
|
|
233
|
+
assert.equal(browserPopup.isClosed(), true);
|
|
234
|
+
assert.equal(await page.getByRole("button", { name: "复制授权链接", exact: true }).count(), 0);
|
|
235
|
+
assert.equal(await page.getByRole("button", { name: "浏览器登录", exact: true }).isEnabled(), true);
|
|
236
|
+
await page.getByRole("button", { name: "关闭", exact: true }).click();
|
|
237
|
+
assert.equal(await page.locator("#modal").isHidden(), true);
|
|
238
|
+
xaiChecks.cancel = { request: cancelRequests.at(-1), status: snapshot.status, authorizationTabClosed: true, modalClosed: true, loginEnabled: true };
|
|
239
|
+
completed.push("xAI cancel follows the server result, closes the authorization tab and clears the pending UI");
|
|
240
|
+
|
|
241
|
+
const devicePopupPromise = page.waitForEvent("popup");
|
|
242
|
+
await page.getByRole("button", { name: "设备码备用", exact: true }).click();
|
|
243
|
+
const devicePopup = await devicePopupPromise;
|
|
244
|
+
await devicePopup.waitForURL(xaiDeviceUrl);
|
|
245
|
+
assert.deepEqual(loginRequests.at(-1), { provider: "xai", method: "device" });
|
|
246
|
+
await page.getByText("XAI-DEMO-5678", { exact: true }).waitFor();
|
|
247
|
+
await page.getByText(/在提供商页面输入设备码/).waitFor();
|
|
248
|
+
await page.getByText(/正在使用设备码备用路线/).waitFor();
|
|
249
|
+
assert.equal(await page.getByText(/正在使用浏览器 PKCE 登录/).count(), 0);
|
|
250
|
+
assert.equal(await page.getByRole("link", { name: "打开提供商授权页", exact: true }).getAttribute("href"), xaiDeviceUrl);
|
|
251
|
+
const deviceOpenCalls = await page.evaluate(() => window.__oauthOpenCalls);
|
|
252
|
+
assert.equal(deviceOpenCalls.length, 2);
|
|
253
|
+
assert.equal(deviceOpenCalls[1].length, 2);
|
|
254
|
+
assert.equal(deviceOpenCalls[1][1], "_blank");
|
|
255
|
+
xaiChecks.device = { request: loginRequests.at(-1), url: devicePopup.url(), deviceCode: "XAI-DEMO-5678", windowOpenArguments: deviceOpenCalls[1] };
|
|
256
|
+
const deviceClose = devicePopup.waitForEvent("close");
|
|
257
|
+
await page.getByRole("button", { name: "取消登录", exact: true }).click();
|
|
258
|
+
await page.getByText("本次登录已取消。", { exact: true }).waitFor();
|
|
259
|
+
await deviceClose;
|
|
260
|
+
assert.deepEqual(cancelRequests.at(-1), { provider: "xai", attemptId: "mock-xai-device" });
|
|
261
|
+
await page.getByRole("button", { name: "关闭", exact: true }).click();
|
|
262
|
+
assert.equal(await page.locator("#modal").isHidden(), true);
|
|
263
|
+
completed.push("xAI explicit device fallback sends the device method, displays its approval URL and code, and can be cancelled");
|
|
264
|
+
await page.close();
|
|
265
|
+
|
|
266
|
+
mode = "xai-legacy"; reset();
|
|
267
|
+
page = await context.newPage();
|
|
268
|
+
await page.goto(`${origin}/oauth/`);
|
|
269
|
+
await page.getByText("当前服务尚未加载浏览器登录,请重新启动 DSH 以启用新版路线。", { exact: true }).waitFor();
|
|
270
|
+
const legacyBrowserButton = page.getByRole("button", { name: "浏览器登录", exact: true });
|
|
271
|
+
assert.equal(await legacyBrowserButton.isDisabled(), true);
|
|
272
|
+
assert.equal(await page.getByRole("button", { name: "设备码备用", exact: true }).isEnabled(), true);
|
|
273
|
+
const loginStartsBeforeLegacy = loginRequests.length;
|
|
274
|
+
const tabsBeforeLegacy = context.pages().length;
|
|
275
|
+
// Native clicks on the disabled control do nothing. A synthetic click also exercises openLogin's capability guard.
|
|
276
|
+
await legacyBrowserButton.evaluate((button) => button.click());
|
|
277
|
+
await legacyBrowserButton.dispatchEvent("click");
|
|
278
|
+
await page.getByText("请重新启动 DSH,以加载新版浏览器登录路线", { exact: true }).waitFor();
|
|
279
|
+
assert.equal(loginRequests.length, loginStartsBeforeLegacy);
|
|
280
|
+
assert.equal(context.pages().length, tabsBeforeLegacy);
|
|
281
|
+
assert.equal(await page.locator("#modal").isHidden(), true);
|
|
282
|
+
xaiChecks.legacy = { browserCapabilityDeclared: false, browserButtonDisabled: true, restartMessageVisible: true, deviceFallbackEnabled: true, loginRequestsStarted: 0, authorizationTabsOpened: 0 };
|
|
283
|
+
completed.push("legacy xAI services without browser capability disable browser login, show the DSH restart hint and cannot start authorization");
|
|
284
|
+
await page.close();
|
|
285
|
+
assert.deepEqual(errors, []);
|
|
286
|
+
assert.deepEqual(blockedRequests, []);
|
|
287
|
+
const report = {
|
|
288
|
+
completed, scenarioCount: completed.length, pageErrors: errors, blockedRequests,
|
|
289
|
+
screenshot: artifact, xaiScreenshot: xaiArtifact, xaiChecks,
|
|
290
|
+
evidence: "real Chromium with loopback/mock example endpoints only; no real OAuth, provider requests or credentials",
|
|
291
|
+
};
|
|
292
|
+
await writeFile(reportArtifact, `${JSON.stringify(report, null, 2)}\n`);
|
|
293
|
+
console.log(JSON.stringify({ ...report, report: reportArtifact }, null, 2));
|
|
294
|
+
} finally {
|
|
295
|
+
await browser.close();
|
|
296
|
+
await new Promise((resolve) => server.close(resolve));
|
|
297
|
+
}
|