Migrate Cypress E2E from cy.intercept to MSW service worker
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,187 +4,18 @@
|
||||
// imacat.yang@dsp.im (imacat), 2026/03/05
|
||||
|
||||
/**
|
||||
* Sets up cy.intercept for all API endpoints using fixture files.
|
||||
* Call setupApiIntercepts() in beforeEach to mock the entire backend.
|
||||
* API mocking is now handled by MSW (Mock Service Worker).
|
||||
* This function is kept for backward compatibility but does nothing.
|
||||
*/
|
||||
export function setupApiIntercepts() {
|
||||
// Auth
|
||||
cy.intercept("POST", "/api/oauth/token", {
|
||||
fixture: "api/token.json",
|
||||
}).as("postToken");
|
||||
|
||||
// User account
|
||||
cy.intercept("GET", "/api/my-account", {
|
||||
fixture: "api/my-account.json",
|
||||
}).as("getMyAccount");
|
||||
|
||||
cy.intercept("PUT", "/api/my-account", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("putMyAccount");
|
||||
|
||||
// Files
|
||||
cy.intercept("GET", "/api/files", {
|
||||
fixture: "api/files.json",
|
||||
}).as("getFiles");
|
||||
|
||||
// Users (account management)
|
||||
cy.intercept("GET", "/api/users", {
|
||||
fixture: "api/users.json",
|
||||
}).as("getUsers");
|
||||
|
||||
cy.intercept("POST", "/api/users", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("postUser");
|
||||
|
||||
cy.intercept("DELETE", "/api/users/*", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("deleteUser");
|
||||
|
||||
cy.intercept("PUT", "/api/users/*", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("putUser");
|
||||
|
||||
// User detail (GET /api/users/:username)
|
||||
cy.intercept("GET", "/api/users/*", {
|
||||
fixture: "api/user-detail.json",
|
||||
}).as("getUserDetail");
|
||||
|
||||
// User roles
|
||||
cy.intercept("PUT", "/api/users/*/roles/*", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("putUserRole");
|
||||
|
||||
cy.intercept("DELETE", "/api/users/*/roles/*", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("deleteUserRole");
|
||||
|
||||
// Filter detail (for fetchFunnel when entering filter from Files)
|
||||
cy.intercept("GET", /\/api\/filters\/\d+$/, {
|
||||
statusCode: 200,
|
||||
body: { rules: [], log: { id: 1 }, name: "filtered-sample" },
|
||||
}).as("getFilterDetail");
|
||||
|
||||
// Discover (map data)
|
||||
cy.intercept("GET", "/api/logs/*/discover", {
|
||||
fixture: "api/discover.json",
|
||||
}).as("getDiscover");
|
||||
|
||||
cy.intercept("GET", "/api/filters/*/discover", {
|
||||
fixture: "api/discover.json",
|
||||
}).as("getFilterDiscover");
|
||||
|
||||
// Performance
|
||||
cy.intercept("GET", "/api/logs/*/performance", {
|
||||
fixture: "api/performance.json",
|
||||
}).as("getPerformance");
|
||||
|
||||
cy.intercept("GET", "/api/filters/*/performance", {
|
||||
fixture: "api/performance.json",
|
||||
}).as("getFilterPerformance");
|
||||
|
||||
// Traces
|
||||
cy.intercept("GET", "/api/logs/*/traces", {
|
||||
fixture: "api/traces.json",
|
||||
}).as("getTraces");
|
||||
|
||||
cy.intercept("GET", "/api/filters/*/traces", {
|
||||
fixture: "api/traces.json",
|
||||
}).as("getFilterTraces");
|
||||
|
||||
// Trace detail (must be after traces list intercepts)
|
||||
cy.intercept("GET", /\/api\/logs\/.*\/traces\/\d+/, {
|
||||
fixture: "api/trace-detail.json",
|
||||
}).as("getTraceDetail");
|
||||
|
||||
cy.intercept("GET", /\/api\/filters\/.*\/traces\/\d+/, {
|
||||
fixture: "api/trace-detail.json",
|
||||
}).as("getFilterTraceDetail");
|
||||
|
||||
// Temp filters
|
||||
cy.intercept("GET", "/api/temp-filters/*/discover", {
|
||||
fixture: "api/discover.json",
|
||||
}).as("getTempFilterDiscover");
|
||||
|
||||
cy.intercept("GET", "/api/temp-filters/*/traces", {
|
||||
fixture: "api/traces.json",
|
||||
}).as("getTempFilterTraces");
|
||||
|
||||
// Filter params
|
||||
cy.intercept("GET", "/api/filters/params*", {
|
||||
statusCode: 200,
|
||||
body: {},
|
||||
}).as("getFilterParams");
|
||||
|
||||
cy.intercept("GET", "/api/filters/has-result*", {
|
||||
statusCode: 200,
|
||||
body: false,
|
||||
}).as("getFilterHasResult");
|
||||
|
||||
// Conformance check params
|
||||
cy.intercept("GET", "/api/log-checks/params*", {
|
||||
fixture: "api/filter-params.json",
|
||||
}).as("getLogCheckParams");
|
||||
|
||||
cy.intercept("GET", "/api/filter-checks/params*", {
|
||||
fixture: "api/filter-params.json",
|
||||
}).as("getFilterCheckParams");
|
||||
|
||||
// Compare dashboard
|
||||
cy.intercept("GET", /\/api\/compare\?datasets=/, {
|
||||
fixture: "api/compare.json",
|
||||
}).as("getCompare");
|
||||
|
||||
// Dependents (for delete confirmation)
|
||||
cy.intercept("GET", "/api/logs/*/dependents", {
|
||||
statusCode: 200,
|
||||
body: [],
|
||||
}).as("getLogDependents");
|
||||
|
||||
cy.intercept("GET", "/api/filters/*/dependents", {
|
||||
statusCode: 200,
|
||||
body: [],
|
||||
}).as("getFilterDependents");
|
||||
|
||||
cy.intercept("GET", "/api/log-checks/*/dependents", {
|
||||
statusCode: 200,
|
||||
body: [],
|
||||
}).as("getLogCheckDependents");
|
||||
|
||||
cy.intercept("GET", "/api/filter-checks/*/dependents", {
|
||||
statusCode: 200,
|
||||
body: [],
|
||||
}).as("getFilterCheckDependents");
|
||||
|
||||
// Rename
|
||||
cy.intercept("PUT", "/api/logs/*/rename", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("renameLog");
|
||||
|
||||
cy.intercept("PUT", "/api/filters/*/rename", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("renameFilter");
|
||||
|
||||
// Deletion
|
||||
cy.intercept("DELETE", "/api/deletion/*", {
|
||||
statusCode: 200,
|
||||
body: { success: true },
|
||||
}).as("deleteDeletion");
|
||||
// MSW handles all API interception via service worker.
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the luciaToken cookie and isLuciaLoggedIn cookie to simulate
|
||||
* a logged-in state, then sets up all API intercepts.
|
||||
* a logged-in state. API interception is handled by MSW.
|
||||
*/
|
||||
export function loginWithFixtures() {
|
||||
setupApiIntercepts();
|
||||
cy.setCookie("luciaToken", "fake-access-token-for-testing");
|
||||
cy.setCookie("isLuciaLoggedIn", "true");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user