mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-11-13 12:13:42 +01:00
52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
import type { HttpRequest } from "@yaakapp-internal/models";
|
|
import { describe, expect, test, vi } from "vite-plus/test";
|
|
|
|
// resolvedModelName only reads plain model fields, but its module imports pull in the
|
|
// platform singleton, which needs a browser. Stub them out to keep this a pure unit test.
|
|
vi.mock("@yaakapp-internal/models", () => ({ foldersAtom: {} }));
|
|
vi.mock("./jotai", () => ({ jotaiStore: { get: () => [] } }));
|
|
|
|
const { resolvedModelName } = await import("./resolvedModelName");
|
|
|
|
function httpRequest(url: string): HttpRequest {
|
|
return { id: "rq_test", model: "http_request", name: "", url } as HttpRequest;
|
|
}
|
|
|
|
describe("resolvedModelName", () => {
|
|
test("returns the name when there is one", () => {
|
|
expect(
|
|
resolvedModelName({ ...httpRequest("https://example.com"), name: "My Request" }),
|
|
).toEqual("My Request");
|
|
});
|
|
|
|
test("replaces a simple tag with its contents", () => {
|
|
expect(resolvedModelName(httpRequest("${[ base_url ]}/users"))).toEqual("base_url/users");
|
|
});
|
|
|
|
test("replaces a tag containing a quoted argument with a space", () => {
|
|
expect(resolvedModelName(httpRequest("${[ fn(arg='my key') ]}/users"))).toEqual(
|
|
"fn(arg='my key')/users",
|
|
);
|
|
});
|
|
|
|
test("replaces each tag in a multi-tag string", () => {
|
|
expect(
|
|
resolvedModelName(httpRequest("${[ fn(arg='my key') ]}/a/${[ other(b='x y') ]}/b")),
|
|
).toEqual("fn(arg='my key')/a/other(b='x y')/b");
|
|
});
|
|
|
|
test("stops each tag at its first closing bracket", () => {
|
|
expect(resolvedModelName(httpRequest("${[ a ]}${[ b ]}"))).toEqual("ab");
|
|
});
|
|
|
|
test("strips the protocol", () => {
|
|
expect(resolvedModelName(httpRequest("https://example.com/${[ path ]}"))).toEqual(
|
|
"example.com/path",
|
|
);
|
|
});
|
|
|
|
test("falls back to a generic name when the url is empty", () => {
|
|
expect(resolvedModelName(httpRequest(""))).toEqual("HTTP Request");
|
|
});
|
|
});
|