yats-toolkit 0.4.2 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,197 @@
1
+ /**
2
+ * Tests — P1 commit-based watch.
3
+ *
4
+ * Covers parseNameStatus (git diff --name-status parsing) and applyDiff: a
5
+ * commit with added/modified/deleted/renamed files must produce the right
6
+ * HTTP calls to the YATS server (file/remove/complete/commit).
7
+ */
8
+
9
+ import { describe, it, before, after } from "node:test";
10
+ import assert from "node:assert/strict";
11
+ import { createServer } from "node:http";
12
+ import { join } from "node:path";
13
+ import { writeFileSync, mkdirSync, rmSync } from "node:fs";
14
+ import { execSync } from "node:child_process";
15
+ import { tmpdir } from "node:os";
16
+
17
+ import { parseNameStatus, applyDiff } from "./watch.js";
18
+ import { mergeRepoConfig } from "./indexer.js";
19
+
20
+ function git(cwd, cmd) {
21
+ return execSync(`git ${cmd}`, { cwd, encoding: "utf-8", stdio: "pipe" }).trim();
22
+ }
23
+
24
+ describe("parseNameStatus", () => {
25
+ it("parses added, modified, deleted, and renamed lines", () => {
26
+ const changes = parseNameStatus(
27
+ "A\tsrc/new.ts\n" +
28
+ "M\tsrc/edited.ts\n" +
29
+ "D\tsrc/old.ts\n" +
30
+ "R100\tsrc/before.ts\tsrc/after.ts\n",
31
+ );
32
+ assert.deepEqual(changes, {
33
+ added: ["src/new.ts"],
34
+ modified: ["src/edited.ts"],
35
+ deleted: ["src/old.ts"],
36
+ renamed: [{ from: "src/before.ts", to: "src/after.ts" }],
37
+ });
38
+ });
39
+
40
+ it("unquotes paths with spaces and ignores blank lines", () => {
41
+ const changes = parseNameStatus(
42
+ '\nM\t"src/my file.ts"\nD\t"src/old dir/file.ts"\n\n',
43
+ );
44
+ assert.deepEqual(changes.added, []);
45
+ assert.deepEqual(changes.modified, ["src/my file.ts"]);
46
+ assert.deepEqual(changes.deleted, ["src/old dir/file.ts"]);
47
+ assert.deepEqual(changes.renamed, []);
48
+ });
49
+
50
+ it("handles empty output", () => {
51
+ assert.deepEqual(parseNameStatus(""), {
52
+ added: [],
53
+ modified: [],
54
+ deleted: [],
55
+ renamed: [],
56
+ });
57
+ });
58
+ });
59
+
60
+ describe("applyDiff — commit-based sync (P1)", () => {
61
+ let workDir;
62
+ let server;
63
+ let requests = [];
64
+ let baseUrl;
65
+ let repoName;
66
+ let v1;
67
+ let v2;
68
+
69
+ before(async () => {
70
+ // Temp git repo with two commits covering every change kind
71
+ workDir = join(tmpdir(), `yats-watch-test-${Date.now()}`);
72
+ mkdirSync(workDir, { recursive: true });
73
+ git(workDir, "init");
74
+ git(workDir, "config user.email test@test.com");
75
+ git(workDir, "config user.name Test");
76
+
77
+ writeFileSync(join(workDir, "a.txt"), "one\n");
78
+ writeFileSync(join(workDir, "b.txt"), "two\n");
79
+ writeFileSync(join(workDir, "d.txt"), "four\n");
80
+ git(workDir, "add -A");
81
+ git(workDir, "commit -m 'v1'");
82
+ v1 = git(workDir, "rev-parse HEAD");
83
+
84
+ writeFileSync(join(workDir, "a.txt"), "one (modified)\n");
85
+ writeFileSync(join(workDir, "c.txt"), "three\n");
86
+ rmSync(join(workDir, "b.txt"));
87
+ git(workDir, "mv d.txt e.txt");
88
+ git(workDir, "add -A");
89
+ git(workDir, "commit -m 'v2'");
90
+ v2 = git(workDir, "rev-parse HEAD");
91
+
92
+ // Fake YATS server that captures requests
93
+ server = createServer((req, res) => {
94
+ let body = "";
95
+ req.on("data", (chunk) => (body += chunk));
96
+ req.on("end", () => {
97
+ requests.push({ method: req.method, url: req.url, body: body ? JSON.parse(body) : null });
98
+ res.writeHead(200, { "Content-Type": "application/json" });
99
+ res.end(JSON.stringify({ ok: true }));
100
+ });
101
+ });
102
+ await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
103
+ baseUrl = `http://127.0.0.1:${server.address().port}`;
104
+ repoName = workDir;
105
+ });
106
+
107
+ after(() => {
108
+ server?.close();
109
+ try { rmSync(workDir, { recursive: true }); } catch {}
110
+ });
111
+
112
+ it("streams exactly the changed files and records the new commit", async () => {
113
+ await applyDiff(workDir, repoName, v1, v2, baseUrl);
114
+
115
+ const byUrl = (path) => requests.filter((r) => r.url === path);
116
+
117
+ const fileRequests = byUrl("/index/file");
118
+ const sentPaths = fileRequests.map((r) => r.body.filePath).sort();
119
+ assert.deepEqual(
120
+ sentPaths,
121
+ ["a.txt", "c.txt", "e.txt"],
122
+ "modified + added + renamed-to files are sent",
123
+ );
124
+ // Content is sent for the modified file
125
+ const aReq = fileRequests.find((r) => r.body.filePath === "a.txt");
126
+ assert.equal(aReq.body.content, "one (modified)\n");
127
+ assert.equal(aReq.body.repoName, repoName, "repository identity is the full path");
128
+
129
+ const removeRequests = byUrl("/index/remove");
130
+ const removedPaths = removeRequests.map((r) => r.body.path).sort();
131
+ assert.deepEqual(
132
+ removedPaths,
133
+ ["b.txt", "d.txt"],
134
+ "deleted and renamed-from files are removed",
135
+ );
136
+
137
+ const complete = byUrl("/index/complete");
138
+ assert.equal(complete.length, 1, "relationships are finalized");
139
+ assert.equal(complete[0].body.repository, repoName);
140
+
141
+ const commit = byUrl("/index/commit");
142
+ assert.equal(commit.length, 1, "new commit is recorded");
143
+ assert.equal(commit[0].body.repository, repoName);
144
+ assert.equal(commit[0].body.commit, v2);
145
+ });
146
+
147
+ it("reports no-op when HEAD did not change files", async () => {
148
+ requests = [];
149
+ await applyDiff(workDir, repoName, v2, v2, baseUrl);
150
+ assert.equal(requests.filter((r) => r.url === "/index/file").length, 0);
151
+ // The commit is still recorded (idempotent sync)
152
+ assert.equal(requests.filter((r) => r.url === "/index/commit").length, 1);
153
+ });
154
+
155
+ it("applies the repo config: docs skipped when indexDocs=false (P6)", async () => {
156
+ // New commit adding a doc file and a code file
157
+ writeFileSync(join(workDir, "guide.md"), "# Guide\n");
158
+ writeFileSync(join(workDir, "extra.txt"), "plain text\n");
159
+ git(workDir, "add -A");
160
+ git(workDir, "commit -m 'v3: add docs + code'");
161
+ const v3 = git(workDir, "rev-parse HEAD");
162
+
163
+ requests = [];
164
+ const effective = mergeRepoConfig({}, { indexDocs: false, docExtensions: [".md"] });
165
+ await applyDiff(workDir, repoName, v2, v3, baseUrl, effective);
166
+
167
+ const sentPaths = requests
168
+ .filter((r) => r.url === "/index/file")
169
+ .map((r) => r.body.filePath)
170
+ .sort();
171
+ assert.deepEqual(
172
+ sentPaths,
173
+ ["extra.txt"],
174
+ "doc files (.md) are skipped, code/plain files are sent",
175
+ );
176
+ });
177
+
178
+ it("applies docPatterns: only whitelisted docs are sent (P6)", async () => {
179
+ const v3 = git(workDir, "rev-parse HEAD");
180
+ mkdirSync(join(workDir, "docs"), { recursive: true });
181
+ writeFileSync(join(workDir, "docs", "api.md"), "# API\n");
182
+ writeFileSync(join(workDir, "notes.md"), "# Notes\n");
183
+ git(workDir, "add -A");
184
+ git(workDir, "commit -m 'v4: more docs'");
185
+ const v4 = git(workDir, "rev-parse HEAD");
186
+
187
+ requests = [];
188
+ const effective = mergeRepoConfig({}, { docExtensions: [".md"], docPatterns: ["docs/"] });
189
+ await applyDiff(workDir, repoName, v3, v4, baseUrl, effective);
190
+
191
+ const sentPaths = requests
192
+ .filter((r) => r.url === "/index/file")
193
+ .map((r) => r.body.filePath)
194
+ .sort();
195
+ assert.deepEqual(sentPaths, ["docs/api.md"], "only docs matching the whitelist are sent");
196
+ });
197
+ });