# Test an AI edit against the bytes in a Markdown file

The owner field changes from Jo to Casey. That adds three characters before the status line, so the character positions have shifted since the assistant prepared its proposal against the older release note.

Only the line `Status: queued.` needs to change to `Status: ready.` Saving the old document would also switch the owner back to Jo.

GoTipe is a block editor library for apps that store Markdown. You don’t need an editor UI for this test: it uses the headless Markdown and suggestions packages.

The table shows the owner and status values; the YAML front matter and formatting are omitted here.

| Line | Original | Current | After the edit |
| --- | --- | --- | --- |
| Owner | Jo | Casey | Casey |
| Status | queued | queued | ready |

## Run the example

For this GoTipe example, the expected output is the current text with queued replaced by ready. The test compares UTF-8 buffers with that output. It’ll fail if the owner gets reverted, CRLF changes to LF, the byte order mark is dropped, or the YAML front matter changes.

Run the Node.js script in a fresh directory. No model or API key needed; the proposal is a static object.

```sh
npm init -y
npm install --save-exact @gotipe/markdown@0.1.3 @gotipe/suggestions@0.1.3 @gotipe/core@0.1.6
```

```js
// Save as review-note.mjs and run node review-note.mjs.
import assert from "node:assert/strict";
import { parseMarkdown, serializeMarkdown } from "@gotipe/markdown";
import { applyAnchoredEdits } from "@gotipe/suggestions";

const original = "\uFEFF---\r\nteam: Docs\r\n---\r\n\r\nOwner: Jo.\r\n\r\nStatus: queued.\r\n";
const proposal = { before: "Status: ", quote: "queued", after: ".", text: "ready" };
const current = original.replace("Owner: Jo.", "Owner: Casey.");

const edit = applyAnchoredEdits(current, [proposal]);
assert.equal(edit.ok, true);
const { doc, snapshot } = parseMarkdown(edit.text);
const saved = serializeMarkdown(doc, snapshot).text;
assert.deepEqual(Buffer.from(saved), Buffer.from(current.replace("queued", "ready")));

const duplicate = current + "\r\nStatus: queued.\r\n";
assert.deepEqual(applyAnchoredEdits(duplicate, [proposal]), {
  ok: false, reason: "ambiguous", index: 0
});
console.log("Byte check passed; duplicate status needs review.");
```

`applyAnchoredEdits` finds the combined `before`, `quote`, and `after` text in the current text. Here, it finds one instance of `Status: queued.` The longer owner name doesn’t affect the match.

Just add another status line and there are two equally good matches. The last assertion checks for `ambiguous`. The failure result has no replacement text; your program decides how to show the conflict to the reviewer.

Look at where parsing happens: after the source edit in the example script. The resulting document and snapshot go to the serializer without further edits; the assertion checks that round trip.

Editing a parsed block needs its own test; modified blocks may be written as normalized Markdown. These buffers are all in memory—there’s no Markdown file reading or writing in the script.

## Save against the revision you read

With shared storage, make saving conditional on the stored revision matching the revision you read. If someone saves different changes between your read and write, reload the current text and reapply the proposal. Otherwise, your save could overwrite their changes even if the anchor matches correctly.

For the byte check on disk, see the [file-editing guide](https://gotipe.com/docs/guides/edit-markdown-without-reformatting/?utm_source=hashnode&utm_medium=referral&utm_campaign=editor_guides). For batches of proposals and missing targets, see the [anchored-edit guide](https://gotipe.com/docs/guides/apply-ai-edits-after-the-document-changes/?utm_source=hashnode&utm_medium=referral&utm_campaign=editor_guides).

The Markdown codec and headless suggestions package used here are MIT-licensed. This post was drafted with AI. The example was executed against the package versions shown above.

