Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+210
@@ -0,0 +1,210 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const serviceMocks = vi.hoisted(() => {
|
||||
class MockGlobalSearchInputError extends Error {
|
||||
readonly code: string;
|
||||
|
||||
constructor(code: string) {
|
||||
super(code);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
class MockGlobalSearchUnavailableError extends Error {}
|
||||
|
||||
return {
|
||||
MockGlobalSearchInputError,
|
||||
MockGlobalSearchUnavailableError,
|
||||
loadSearchContext: vi.fn(),
|
||||
recordCommittedSearch: vi.fn(),
|
||||
runGlobalSearch: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
const csrfMocks = vi.hoisted(() => ({ validate: vi.fn() }));
|
||||
|
||||
vi.mock("~/services/search/globalSearch.server", () => ({
|
||||
GlobalSearchInputError: serviceMocks.MockGlobalSearchInputError,
|
||||
GlobalSearchUnavailableError: serviceMocks.MockGlobalSearchUnavailableError,
|
||||
loadSearchContext: serviceMocks.loadSearchContext,
|
||||
recordCommittedSearch: serviceMocks.recordCommittedSearch,
|
||||
runGlobalSearch: serviceMocks.runGlobalSearch,
|
||||
}));
|
||||
vi.mock("~/utils/session.server", () => ({ validateCSRFToken: csrfMocks.validate }));
|
||||
|
||||
import { action, loader } from "./search.api";
|
||||
|
||||
const ctx = {
|
||||
tenantId: "tenant_1",
|
||||
tenantSlug: "acme",
|
||||
userId: "user_1",
|
||||
permissions: ["search.use"],
|
||||
};
|
||||
|
||||
describe("search resource route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
serviceMocks.loadSearchContext.mockResolvedValue(ctx);
|
||||
serviceMocks.runGlobalSearch.mockResolvedValue({
|
||||
query: "Ada",
|
||||
requestedEntityTypes: ["contact"],
|
||||
requestId: "request-7",
|
||||
requestKey: "Ada|contact|8",
|
||||
results: [],
|
||||
recentSearches: [],
|
||||
hiddenEntityTypes: [],
|
||||
latencyHint: "Search index updates can take up to 10 seconds.",
|
||||
});
|
||||
serviceMocks.recordCommittedSearch.mockResolvedValue({ recorded: true, requestKey: "Ada|contact,project|*" });
|
||||
csrfMocks.validate.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
it("keeps GET read-only and forwards query, filter, limit, and request identity", async () => {
|
||||
const request = new Request("https://app.example.test/acme/search/api?q=Ada&type=contact&limit=8&requestId=request-7");
|
||||
const response = await loader({ request, params: { tenant: "acme" }, context: {} });
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(serviceMocks.runGlobalSearch).toHaveBeenCalledWith({
|
||||
request,
|
||||
params: { tenant: "acme" },
|
||||
query: "Ada",
|
||||
limit: 8,
|
||||
entityTypes: ["contact"],
|
||||
requestId: "request-7",
|
||||
});
|
||||
expect(serviceMocks.recordCommittedSearch).not.toHaveBeenCalled();
|
||||
await expect(response.json()).resolves.toMatchObject({ latencyHint: "Search index updates can take up to 10 seconds." });
|
||||
});
|
||||
|
||||
it("maps invalid GET input to a bounded 400 response", async () => {
|
||||
serviceMocks.runGlobalSearch.mockRejectedValueOnce(new serviceMocks.MockGlobalSearchInputError("query_too_long"));
|
||||
const request = new Request("https://app.example.test/acme/search/api?q=too-long");
|
||||
|
||||
const response = await loader({ request, params: { tenant: "acme" }, context: {} });
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
await expect(response.json()).resolves.toEqual({ error: "query_too_long" });
|
||||
expect(serviceMocks.recordCommittedSearch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns a bounded service error that the Spotlight controller can render", async () => {
|
||||
serviceMocks.runGlobalSearch.mockRejectedValueOnce(new serviceMocks.MockGlobalSearchUnavailableError("database unavailable"));
|
||||
const request = new Request("https://app.example.test/acme/search/api?q=Ada");
|
||||
|
||||
const response = await loader({ request, params: { tenant: "acme" }, context: {} });
|
||||
|
||||
expect(response.status).toBe(503);
|
||||
await expect(response.json()).resolves.toEqual({ error: "search_unavailable" });
|
||||
expect(serviceMocks.recordCommittedSearch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("records only an explicit committed result with the authenticated context", async () => {
|
||||
const form = new URLSearchParams({
|
||||
intent: "recordRecent",
|
||||
query: "Ada",
|
||||
resultCount: "7",
|
||||
selectedEntityType: "contact",
|
||||
selectedEntityId: "kontakt_1",
|
||||
csrf: "csrf-token",
|
||||
});
|
||||
form.append("type", "contact");
|
||||
form.append("type", "project");
|
||||
const request = new Request("https://app.example.test/acme/search/api", { method: "POST", body: form });
|
||||
|
||||
const response = await action({ request, params: { tenant: "acme" }, context: {} });
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
await expect(response.json()).resolves.toEqual({ ok: true, recorded: true, requestKey: "Ada|contact,project|*" });
|
||||
expect(serviceMocks.loadSearchContext).toHaveBeenCalledWith({ request, params: { tenant: "acme" } });
|
||||
expect(csrfMocks.validate).toHaveBeenCalledWith(request, { formData: expect.anything() });
|
||||
expect(serviceMocks.recordCommittedSearch).toHaveBeenCalledWith({
|
||||
ctx,
|
||||
query: "Ada",
|
||||
entityTypes: ["contact", "project"],
|
||||
resultCount: "7",
|
||||
selectedEntityType: "contact",
|
||||
selectedEntityId: "kontakt_1",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns success with recorded=false when history persistence fails safely", async () => {
|
||||
serviceMocks.recordCommittedSearch.mockResolvedValueOnce({ recorded: false, requestKey: "Ada|contact|*" });
|
||||
const request = createCommitRequest();
|
||||
|
||||
const response = await action({ request, params: { tenant: "acme" }, context: {} });
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
await expect(response.json()).resolves.toEqual({ ok: true, recorded: false, requestKey: "Ada|contact|*" });
|
||||
});
|
||||
|
||||
it("rejects unsupported content types before parsing or CSRF validation", async () => {
|
||||
const request = new Request("https://app.example.test/acme/search/api", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ intent: "recordRecent" }),
|
||||
});
|
||||
|
||||
const response = await action({ request, params: { tenant: "acme" }, context: {} });
|
||||
|
||||
expect(response.status).toBe(415);
|
||||
await expect(response.json()).resolves.toEqual({ error: "unsupported_media_type" });
|
||||
expect(csrfMocks.validate).not.toHaveBeenCalled();
|
||||
expect(serviceMocks.recordCommittedSearch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects unknown intents after CSRF validation and before persistence", async () => {
|
||||
const request = new Request("https://app.example.test/acme/search/api", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ intent: "unknown", csrf: "csrf-token" }),
|
||||
});
|
||||
|
||||
const response = await action({ request, params: { tenant: "acme" }, context: {} });
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
await expect(response.json()).resolves.toEqual({ error: "invalid_intent" });
|
||||
expect(csrfMocks.validate).toHaveBeenCalledWith(request, { formData: expect.anything() });
|
||||
expect(serviceMocks.recordCommittedSearch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("enforces search access before CSRF validation and persistence", async () => {
|
||||
serviceMocks.loadSearchContext.mockRejectedValueOnce(new Error("permission denied"));
|
||||
const request = createCommitRequest();
|
||||
|
||||
await expect(action({ request, params: { tenant: "acme" }, context: {} })).rejects.toThrow("permission denied");
|
||||
expect(csrfMocks.validate).not.toHaveBeenCalled();
|
||||
expect(serviceMocks.recordCommittedSearch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("stops before persistence when CSRF validation fails", async () => {
|
||||
csrfMocks.validate.mockRejectedValueOnce(new Error("invalid csrf"));
|
||||
const request = createCommitRequest();
|
||||
|
||||
await expect(action({ request, params: { tenant: "acme" }, context: {} })).rejects.toThrow("invalid csrf");
|
||||
expect(serviceMocks.recordCommittedSearch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("maps invalid search input to a bounded 400 response", async () => {
|
||||
serviceMocks.recordCommittedSearch.mockRejectedValueOnce(new serviceMocks.MockGlobalSearchInputError("invalid_result_count"));
|
||||
const request = createCommitRequest();
|
||||
|
||||
const response = await action({ request, params: { tenant: "acme" }, context: {} });
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
await expect(response.json()).resolves.toEqual({ error: "invalid_result_count" });
|
||||
});
|
||||
});
|
||||
|
||||
function createCommitRequest() {
|
||||
return new Request("https://app.example.test/acme/search/api", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({
|
||||
intent: "recordRecent",
|
||||
query: "Ada",
|
||||
resultCount: "1",
|
||||
type: "contact",
|
||||
selectedEntityType: "contact",
|
||||
selectedEntityId: "kontakt_1",
|
||||
csrf: "csrf-token",
|
||||
}),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user