agentleFS
Open-Source-LegalOpen-Source-Legal/OpenContracts/.cursor/rules/test-document-knowledge-base.mdc

OpenContracts / rules

Cursor rule1.5k starsChanged 14 months agoMITView on GitHub

When testing <DocumentKnowledgeBase/> or our pdf.js annotations

Trending

Quiet since April.thin dataabout the whole repository

Dec 2025: 0 mentionsJan 2026: 0 mentionsFeb 2026: 0 mentionsMar 2026: 0 mentionsApr 2026: 2 mentionsMay 2026: 0 mentionsJun 2026: 0 mentionsJul 2026: 0 mentionsAug 2026: 0 mentionsSep 2026: 0 mentionsOct 2026: 0 mentionsNov 2026: 0 mentions
Dec 2025Jun 2026Nov 2026
Mentions a month of the Open-Source-Legal/OpenContracts repository that our weekly crawl found: only 2 in all, so this is a hint, not a trend. There is no adoption data for this file yet.
  • Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.

    Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.

  • In this repository4 other files it gives its agents.

The file

Show its rules and text

We split this test-document-knowledge-base.mdc into the rules it gives an agent

13 rules. About 0 are common, 8 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈8

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Follow the checklist below and copy / adapt the helpers that already live in this directory.

    line 9
  2. Very specific:

    Never mount DocumentKnowledgeBase directly in tests – you will miss at least one of the above.

    line 25
  3. Distinctive:

    When adding new tests, start by copying this array and trimming what you do not need – never delete the empty-string variant unless you are …

    line 61
  4. Very specific:

    If you add a different document, remember to update the paths and ensure the asset exists inside frontend/test-assets.

    line 80
  5. Distinctive:

    Wait for *visible evidence* instead of raw network-idle:

    line 90
  6. Distinctive:

    When interacting with the PDF canvas use page.mouse not locator.dragTo – the selection layer is an overlaid div, not the canvas element …

    line 93
  7. Distinctive:

    After synthetic drag operations, give the UI time to flush:

    line 94
  8. Distinctive:

    Every policy that used external reactive variables was purged to avoid evaluation during Playwright’s serialisation (§ see 14681).

    line 112
  9. Very specific:

    If your new test needs another root field (e.g.

    line 114
  10. Distinctive:

    Update the mock list – new queries, new variables.

    line 131
  11. Very specific:

    Add any new REST asset routes to registerRestMocks.

    line 133
  12. Very specific:

    If you introduce new atoms, wrap them in the test <Provider> hierarchy.

    line 134
  13. Distinctive:

    Keep this read-me up-to-date.

    line 136

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
test-document-knowledge-base.mdc
---
description: When testing <DocumentKnowledgeBase/> or our pdf.js annotations
globs: *.ct.tsx
---
# Testing `DocumentKnowledgeBase`

This document records the **canonical approach** for component–testing `DocumentKnowledgeBase` with Playwright-CT, Apollo Client mocks and Jotai atoms.
The component depends on many React-contexts, GraphQL queries, REST calls and external assets – missing any of them will usually surface as *silent timeouts* or *stack-overflows*.
Follow the checklist below and copy / adapt the helpers that already live in this directory.

---

## 1.  Always mount through `DocumentKnowledgeBaseTestWrapper`

`DocumentKnowledgeBaseTestWrapper.tsx` encapsulates **everything the component needs**:

* `MockedProvider` with
  * a minimal but **type-policy aware** `InMemoryCache`
  * an `ApolloLink` that
    • delegates normal requests to `MockLink`
    • intercepts **wild-card** `addAnnotation` mutations, returning a canned payload
* Jotai’s global `<Provider>` so every `…Atom` used inside the component is satisfied
* The optional `CorpusStateDebugger` that prints the corpus atom whenever it changes

Never mount `DocumentKnowledgeBase` directly in tests – you will miss at least one of the above.

Example:

```tsx
await mount(
<DocumentKnowledgeBaseTestWrapper
mocks={graphqlMocks}
documentId={PDF_DOC_ID}
corpusId={CORPUS_ID}
/>
);
```


---

## 2.  GraphQL mocks – common pitfalls

`DocumentKnowledgeBase` renders **before** it knows which tab / layer the user will pick, therefore a *surprisingly large* set of queries executes immediately.

1. `GET_DOCUMENT_KNOWLEDGE_AND_ANNOTATIONS`
2. `GET_DOCUMENT_ANALYSES_AND_EXTRACTS`
3. `GET_CONVERSATIONS`

Extra `refetch` calls are triggered when:
* the user selects an analysis (`analysisId` set)
* the user selects an extract (`analysisId` replaced by the extract id)

### Mandatory mocks

The test file `DocumentKnowledgeBase.ct.tsx` already includes a *complete* list that works for:
* a PDF document (`PDF_DOC_ID`)
* a TXT document (`TXT_DOC_ID`)
* the **unexpected** boot call where `documentId === ""`

When adding new tests, start by copying this array and trimming what you do not need – **never** delete the empty-string variant unless you are 100 % sure no component code path calls it.

### “Why do I see `Unhandled GraphQL request`?”

1. Variables mismatch – Playwright’s mock-matcher must equal the variables *exactly* (including `undefined` vs missing keys).
2. A second refetch for the same query – add the mock **again** (MockLink does *not* reuse responses).

---

## 3.  Asset & REST mocking

The component loads:

* `pawlsParseFile` (JSON)
* `pdfFile`    (binary)
* `txtExtractFile` (plain-text)
* `mdSummaryFile`  (markdown)

`DocumentKnowledgeBase.ct.tsx → registerRestMocks` has route handlers for all of them.
If you add a different document, remember to update the paths and ensure the asset exists inside `frontend/test-assets`.

---

## 4.  Playwright test guidelines

1. Increase the default timeout – PDF rendering in Chromium under Playwright is slow:
   ```ts
   const LONG_TIMEOUT = 20_000;
   ```
2. Wait for *visible evidence* instead of raw network-idle:
   * Heading with the document title
   * The “Summary” button with `.active` class
3. When interacting with the PDF canvas use **page.mouse** not **locator.dragTo** – the selection layer is an overlaid div, not the canvas element itself.
4. After synthetic drag operations, give the UI time to flush:
   ```ts
   await page.waitForTimeout(500);        // UI settle
   await page.waitForTimeout(1_000);      // Apollo cache settle
   ```
5. Assertions that depend on Apollo mutation results must target the **id returned by the mutation mock**:
   ```ts
   const annotationElement = annotationsPanel.locator(
     '[data-annotation-id="new-annot-1"]'
   );
   await expect(annotationElement).toBeVisible({ timeout: 15_000 });
   ```

---

## 5.  Minimal In-Memory cache

`mergeArrayByIdFieldPolicy` and `relayStylePagination()` are the only custom read/merge helpers retained.
Every policy that used external reactive variables was **purged** to avoid evaluation during Playwright’s serialisation (§ see 14681).

If your new test needs another root field (e.g. `fieldsets`) add it with *simple* `relayStylePagination()`.

---

## 6.  Troubleshooting checklist

| Symptom | Likely cause | Fix |
|---------|--------------|-----|
| `Maximum call stack size exceeded` inside Playwright | Entire `InMemoryCache` serialised in the test | Keep cache definition inside the wrapper, **not** the test |
| `Unhandled GraphQL request` | Missing / mismatched mock | Add the mock with *exact* variables, duplicate for refetches |
| PDF never finishes loading | PDF asset missing | Ensure `test-assets/test.pdf` exists and `page.route` handler returns it |
| Annotation never appears | Drag did not produce selection, mutation not fired | Check drag co-ordinates, verify console log “REQUEST_ADD_ANNOTATION” |

---

## 7.  When changing component behaviour

1. Update the mock list – new queries, new variables.
2. Extend the custom ApolloLink if you introduce more wildcard mutations.
3. Add any new REST asset routes to `registerRestMocks`.
4. If you introduce new atoms, wrap them in the test `<Provider>` hierarchy.

Keep this read-me up-to-date; flaky tests usually trace back to one of the steps above.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.