fix: restore portfolio loader and safe consent revocation
This commit is contained in:
1 parent
bff3e4d735
commit
a4e5e5c048
4 files changed
+315
-13
No files matched your search
@@ -0,0 +1,216 @@
|
||||
jest.mock("node:fs/promises", () => ({
|
||||
readFile: jest.fn(),
|
||||
}));
|
||||
|
||||
jest.mock("./project-health", () => ({
|
||||
...jest.requireActual("./project-health"),
|
||||
createProjectHealthSnapshot: jest.fn(),
|
||||
}));
|
||||
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { loadFeaturedProjects, portfolio } from "./portfolio-data";
|
||||
import { createProjectHealthSnapshot } from "./project-health";
|
||||
|
||||
const featuredIds = portfolio.projects
|
||||
.filter(({ featured }) => featured !== false)
|
||||
.map(({ id }) => id);
|
||||
|
||||
describe("loadFeaturedProjects", () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.mocked(readFile).mockResolvedValue(
|
||||
JSON.stringify({
|
||||
checkedAt: "2026-07-11T05:31:16.931Z",
|
||||
projects: {},
|
||||
}) as never,
|
||||
);
|
||||
jest
|
||||
.mocked(createProjectHealthSnapshot)
|
||||
.mockRejectedValue(
|
||||
new Error("remote refresh should not run during render"),
|
||||
);
|
||||
});
|
||||
|
||||
it("returns every featured project without network in production", async () => {
|
||||
const nodeEnv = jest.replaceProperty(process.env, "NODE_ENV", "production");
|
||||
jest.mocked(readFile).mockRejectedValueOnce(new Error("missing snapshot"));
|
||||
|
||||
try {
|
||||
const result = await loadFeaturedProjects();
|
||||
|
||||
expect(result.map(({ id }) => id)).toEqual(featuredIds);
|
||||
expect(
|
||||
result.find(({ id }) => id === "sev-modaintima")?.demoAvailability,
|
||||
).toBe("unknown");
|
||||
expect(createProjectHealthSnapshot).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
nodeEnv.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("does not refresh an old snapshot in production", async () => {
|
||||
const nodeEnv = jest.replaceProperty(process.env, "NODE_ENV", "production");
|
||||
|
||||
try {
|
||||
const result = await loadFeaturedProjects();
|
||||
|
||||
expect(result.map(({ id }) => id)).toEqual(featuredIds);
|
||||
expect(createProjectHealthSnapshot).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
nodeEnv.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("refreshes an old snapshot in development", async () => {
|
||||
const nodeEnv = jest.replaceProperty(
|
||||
process.env,
|
||||
"NODE_ENV",
|
||||
"development",
|
||||
);
|
||||
const checkedAt = new Date().toISOString();
|
||||
jest.mocked(createProjectHealthSnapshot).mockResolvedValue({
|
||||
checkedAt,
|
||||
projects: {
|
||||
"sev-modaintima": {
|
||||
status: 200,
|
||||
contentType: "text/html; charset=utf-8",
|
||||
checkedAt,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await loadFeaturedProjects();
|
||||
|
||||
expect(result.map(({ id }) => id)).toEqual(featuredIds);
|
||||
expect(
|
||||
result.find(({ id }) => id === "sev-modaintima")?.demoAvailability,
|
||||
).toBe("available");
|
||||
expect(createProjectHealthSnapshot).toHaveBeenCalledTimes(1);
|
||||
expect(createProjectHealthSnapshot).toHaveBeenCalledWith(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: "sev-modaintima",
|
||||
demoUrl: "https://sevmodaintima.com.br",
|
||||
}),
|
||||
]),
|
||||
);
|
||||
} finally {
|
||||
nodeEnv.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("does not refresh a fresh snapshot in development", async () => {
|
||||
const nodeEnv = jest.replaceProperty(
|
||||
process.env,
|
||||
"NODE_ENV",
|
||||
"development",
|
||||
);
|
||||
const checkedAt = new Date().toISOString();
|
||||
jest.mocked(readFile).mockResolvedValueOnce(
|
||||
JSON.stringify({
|
||||
checkedAt,
|
||||
projects: {
|
||||
"sev-modaintima": {
|
||||
status: 200,
|
||||
contentType: "text/html",
|
||||
checkedAt,
|
||||
},
|
||||
},
|
||||
}) as never,
|
||||
);
|
||||
|
||||
try {
|
||||
const result = await loadFeaturedProjects();
|
||||
|
||||
expect(
|
||||
result.find(({ id }) => id === "sev-modaintima")?.demoAvailability,
|
||||
).toBe("available");
|
||||
expect(createProjectHealthSnapshot).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
nodeEnv.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("refreshes a missing snapshot in development", async () => {
|
||||
const nodeEnv = jest.replaceProperty(
|
||||
process.env,
|
||||
"NODE_ENV",
|
||||
"development",
|
||||
);
|
||||
const checkedAt = new Date().toISOString();
|
||||
jest.mocked(readFile).mockRejectedValueOnce(new Error("missing snapshot"));
|
||||
jest.mocked(createProjectHealthSnapshot).mockResolvedValue({
|
||||
checkedAt,
|
||||
projects: {
|
||||
"sev-modaintima": {
|
||||
status: 200,
|
||||
contentType: "text/html",
|
||||
checkedAt,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await loadFeaturedProjects();
|
||||
|
||||
expect(result.map(({ id }) => id)).toEqual(featuredIds);
|
||||
expect(createProjectHealthSnapshot).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
nodeEnv.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves prior observations when refreshed checks fail", async () => {
|
||||
const nodeEnv = jest.replaceProperty(
|
||||
process.env,
|
||||
"NODE_ENV",
|
||||
"development",
|
||||
);
|
||||
const refreshedAt = new Date().toISOString();
|
||||
jest.mocked(readFile).mockResolvedValueOnce(
|
||||
JSON.stringify({
|
||||
checkedAt: "2020-01-01T00:00:00.000Z",
|
||||
projects: {
|
||||
"sev-modaintima": {
|
||||
status: 200,
|
||||
contentType: "text/html",
|
||||
checkedAt: "2020-01-01T00:00:00.000Z",
|
||||
},
|
||||
},
|
||||
}) as never,
|
||||
);
|
||||
jest.mocked(createProjectHealthSnapshot).mockResolvedValue({
|
||||
checkedAt: refreshedAt,
|
||||
projects: {
|
||||
"sev-modaintima": {
|
||||
status: null,
|
||||
contentType: null,
|
||||
checkedAt: refreshedAt,
|
||||
error: "network unavailable",
|
||||
},
|
||||
"twins-lab": {
|
||||
status: null,
|
||||
contentType: null,
|
||||
checkedAt: refreshedAt,
|
||||
error: "network unavailable",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await loadFeaturedProjects();
|
||||
const previous = result.find(({ id }) => id === "sev-modaintima");
|
||||
const unknown = result.find(({ id }) => id === "twins-lab");
|
||||
|
||||
expect(result.map(({ id }) => id)).toEqual(featuredIds);
|
||||
expect(previous?.demoAvailability).toBe("available");
|
||||
expect(previous?.demoCheckedAt).toBe("2020-01-01T00:00:00.000Z");
|
||||
expect(unknown?.demoAvailability).toBe("unknown");
|
||||
expect(unknown?.demoCheckedAt).toBeNull();
|
||||
expect(createProjectHealthSnapshot).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
nodeEnv.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user