# test.coordination/core/transformers/reference.transformer.test.ts

> 30 lines of code and 0 definitions.

Tree: Codebase Testing
Language: typescript
Canonical: https://banes-lab.com/anatomy/testing#file-testing-test-coordination-core-transformers-reference-transformer-test-ts
Source text: https://banes-lab.com/source/testing/test.coordination/core/transformers/reference.transformer.test.ts.txt

Listed in [test.coordination/core/transformers](https://banes-lab.com/api/source/testing/test.coordination/core/transformers.md), after [test.coordination/core/transformers/corpus.transformer.test.ts](https://banes-lab.com/source/testing/test.coordination/core/transformers/corpus.transformer.test.ts.md) and before [test.coordination/core/transformers/scope.transformer.test.ts](https://banes-lab.com/source/testing/test.coordination/core/transformers/scope.transformer.test.ts.md).

## Contained in

- [test.coordination/core/transformers](https://banes-lab.com/anatomy/testing/folder-testing-test-coordination-core-transformers.md)

## Source

```typescript
import { describe, it } from "vitest";
import { join, resolve } from "node:path";
import { mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { renameMapOf, rewriteReferences } from "coordination-surface/tools/core/transformers/reference.transformer.ts";
import { SURFACE_ROOT } from "coordination-surface/tools/core/constants/path.constants.ts";
import assert from "node:assert/strict";
import { tmpdir } from "node:os";
import { writeVerbatim } from "@govlab/canonical-write";

describe("renameMapOf", () => {
    it("maps each move and its local: form", () => {
        assert.deepEqual(renameMapOf([{ from: "a.md", to: "b.md" }]), { "a.md": "b.md", "local:a.md": "local:b.md" });
    });
});

describe("rewriteReferences", () => {
    it("rewrites every reference to a moved file across the surface's documents, and counts the edits", () => {
        const root = mkdtempSync(join(tmpdir(), "coordination-references-"));
        try {
            const surface = resolve(root, SURFACE_ROOT);
            mkdirSync(surface, { recursive: true });
            const document = join(surface, "notes.md");
            writeVerbatim(document, "see [the old](old.md) and `old.md`\n");
            const count = rewriteReferences(root, renameMapOf([{ from: "old.md", to: "new.md" }]));
            assert.equal(count, 2);
            assert.equal(readFileSync(document, "utf8"), "see [the old](new.md) and `new.md`\n");
            assert.equal(rewriteReferences(root, { "absent.md": "x.md" }), 0);
        } finally {
            rmSync(root, { force: true, recursive: true });
        }
    });
});
```
