diff --git a/src/core/folder-counts.ts b/src/core/folder-counts.ts index 52d66c4..7bf9116 100644 --- a/src/core/folder-counts.ts +++ b/src/core/folder-counts.ts @@ -30,7 +30,7 @@ export async function folderCounts(repo: Repository, requestedPath: unknown): Pr ...(originalPath ? { $or: [{ path: originalPath }, { path: { $regex: "^" + escapedPrefix } }] } : {}), } }, { $project: { child: { $cond: [ - { $eq: ["$path", originalPath] }, "", + { $eq: ["$path", { $literal: originalPath }] }, "", { $arrayElemAt: [{ $split: [{ $substrCP: ["$path", prefixLength, { $subtract: [{ $strLenCP: "$path" }, prefixLength] }] }, "/"] }, 0] }, ] } } }, diff --git a/test/folder-counts.test.js b/test/folder-counts.test.js index bba67d6..2f1f1ec 100644 --- a/test/folder-counts.test.js +++ b/test/folder-counts.test.js @@ -66,6 +66,13 @@ describe("folder count request validation", () => { expect(JSON.stringify(scoped)).not.to.match(/Alice|Bob/); }); + for (const name of ["$data", "$$ROOT", "$$CURRENT"]) it(`treats directory ${name} as a literal in aggregation expressions`, async () => { + await insert([{ name, path: "" }, { name: "direct.txt", path: name, size: 1 }, + { name: "$nested", path: name }, { name: "child.txt", path: `${name}/$nested`, size: 1 }]); + expect(await folderCounts(repo, name)).to.deep.equal({ [name]: 2, [`${name}/$nested`]: 1 }); + expect(await folderCounts(repo, `${name}/$nested`)).to.deep.equal({ [`${name}/$nested`]: 1 }); + }); + it("uses the active generation on each request without reusing obsolete counts", async () => { await insert([{ path: "old", name: "a", size: 1 }]); expect(await folderCounts(repo, "")).to.deep.equal({ "": 1, old: 1 });