- Create `releaseBootstrap.js` service to handle runtime configuration and remote frontend loading. - Integrate runtime-aware API URL resolution across components and services. - Update i18n phrases to clarify new domains for `ssl_domain_message`. - Enhance unit and E2E tests to validate runtime-based frontend loading and fallback behavior. - Refactor session-related API calls and incorporate runtime-configured base URLs.
138 lines
4.3 KiB
JavaScript
138 lines
4.3 KiB
JavaScript
// @vitest-environment jsdom
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
RELEASE_RUNTIME_GLOBAL_KEY,
|
|
bootstrapReleaseApp,
|
|
loadRemoteReleaseEntry,
|
|
runtimeApiUrl,
|
|
shouldLoadRemoteRelease,
|
|
} from "@/services/releaseBootstrap.js";
|
|
|
|
describe("release bootstrap", () => {
|
|
beforeEach(() => {
|
|
localStorage.clear();
|
|
delete window[RELEASE_RUNTIME_GLOBAL_KEY];
|
|
});
|
|
|
|
afterEach(() => {
|
|
localStorage.clear();
|
|
delete window[RELEASE_RUNTIME_GLOBAL_KEY];
|
|
document.head.innerHTML = "";
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("includes selected release channel in the runtime request", () => {
|
|
localStorage.setItem("release_channel_selected_slug", "Canary Preview!");
|
|
|
|
expect(runtimeApiUrl("https://api.truckwash.io")).toBe(
|
|
"https://api.truckwash.io/release/runtime?release_channel=canary-preview"
|
|
);
|
|
});
|
|
|
|
it("loads the local app for the default channel", async () => {
|
|
const loadLocalApp = vi.fn(async () => ({ local: true }));
|
|
const fetchFn = vi.fn(async () => ({
|
|
ok: true,
|
|
json: async () => ({
|
|
data: {
|
|
channel: { slug: "stable", default_channel: true },
|
|
availability: { configured: true },
|
|
},
|
|
}),
|
|
}));
|
|
|
|
await bootstrapReleaseApp({ loadLocalApp, fetchFn });
|
|
|
|
expect(loadLocalApp).toHaveBeenCalledTimes(1);
|
|
expect(window[RELEASE_RUNTIME_GLOBAL_KEY].channel.slug).toBe("stable");
|
|
});
|
|
|
|
it("loads a non-default release entry without changing the browser URL", async () => {
|
|
const runtime = {
|
|
channel: { slug: "canary", default_channel: false },
|
|
availability: { configured: true },
|
|
urls: {
|
|
frontend_base_url: "https://lb.truckwash.io/canary/frontend",
|
|
api_base_url: "https://lb.truckwash.io/canary/api",
|
|
},
|
|
};
|
|
const fetchFn = vi
|
|
.fn()
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({ data: runtime }),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({
|
|
entry: "assets/index-canary.js",
|
|
css: ["assets/index-canary.css"],
|
|
}),
|
|
});
|
|
const importModule = vi.fn(async (url) => ({ url }));
|
|
const originalHref = window.location.href;
|
|
|
|
await bootstrapReleaseApp({
|
|
loadLocalApp: vi.fn(),
|
|
fetchFn,
|
|
importModule,
|
|
documentRef: document,
|
|
});
|
|
|
|
expect(shouldLoadRemoteRelease(runtime)).toBe(true);
|
|
expect(importModule).toHaveBeenCalledWith("https://lb.truckwash.io/canary/frontend/assets/index-canary.js");
|
|
expect(document.querySelector("link")?.href).toBe(
|
|
"https://lb.truckwash.io/canary/frontend/assets/index-canary.css"
|
|
);
|
|
expect(window.location.href).toBe(originalHref);
|
|
});
|
|
|
|
it("falls back to the local app with unavailable runtime when the release entry fails", async () => {
|
|
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
const loadLocalApp = vi.fn(async () => ({ local: true }));
|
|
const runtime = {
|
|
channel: { slug: "canary", default_channel: false },
|
|
availability: { configured: true },
|
|
frontend_base_url: "https://lb.truckwash.io/canary/frontend",
|
|
};
|
|
const fetchFn = vi
|
|
.fn()
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({ data: runtime }),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ok: false,
|
|
status: 404,
|
|
});
|
|
|
|
await bootstrapReleaseApp({ loadLocalApp, fetchFn, importModule: vi.fn(), documentRef: document });
|
|
|
|
expect(loadLocalApp).toHaveBeenCalledTimes(1);
|
|
expect(window[RELEASE_RUNTIME_GLOBAL_KEY].availability).toMatchObject({
|
|
configured: false,
|
|
missing: ["frontend_entry"],
|
|
status: "unconfigured",
|
|
});
|
|
});
|
|
|
|
it("injects release entry CSS only once", async () => {
|
|
const fetchFn = vi.fn(async () => ({
|
|
ok: true,
|
|
json: async () => ({
|
|
entry: "assets/index-canary.js",
|
|
css: ["assets/index-canary.css"],
|
|
}),
|
|
}));
|
|
const importModule = vi.fn(async () => ({}));
|
|
const runtime = {
|
|
frontend_base_url: "https://lb.truckwash.io/canary/frontend",
|
|
};
|
|
|
|
await loadRemoteReleaseEntry({ runtime, fetchFn, importModule, documentRef: document });
|
|
await loadRemoteReleaseEntry({ runtime, fetchFn, importModule, documentRef: document });
|
|
|
|
expect(document.querySelectorAll("link[data-release-entry-css]")).toHaveLength(1);
|
|
});
|
|
});
|