ai-query对接新版freesun-agent接口的分支
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

90 lines
2.5 KiB

import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
const root = path.resolve(import.meta.dirname, "..");
const sourcePath = path.join(root, "client", "src", "utils", "authCompat.js");
const source = fs.readFileSync(sourcePath, "utf8");
function createStorage() {
const values = new Map();
return {
get length() {
return values.size;
},
getItem(key) {
return values.has(key) ? values.get(key) : null;
},
setItem(key, value) {
values.set(key, String(value));
},
removeItem(key) {
values.delete(key);
},
key(index) {
return Array.from(values.keys())[index] ?? null;
},
};
}
function loadAuthCompat(windowLike) {
const transformed = source
.replace(/export const ([A-Za-z0-9_]+) =/g, "const $1 =")
.replace(/export \{ AUTH_STORAGE_KEYS \};/g, "")
+ "\nreturn { ensureDevMockAuthInfo, hasCompatibleAuthInfo, getCompatibleUserId, getCompatibleAuthUserInfo, AUTH_STORAGE_KEYS };";
return new Function("window", transformed)(windowLike);
}
function createWindow() {
return {
localStorage: createStorage(),
sessionStorage: createStorage(),
};
}
{
const windowLike = createWindow();
const auth = loadAuthCompat(windowLike);
const seeded = auth.ensureDevMockAuthInfo({ isDev: true, env: {} });
assert.equal(seeded, true);
assert.equal(auth.hasCompatibleAuthInfo(), true);
assert.equal(auth.getCompatibleUserId(), "local-dev-user");
assert.equal(windowLike.localStorage.getItem("aiTenderLoginIdentity"), "PHONE");
assert.equal(windowLike.sessionStorage.getItem("userid"), "local-dev-user");
}
{
const windowLike = createWindow();
windowLike.localStorage.setItem("userId", "real-user");
const auth = loadAuthCompat(windowLike);
const seeded = auth.ensureDevMockAuthInfo({ isDev: true, env: {} });
assert.equal(seeded, false);
assert.equal(auth.getCompatibleUserId(), "real-user");
}
{
const windowLike = createWindow();
const auth = loadAuthCompat(windowLike);
const seeded = auth.ensureDevMockAuthInfo({
isDev: true,
env: { FS_SUPER_AGENT_MOCK_LOGIN: "0" },
});
assert.equal(seeded, false);
assert.equal(auth.hasCompatibleAuthInfo(), false);
}
{
const windowLike = createWindow();
const auth = loadAuthCompat(windowLike);
const seeded = auth.ensureDevMockAuthInfo({ isDev: false, env: {} });
assert.equal(seeded, false);
assert.equal(auth.hasCompatibleAuthInfo(), false);
}