|
| 1 | +import type { Kysely } from "kysely"; |
| 2 | +import { it, expect, beforeEach, afterEach } from "vitest"; |
| 3 | + |
| 4 | +import { handleContentCreate } from "../../src/api/index.js"; |
| 5 | +import type { Database } from "../../src/database/types.js"; |
| 6 | +import { emdashLoader } from "../../src/loader.js"; |
| 7 | +import { runWithContext } from "../../src/request-context.js"; |
| 8 | +import { |
| 9 | + describeEachDialect, |
| 10 | + setupForDialectWithCollections, |
| 11 | + teardownForDialect, |
| 12 | + type DialectName, |
| 13 | + type DialectTestContext, |
| 14 | +} from "../utils/test-db.js"; |
| 15 | + |
| 16 | +describeEachDialect("Loader taxonomy term filter", (dialectName: DialectName) => { |
| 17 | + let ctx: DialectTestContext; |
| 18 | + let db: Kysely<Database>; |
| 19 | + let termSeq = 0; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + ctx = await setupForDialectWithCollections(dialectName); |
| 23 | + db = ctx.db; |
| 24 | + termSeq = 0; |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + await teardownForDialect(ctx); |
| 29 | + }); |
| 30 | + |
| 31 | + async function createPost(title: string) { |
| 32 | + const result = await handleContentCreate(db, "post", { |
| 33 | + data: { title }, |
| 34 | + status: "published", |
| 35 | + }); |
| 36 | + if (!result.success) throw new Error("Failed to create post"); |
| 37 | + return result.data!.item; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Insert a taxonomy term and return its id. `category` and `tag` are the |
| 42 | + * default taxonomy defs seeded by migration 006, so both are recognized as |
| 43 | + * taxonomy keys by the `where` filter. We use `id` as the value stored in |
| 44 | + * `content_taxonomies.taxonomy_id` (these terms have no translations, so the |
| 45 | + * row id coincides with the translation_group the pivot references). |
| 46 | + */ |
| 47 | + async function term(name: string, slug: string) { |
| 48 | + const id = `tax_${name}_${slug}_${termSeq++}`; |
| 49 | + await db |
| 50 | + .insertInto("taxonomies" as never) |
| 51 | + .values({ id, name, slug, label: slug, translation_group: id } as never) |
| 52 | + .execute(); |
| 53 | + return id; |
| 54 | + } |
| 55 | + |
| 56 | + async function tag(contentId: string, taxonomyId: string) { |
| 57 | + await db |
| 58 | + .insertInto("content_taxonomies" as never) |
| 59 | + .values({ collection: "post", entry_id: contentId, taxonomy_id: taxonomyId } as never) |
| 60 | + .execute(); |
| 61 | + } |
| 62 | + |
| 63 | + function load(where: Record<string, unknown>) { |
| 64 | + const loader = emdashLoader(); |
| 65 | + return runWithContext({ editMode: false, db }, () => |
| 66 | + loader.loadCollection!({ filter: { type: "post", where: where as never } }), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + it("filters by a single taxonomy term", async () => { |
| 71 | + const news = await term("category", "news"); |
| 72 | + const a = await createPost("In News"); |
| 73 | + await createPost("Untagged"); |
| 74 | + await tag(a.id, news); |
| 75 | + |
| 76 | + const result = await load({ category: "news" }); |
| 77 | + |
| 78 | + expect(result.entries).toHaveLength(1); |
| 79 | + expect(result.entries[0]!.data.title).toBe("In News"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("ANDs across two taxonomies — only entries tagged in BOTH match (#1479)", async () => { |
| 83 | + const news = await term("category", "news"); |
| 84 | + const featured = await term("tag", "featured"); |
| 85 | + |
| 86 | + const both = await createPost("News + Featured"); |
| 87 | + const newsOnly = await createPost("News Only"); |
| 88 | + const featuredOnly = await createPost("Featured Only"); |
| 89 | + |
| 90 | + await tag(both.id, news); |
| 91 | + await tag(both.id, featured); |
| 92 | + await tag(newsOnly.id, news); |
| 93 | + await tag(featuredOnly.id, featured); |
| 94 | + |
| 95 | + // Before the fix, the second taxonomy key ("tag") was silently dropped |
| 96 | + // and this returned both "News + Featured" and "News Only". |
| 97 | + const result = await load({ category: ["news"], tag: ["featured"] }); |
| 98 | + |
| 99 | + expect(result.entries).toHaveLength(1); |
| 100 | + expect(result.entries[0]!.data.title).toBe("News + Featured"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("ORs slugs within a taxonomy while ANDing across taxonomies", async () => { |
| 104 | + const news = await term("category", "news"); |
| 105 | + const sports = await term("category", "sports"); |
| 106 | + const featured = await term("tag", "featured"); |
| 107 | + |
| 108 | + // Matches: in (news OR sports) AND featured. |
| 109 | + const a = await createPost("News + Featured"); |
| 110 | + const b = await createPost("Sports + Featured"); |
| 111 | + const c = await createPost("News, not Featured"); |
| 112 | + |
| 113 | + await tag(a.id, news); |
| 114 | + await tag(a.id, featured); |
| 115 | + await tag(b.id, sports); |
| 116 | + await tag(b.id, featured); |
| 117 | + await tag(c.id, news); |
| 118 | + |
| 119 | + const result = await load({ category: ["news", "sports"], tag: ["featured"] }); |
| 120 | + |
| 121 | + const titles = result.entries.map((e) => e.data.title); |
| 122 | + expect(titles).toHaveLength(2); |
| 123 | + expect(titles).toContain("News + Featured"); |
| 124 | + expect(titles).toContain("Sports + Featured"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("returns no entries when any one taxonomy filter is an empty array", async () => { |
| 128 | + const news = await term("category", "news"); |
| 129 | + const post = await createPost("In News"); |
| 130 | + await tag(post.id, news); |
| 131 | + |
| 132 | + // `category` matches, but the empty `tag` array short-circuits the whole |
| 133 | + // query to empty rather than emitting `t.slug IN ()`. |
| 134 | + const result = await load({ category: ["news"], tag: [] }); |
| 135 | + |
| 136 | + expect(result.entries).toHaveLength(0); |
| 137 | + }); |
| 138 | +}); |
0 commit comments