Skip to content

aidenlx/zotlit / skills/zotlit-query

zotlit-query

Verified

Query Zotero works, files, and reading marks through ZotLit in Obsidian. Use for literature reviews, reading plans, missing PDFs, broken linked files, duplicate PDFs, Annotation evidence, counts by paper or year, and complete exports.

evals/corpus-query.test.mjs

View on GitHub
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import { existsSync } from "node:fs";
import { readFile, rm } from "node:fs/promises";
import { createRequire } from "node:module";
import { join, resolve } from "node:path";
import test from "node:test";

import { buildFixture, getFixtureLayout } from "@zotlit/scripts/fixture";

import { validate } from "./check.mjs";

const repo = resolve(import.meta.dirname, "../../..");
const requireQuery = createRequire(
  join(repo, "packages/item-query/package.json"),
);
const requirePlugin = createRequire(join(repo, "apps/obsidian/package.json"));
const { Effect } = await import(requireQuery.resolve("effect"));
const { createClient } = await import(
  requireQuery.resolve("@zotlit/db/client/node")
);
const { attachmentAbsPath } = await import(
  requireQuery.resolve("@zotlit/db/path")
);
const { ItemQueryDatabase } = await import(
  requireQuery.resolve("@zotlit/db/item-query")
);
const {
  listQueryValues,
  collectQuery,
  ITEMS,
  ATTACHMENTS,
  ANNOTATIONS,
  AttachmentFileResolver,
} = await import(requirePlugin.resolve("@zotlit/item-query"));
const oracle = JSON.parse(
  await readFile(new URL("./oracle.json", import.meta.url), "utf8"),
);

await test("every case oracle matches a real query over the seeded Fixture", async (t) => {
  const id = randomUUID(),
    base = join(repo, ".scratch", `query-corpus-base-${id}`),
    root = join(repo, ".scratch", `query-corpus-${id}`);
  let client;
  try {
    await buildFixture(getFixtureLayout(base));
    execFileSync(process.execPath, [
      join(import.meta.dirname, "prepare.mjs"),
      base,
      root,
    ]);
    client = createClient(join(root, "zotero-data", "zotero.sqlite"), {
      connection: { readOnly: true },
    });
    const cases = [
      ...Object.entries(oracle.cases),
      ...["annotations.length", "annotations[]"].map((field) => [
        "count_per_paper",
        {
          ...oracle.cases.count_per_paper,
          group: undefined,
          count: 2,
          fields: ["title", field],
          request: {
            from: "items",
            library: "personal",
            filter:
              'collections.within("Query thesis") && annotations.length > 0',
          },
        },
        `count_per_paper from Items with ${field}`,
      ]),
    ];
    for (const [name, spec, label = name] of cases)
      await t.test(label, async () => {
        const fields = spec.fields ?? spec.request.fields.split(",");
        const libraries = [
          ...(spec.request.library === "all" ||
          spec.request.library.includes("personal")
            ? [{ libraryID: 1, groupID: null }]
            : []),
          ...(spec.request.library === "all" ||
          spec.request.library.includes("group:118")
            ? [{ libraryID: 3, groupID: 118 }]
            : []),
        ];
        let filter = spec.request.filter;
        if (spec.discovery) {
          const listing = await Effect.runPromise(
            listQueryValues(libraries[0], {
              kind: "collections",
              match: "thesis",
              limit: null,
            }).pipe(Effect.provideService(ItemQueryDatabase, { client })),
          );
          assert.ok(listing.values.includes(spec.discovery.path));
          filter = `collections.within(${JSON.stringify(listing.values.find((path) => path === spec.discovery.path))})`;
        }
        const result = await Effect.runPromise(
          collectQuery(
            {
              items: ITEMS,
              attachments: ATTACHMENTS,
              annotations: ANNOTATIONS,
            }[spec.request.from],
            {
              libraries,
              filter,
              fields,
              ...(spec.request.sort
                ? {
                    sort: spec.request.sort.split(",").map((field) => ({
                      field: field.startsWith("-") ? field.slice(1) : field,
                      direction: field.startsWith("-") ? "desc" : "asc",
                    })),
                  }
                : {}),
              ...(spec.group ? { group: spec.group } : {}),
            },
          ).pipe(
            Effect.provideService(ItemQueryDatabase, { client }),
            Effect.provideService(AttachmentFileResolver, (attachment) =>
              Effect.sync(() => {
                const path = attachmentAbsPath(attachment, {
                  dataDir: join(root, "zotero-data"),
                  baseAttachmentPath: null,
                });
                return {
                  path: path ?? null,
                  exists: path ? existsSync(path) : false,
                };
              }),
            ),
          ),
        );
        const envelope = {
          ...result,
          ok: true,
          contractVersion: 3,
          command: "zotlit:query",
          identity: {
            source: {
              databasePath: join(root, "zotero-data", "zotero.sqlite"),
            },
            vault: { path: join(root, "zt-fixture-vault") },
          },
          libraries: libraries.map((l) =>
            l.groupID
              ? { type: "group", groupID: l.groupID }
              : { type: "personal" },
          ),
          request: {
            from: spec.request.from,
            filter: spec.request.filter,
            fields,
            group: spec.group,
            limit: null,
          },
        };
        assert.deepEqual(validate(name, envelope, { runRoot: root }), []);
        // A grouped count is of records; element groups can overlap.
        assert.equal(
          result.groups ? result.totalCount : result.returnedCount,
          spec.count,
        );
      });
  } finally {
    client?.$client.close();
    await rm(base, { recursive: true, force: true });
    await rm(root, { recursive: true, force: true });
  }
});
add-product-e2e-evalpaperclipai/paperclipAdd or extend a Paperclip full-stack runner E2E workflow, fixture, matcher, or report evidence path for local or Daytona execution.Stars: 115kUpdated Claude CodeCodexVerifiedInstall
add-runner-evalpaperclipai/paperclipAdd or extend a Paperclip Runner protocol evaluation definition, roster, assertion, or report fixture with provenance and narrow validation.Stars: 115kUpdated Claude CodeCodexVerifiedInstall
diagnose-why-work-stoppedpaperclipai/paperclipCompatibility entry point for failed, stalled, looping, or over-recovered Paperclip work. Use fix-failed-paperclip-run for the full diagnosis, regression, repair, real verification, and green-PR workflow; honor an explicitly requested diagnosis-only scope.Stars: 115kUpdated Claude CodeCodexVerifiedInstall
fix-failed-paperclip-runpaperclipai/paperclipDiagnose and fix failed, stuck, slow, or incorrectly blocked Paperclip tasks and runs. Use with a task/run URL or failure report to trace the root cause, prove a regression red then green, check second-order effects, verify the real workflow, and deliver a PR with passing checks.Stars: 115kUpdated Claude CodeCodexVerifiedInstall