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();
|
||||
}
|
||||
});
|
||||
});
|
||||
+55
-12
@@ -3,7 +3,9 @@ import { resolve } from "node:path";
|
||||
import portfolioData from "@/public/portfolio-data.json";
|
||||
import {
|
||||
createProjectHealthSnapshot,
|
||||
filterLiveProjects,
|
||||
type DemoAvailability,
|
||||
getDemoAvailability,
|
||||
PROJECT_HEALTH_MAX_AGE_MS,
|
||||
type ProjectHealthSnapshot,
|
||||
} from "./project-health";
|
||||
|
||||
@@ -13,16 +15,37 @@ export type PortfolioExperience = PortfolioData["experiences"][number];
|
||||
export type PortfolioSkillCategory =
|
||||
PortfolioData["skills"]["technical"][number];
|
||||
|
||||
export type PortfolioProjectView = PortfolioProject & {
|
||||
demoAvailability: DemoAvailability;
|
||||
demoCheckedAt: string | null;
|
||||
};
|
||||
|
||||
export const portfolio = portfolioData;
|
||||
|
||||
export function getLiveFeaturedProjects(
|
||||
export function getFeaturedProjects(
|
||||
snapshot: ProjectHealthSnapshot | null,
|
||||
): PortfolioProjectView[] {
|
||||
return portfolio.projects
|
||||
.filter(({ featured }) => featured !== false)
|
||||
.map((project) => {
|
||||
const check = snapshot?.projects[project.id];
|
||||
return {
|
||||
...project,
|
||||
demoAvailability: getDemoAvailability(project.demoUrl, check),
|
||||
demoCheckedAt: check?.checkedAt ?? null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function isSnapshotFresh(
|
||||
snapshot: ProjectHealthSnapshot | null,
|
||||
now = new Date(),
|
||||
) {
|
||||
return filterLiveProjects<PortfolioProject>(
|
||||
portfolio.projects,
|
||||
snapshot,
|
||||
now,
|
||||
if (!snapshot) return false;
|
||||
const checkedAt = Date.parse(snapshot.checkedAt);
|
||||
return (
|
||||
Number.isFinite(checkedAt) &&
|
||||
now.getTime() - checkedAt <= PROJECT_HEALTH_MAX_AGE_MS
|
||||
);
|
||||
}
|
||||
|
||||
@@ -35,16 +58,36 @@ export async function loadProjectHealthSnapshot(): Promise<ProjectHealthSnapshot
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadLiveFeaturedProjects() {
|
||||
const fromSnapshot = getLiveFeaturedProjects(await loadProjectHealthSnapshot());
|
||||
if (fromSnapshot.length > 0) return fromSnapshot;
|
||||
export async function loadFeaturedProjects(): Promise<PortfolioProjectView[]> {
|
||||
const snapshot = await loadProjectHealthSnapshot();
|
||||
if (process.env.NODE_ENV !== "development" || isSnapshotFresh(snapshot)) {
|
||||
return getFeaturedProjects(snapshot);
|
||||
}
|
||||
|
||||
const projectDemos = portfolio.projects.reduce<
|
||||
Array<{ id: string; demoUrl: string }>
|
||||
>((demos, project) => {
|
||||
if (project.demoUrl) demos.push({ id: project.id, demoUrl: project.demoUrl });
|
||||
if (project.demoUrl)
|
||||
demos.push({ id: project.id, demoUrl: project.demoUrl });
|
||||
return demos;
|
||||
}, []);
|
||||
const freshSnapshot = await createProjectHealthSnapshot(projectDemos);
|
||||
return getLiveFeaturedProjects(freshSnapshot);
|
||||
|
||||
try {
|
||||
const refreshed = await createProjectHealthSnapshot(projectDemos);
|
||||
const projects = { ...refreshed.projects };
|
||||
|
||||
for (const [id, check] of Object.entries(projects)) {
|
||||
if (!check.error && check.status !== null) continue;
|
||||
const previous = snapshot?.projects[id];
|
||||
if (previous && !previous.error && previous.status !== null) {
|
||||
projects[id] = previous;
|
||||
} else {
|
||||
delete projects[id];
|
||||
}
|
||||
}
|
||||
|
||||
return getFeaturedProjects({ ...refreshed, projects });
|
||||
} catch {
|
||||
return getFeaturedProjects(snapshot);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user