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.

92 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { describe, expect, it } from "vitest";
import {
isEmbedHostedShell,
parseEmbedEntryModule,
parseEmbedSsoParams,
} from "@/lib/embed/sso-params";
import { msLoggedInAddressQueryText } from "@/lib/embed/host-bridge";
describe("parseEmbedSsoParams", () => {
it("解析 login_type=api_key + api_key", () => {
const p = new URLSearchParams(
"login_type=api_key&api_key=chj_demo_token",
);
expect(parseEmbedSsoParams(p)).toEqual({
mode: "api_key",
apiKey: "chj_demo_token",
});
});
it("解析短别名 type=key + key=", () => {
const p = new URLSearchParams("type=api_key&key=demo-host-token");
expect(parseEmbedSsoParams(p)).toEqual({
mode: "api_key",
apiKey: "demo-host-token",
});
});
it("仅有 key 无 type 时默认 api_key", () => {
const p = new URLSearchParams("api_key=abc");
expect(parseEmbedSsoParams(p)).toEqual({ mode: "api_key", apiKey: "abc" });
});
it("解析账号密码", () => {
const p = new URLSearchParams(
"login_type=password&customer_id=CUST_001&password=secret",
);
expect(parseEmbedSsoParams(p)).toEqual({
mode: "password",
customerId: "CUST_001",
password: "secret",
});
});
it("缺字段 → none", () => {
expect(parseEmbedSsoParams(new URLSearchParams("login_type=api_key"))).toEqual(
{ mode: "none" },
);
});
});
describe("parseEmbedEntryModule", () => {
it("module=MS_LOGGED_IN", () => {
expect(
parseEmbedEntryModule(
new URLSearchParams("module=MS_LOGGED_IN&embed=1"),
),
).toBe("MS_LOGGED_IN");
});
it("entry=ms_logged_in", () => {
expect(
parseEmbedEntryModule(new URLSearchParams("entry=ms_logged_in")),
).toBe("MS_LOGGED_IN");
});
it("无效 → null", () => {
expect(parseEmbedEntryModule(new URLSearchParams("module=foo"))).toBeNull();
});
});
describe("msLoggedInAddressQueryText", () => {
it("优先 formatted_address,其次 street", () => {
expect(
msLoggedInAddressQueryText({
street: "A",
formatted_address: "整段地址原文",
}),
).toBe("整段地址原文");
expect(msLoggedInAddressQueryText({ street: "仅街道" })).toBe("仅街道");
});
});
describe("isEmbedHostedShell", () => {
it("embed=1 或带 SSO 凭证视为托管壳", () => {
expect(isEmbedHostedShell(new URLSearchParams("embed=1"))).toBe(true);
expect(
isEmbedHostedShell(new URLSearchParams("api_key=x")),
).toBe(true);
expect(isEmbedHostedShell(new URLSearchParams(""))).toBe(false);
});
});