Files
emdash-cms--emdash/packages/core/tests/unit/object-cache-comments-schema.test.ts
wehub-resource-sync b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:53 +08:00

169 lines
5.5 KiB
TypeScript

import type { Kysely } from "kysely";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("virtual:emdash/wait-until", () => ({ waitUntil: undefined }), { virtual: true });
vi.mock("../../src/loader.js", () => ({ getDb: vi.fn() }));
import { getComments } from "../../src/comments/query.js";
import { CommentReactionRepository } from "../../src/database/repositories/comment-reaction.js";
import { CommentRepository } from "../../src/database/repositories/comment.js";
import { ContentRepository } from "../../src/database/repositories/content.js";
import type { Database } from "../../src/database/types.js";
import { getDb } from "../../src/loader.js";
import {
__setObjectCacheBackendForTests,
type ObjectCacheBackend,
} from "../../src/object-cache/index.js";
import { invalidateUrlPatternCache } from "../../src/query.js";
import { getCollectionInfo } from "../../src/schema/query.js";
import { setupTestDatabaseWithCollections, teardownTestDatabase } from "../utils/test-db.js";
function memoryBackend(): ObjectCacheBackend {
const store = new Map<string, string>();
return {
get: (k) => Promise.resolve(store.get(k) ?? null),
set: (k, v) => {
store.set(k, v);
return Promise.resolve();
},
delete: (k) => {
store.delete(k);
return Promise.resolve();
},
};
}
const flush = () => new Promise((r) => setTimeout(r, 0));
describe("object cache: schema (getCollectionInfo)", () => {
let db: Kysely<Database>;
beforeEach(async () => {
db = await setupTestDatabaseWithCollections();
vi.mocked(getDb).mockResolvedValue(db);
__setObjectCacheBackendForTests(memoryBackend(), { revalidate: 60_000, defaultTtl: 3600 });
});
afterEach(async () => {
__setObjectCacheBackendForTests(null);
await teardownTestDatabase(db);
vi.restoreAllMocks();
});
it("serves the second read from KV, and busts on a schema change", async () => {
const first = await getCollectionInfo("post");
expect(first?.slug).toBe("post");
await flush();
// D1 down — a cached read must not need it.
vi.mocked(getDb).mockRejectedValue(new Error("D1 unavailable"));
const second = await getCollectionInfo("post");
expect(second?.slug).toBe("post");
// A schema change bumps the schema epoch → next read reloads (and now
// D1 is down, so it surfaces).
invalidateUrlPatternCache();
await flush();
await expect(getCollectionInfo("post")).rejects.toThrow(/D1 unavailable/);
});
});
describe("object cache: comments (getComments)", () => {
let db: Kysely<Database>;
let postId: string;
beforeEach(async () => {
db = await setupTestDatabaseWithCollections();
vi.mocked(getDb).mockResolvedValue(db);
__setObjectCacheBackendForTests(memoryBackend(), { revalidate: 60_000, defaultTtl: 3600 });
const post = await new ContentRepository(db).create({
type: "post",
slug: "p1",
data: { title: "P1" },
});
postId = post.id;
await new CommentRepository(db).create({
collection: "post",
contentId: postId,
authorName: "A",
authorEmail: "a@example.com",
body: "first!",
status: "approved",
});
await flush();
});
afterEach(async () => {
__setObjectCacheBackendForTests(null);
await teardownTestDatabase(db);
vi.restoreAllMocks();
});
it("serves the second read from KV, and busts when a comment is written", async () => {
const first = await getComments({ collection: "post", contentId: postId });
expect(first.total).toBe(1);
await flush();
vi.mocked(getDb).mockRejectedValue(new Error("D1 unavailable"));
const second = await getComments({ collection: "post", contentId: postId });
expect(second.total).toBe(1); // served from KV, no D1
// Posting another comment bumps the comments epoch → reload (D1 down).
vi.mocked(getDb).mockResolvedValue(db);
await new CommentRepository(db).create({
collection: "post",
contentId: postId,
authorName: "B",
authorEmail: "b@example.com",
body: "second!",
status: "approved",
});
await flush();
const third = await getComments({ collection: "post", contentId: postId });
expect(third.total).toBe(2);
});
it("does not collide when reactions/sort differ from a prior cached call", async () => {
const base = await getComments({ collection: "post", contentId: postId });
const commentId = base.items[0]!.id;
await new CommentReactionRepository(db).toggle({
commentId,
reaction: "like",
voterHash: "voter-1",
});
await flush();
// Reaction-less call: no counts attached.
const plain = await getComments({ collection: "post", contentId: postId });
expect(plain.items[0]!.reactions).toBeUndefined();
await flush();
// reactions:true must get its own entry, not the reaction-less snapshot.
const withReactions = await getComments({
collection: "post",
contentId: postId,
reactions: true,
});
expect(withReactions.items[0]!.reactions).toEqual({ like: 1 });
});
it("busts the comments cache when a reaction is toggled", async () => {
const { handleReactionToggle } = await import("../../src/api/handlers/comment-reactions.js");
const base = await getComments({ collection: "post", contentId: postId, reactions: true });
const commentId = base.items[0]!.id;
expect(base.items[0]!.reactions).toBeUndefined();
await flush();
const res = await handleReactionToggle(db, {
collection: "post",
contentId: postId,
commentId,
reaction: "like",
voterHash: "voter-1",
});
expect(res.success).toBe(true);
await flush();
const after = await getComments({ collection: "post", contentId: postId, reactions: true });
expect(after.items[0]!.reactions).toEqual({ like: 1 });
});
});