fs-context.test.ts 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. import { execFile as execFileCallback } from "node:child_process";
  2. import { EventEmitter } from "node:events";
  3. import {
  4. chmod,
  5. lstat,
  6. mkdir,
  7. mkdtemp,
  8. open,
  9. readdir,
  10. readFile,
  11. rm,
  12. stat,
  13. symlink,
  14. writeFile,
  15. } from "node:fs/promises";
  16. import { tmpdir } from "node:os";
  17. import { dirname, join } from "node:path";
  18. import { fileURLToPath } from "node:url";
  19. import { promisify } from "node:util";
  20. import { describe, expect, it, vi } from "vitest";
  21. import { READ_MAX_FILE_SIZE_BYTES } from "@zcode/contracts";
  22. import { createNodeContextSourceAdapter } from "../src/context/index.js";
  23. import {
  24. createNodeFileSystemAdapter,
  25. setRipgrepTimeoutMsForTests,
  26. setRipgrepWorkerFactoryForTests,
  27. } from "../src/fs/index.js";
  28. const execFile = promisify(execFileCallback);
  29. // Bugfix: 这些测试可能从仓库根或 apps/zcode-cli 子工程启动;fixture 路径必须绑定到当前测试文件位置。
  30. const TEST_DIR = dirname(fileURLToPath(import.meta.url));
  31. const GB2312_FIXTURE_PATH = join(TEST_DIR, "..", "..", "..", "tests", "gb2312.js");
  32. const GB2312_THIS_IS_BYTES = Buffer.from([0xd5, 0xe2, 0xca, 0xc7]);
  33. class HangingRipgrepWorker extends EventEmitter {
  34. terminated = false;
  35. terminate(): Promise<number> {
  36. this.terminated = true;
  37. queueMicrotask(() => {
  38. this.emit("exit", 1);
  39. });
  40. return Promise.resolve(1);
  41. }
  42. }
  43. describe("Node file and context adapters", () => {
  44. it("creates directory parents idempotently through FileSystemPort", async () => {
  45. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-directory-"));
  46. const path = join(dir, "nested", "memory");
  47. const fs = createNodeFileSystemAdapter();
  48. try {
  49. await expect(fs.createDirectory({ path })).resolves.toEqual({ path });
  50. await expect(fs.createDirectory({ path })).resolves.toEqual({ path });
  51. expect((await stat(path)).isDirectory()).toBe(true);
  52. } finally {
  53. await rm(dir, { force: true, recursive: true });
  54. }
  55. });
  56. it("reads and atomically writes text files through FileSystemPort", async () => {
  57. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-port-"));
  58. const path = join(dir, "nested", "file.txt");
  59. const fs = createNodeFileSystemAdapter();
  60. try {
  61. await fs.writeTextFile({
  62. path,
  63. content: "hello",
  64. createParents: true,
  65. atomic: true,
  66. });
  67. const read = await fs.readTextFile({ path });
  68. expect(read.content).toBe("hello");
  69. expect(read.revision?.id).toMatch(/^mtime:/);
  70. await fs.writeTextFile({
  71. path,
  72. content: "updated",
  73. expectedRevision: read.revision,
  74. });
  75. expect(await readFile(path, "utf8")).toBe("updated");
  76. await expect(fs.removeFile({ path })).resolves.toMatchObject({
  77. path,
  78. removed: true,
  79. });
  80. await expect(fs.removeFile({ path, missingOk: true })).resolves.toMatchObject({
  81. path,
  82. removed: false,
  83. });
  84. } finally {
  85. await rm(dir, { force: true, recursive: true });
  86. }
  87. });
  88. it.skipIf(process.platform === "win32")(
  89. "preserves executable permissions when atomically rewriting existing files",
  90. async () => {
  91. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-mode-"));
  92. const path = join(dir, "run.sh");
  93. const fs = createNodeFileSystemAdapter();
  94. try {
  95. await writeFile(path, "#!/usr/bin/env bash\necho old\n", "utf8");
  96. await chmod(path, 0o755);
  97. await fs.writeTextFile({
  98. path,
  99. content: "#!/usr/bin/env bash\necho new\n",
  100. atomic: true,
  101. });
  102. expect(await readFile(path, "utf8")).toBe("#!/usr/bin/env bash\necho new\n");
  103. expect((await stat(path)).mode & 0o777).toBe(0o755);
  104. } finally {
  105. await rm(dir, { force: true, recursive: true });
  106. }
  107. },
  108. );
  109. it.skipIf(process.platform === "win32")(
  110. "rejects atomic writes through symlink targets",
  111. async () => {
  112. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-symlink-"));
  113. const targetPath = join(dir, "target.txt");
  114. const linkPath = join(dir, "link.txt");
  115. const fs = createNodeFileSystemAdapter();
  116. try {
  117. await writeFile(targetPath, "old", "utf8");
  118. await symlink(targetPath, linkPath);
  119. await expect(
  120. fs.writeTextFile({
  121. path: linkPath,
  122. content: "new",
  123. atomic: true,
  124. }),
  125. ).rejects.toMatchObject({
  126. code: "io_error",
  127. message: `Refusing to write through symlink: ${linkPath}. Resolve the symlink and pass the real target path explicitly.`,
  128. });
  129. expect(await readFile(targetPath, "utf8")).toBe("old");
  130. expect((await lstat(linkPath)).isSymbolicLink()).toBe(true);
  131. expect((await readdir(dir)).filter((entry) => entry.includes(".tmp."))).toEqual([]);
  132. } finally {
  133. await rm(dir, { force: true, recursive: true });
  134. }
  135. },
  136. );
  137. it("normalizes CRLF reads and preserves requested CRLF writes", async () => {
  138. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-eol-"));
  139. const path = join(dir, "file.txt");
  140. const fs = createNodeFileSystemAdapter();
  141. try {
  142. await writeFile(path, "alpha\r\nbeta\r\n", "utf8");
  143. const read = await fs.readTextFile({ path });
  144. expect(read.content).toBe("alpha\nbeta\n");
  145. expect(read.lineEndings).toBe("CRLF");
  146. expect(read.encoding).toBe("utf8");
  147. await fs.writeTextFile({
  148. path,
  149. content: "alpha\ngamma\n",
  150. encoding: read.encoding,
  151. lineEndings: read.lineEndings,
  152. expectedRevision: read.revision,
  153. });
  154. expect(await readFile(path, "utf8")).toBe("alpha\r\ngamma\r\n");
  155. } finally {
  156. await rm(dir, { force: true, recursive: true });
  157. }
  158. });
  159. it("reads true line ranges from large files and rejects unbounded over-budget reads", async () => {
  160. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-range-"));
  161. const path = join(dir, "large.txt");
  162. const fs = createNodeFileSystemAdapter();
  163. const line = `${"x".repeat(120)}\n`;
  164. try {
  165. await writeFile(path, line.repeat(3000), "utf8");
  166. await expect(
  167. fs.readTextFileRange({
  168. path,
  169. maxBytes: READ_MAX_FILE_SIZE_BYTES,
  170. }),
  171. ).rejects.toMatchObject({
  172. code: "too_large",
  173. message: expect.stringContaining("maximum allowed size (256KB)"),
  174. });
  175. const read = await fs.readTextFileRange({
  176. path,
  177. offsetLine: 2500,
  178. limitLines: 2,
  179. });
  180. expect(read.content).toBe(`${"x".repeat(120)}\n${"x".repeat(120)}`);
  181. expect(read.startLine).toBe(2501);
  182. expect(read.lineCount).toBe(2);
  183. expect(read.totalLines).toBe(3001);
  184. expect(read.sizeBytes).toBeGreaterThan(READ_MAX_FILE_SIZE_BYTES);
  185. } finally {
  186. await rm(dir, { force: true, recursive: true });
  187. }
  188. });
  189. it("enforces the binary maxBytes contract without returning truncated content", async () => {
  190. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-binary-limit-"));
  191. const path = join(dir, "video.mp4");
  192. const fs = createNodeFileSystemAdapter();
  193. try {
  194. await writeFile(path, Buffer.from([1, 2, 3]));
  195. await expect(fs.readBinaryFile({ path, maxBytes: 2 })).rejects.toMatchObject({
  196. code: "too_large",
  197. });
  198. await expect(fs.readBinaryFile({ path, maxBytes: 3 })).resolves.toMatchObject({
  199. bytesRead: 3,
  200. sizeBytes: 3,
  201. });
  202. } finally {
  203. await rm(dir, { force: true, recursive: true });
  204. }
  205. });
  206. it("continues reading after a non-EOF short binary read", async () => {
  207. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-binary-short-read-"));
  208. const path = join(dir, "video.mp4");
  209. const fs = createNodeFileSystemAdapter();
  210. let readSpy: ReturnType<typeof vi.spyOn> | undefined;
  211. try {
  212. await writeFile(path, Buffer.from([1, 2, 3]));
  213. const probe = await open(path, "r");
  214. const fileHandlePrototype = Object.getPrototypeOf(probe) as {
  215. read: typeof probe.read;
  216. };
  217. const originalRead = fileHandlePrototype.read;
  218. await probe.close();
  219. let firstRead = true;
  220. readSpy = vi.spyOn(fileHandlePrototype, "read").mockImplementation(async function (
  221. this: typeof probe,
  222. buffer: Buffer,
  223. offset: number,
  224. length: number,
  225. position: number | null,
  226. ) {
  227. const readLength = firstRead ? Math.min(1, length) : length;
  228. firstRead = false;
  229. return originalRead.call(this, buffer, offset, readLength, position);
  230. } as never);
  231. const read = await fs.readBinaryFile({ path, maxBytes: 3 });
  232. expect(read.content).toEqual(Buffer.from([1, 2, 3]));
  233. expect(read.bytesRead).toBe(3);
  234. } finally {
  235. readSpy?.mockRestore();
  236. await rm(dir, { force: true, recursive: true });
  237. }
  238. });
  239. it("detects UTF-16LE text files and keeps their line ending metadata", async () => {
  240. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-utf16-"));
  241. const path = join(dir, "file.txt");
  242. const fs = createNodeFileSystemAdapter();
  243. try {
  244. await writeFile(path, Buffer.from("\uFEFFalpha\r\nbeta\r\n", "utf16le"));
  245. const read = await fs.readTextFile({ path });
  246. expect(read.encoding).toBe("utf16le");
  247. expect(read.lineEndings).toBe("CRLF");
  248. expect(read.content).toBe("\uFEFFalpha\nbeta\n");
  249. await fs.writeTextFile({
  250. path,
  251. content: read.content.replace("beta", "gamma"),
  252. encoding: read.encoding,
  253. lineEndings: read.lineEndings,
  254. });
  255. expect((await readFile(path)).toString("utf16le")).toBe("\uFEFFalpha\r\ngamma\r\n");
  256. } finally {
  257. await rm(dir, { force: true, recursive: true });
  258. }
  259. });
  260. it("reads ranges from the GB2312 fixture as logical Unicode text", async () => {
  261. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-gb2312-read-"));
  262. const path = join(dir, "gb2312.js");
  263. const fs = createNodeFileSystemAdapter();
  264. try {
  265. await writeFile(path, await readFile(GB2312_FIXTURE_PATH));
  266. const read = await fs.readTextFile({ path });
  267. expect(read.encoding).toBe("gb2312");
  268. expect(read.content).toContain("// 这是一个变量");
  269. expect(read.content).toContain("// 你好");
  270. const range = await fs.readTextFileRange({
  271. path,
  272. offsetLine: 2,
  273. limitLines: 3,
  274. });
  275. expect(range.encoding).toBe("gb2312");
  276. expect(range.content).toBe("// 这是一个函数\nfunction go() {\n return 1");
  277. } finally {
  278. await rm(dir, { force: true, recursive: true });
  279. }
  280. });
  281. it("preserves GB2312 bytes when rewriting the fixture", async () => {
  282. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-gb2312-write-"));
  283. const path = join(dir, "gb2312.js");
  284. const fs = createNodeFileSystemAdapter();
  285. try {
  286. await writeFile(path, await readFile(GB2312_FIXTURE_PATH));
  287. const read = await fs.readTextFile({ path });
  288. await fs.writeTextFile({
  289. path,
  290. content: read.content.replace("return 1", "return 2"),
  291. encoding: read.encoding,
  292. lineEndings: read.lineEndings,
  293. expectedRevision: read.revision,
  294. });
  295. const raw = await readFile(path);
  296. expect(raw.includes(GB2312_THIS_IS_BYTES)).toBe(true);
  297. expect(raw.includes(Buffer.from("这是", "utf8"))).toBe(false);
  298. const reread = await fs.readTextFile({ path });
  299. expect(reread.encoding).toBe("gb2312");
  300. expect(reread.content).toContain("return 2");
  301. } finally {
  302. await rm(dir, { force: true, recursive: true });
  303. }
  304. });
  305. it("rejects unencodable GB2312 writes without modifying the file", async () => {
  306. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-gb2312-reject-"));
  307. const path = join(dir, "gb2312.js");
  308. const fs = createNodeFileSystemAdapter();
  309. try {
  310. const originalBytes = await readFile(GB2312_FIXTURE_PATH);
  311. await writeFile(path, originalBytes);
  312. const read = await fs.readTextFile({ path });
  313. await expect(
  314. fs.writeTextFile({
  315. path,
  316. content: `${read.content}\n// 🙂\n`,
  317. encoding: read.encoding,
  318. lineEndings: read.lineEndings,
  319. }),
  320. ).rejects.toMatchObject({
  321. code: "unsupported",
  322. });
  323. expect(await readFile(path)).toEqual(originalBytes);
  324. } finally {
  325. await rm(dir, { force: true, recursive: true });
  326. }
  327. });
  328. it("rejects relative file system adapter paths", async () => {
  329. const fs = createNodeFileSystemAdapter();
  330. await expect(fs.readTextFile({ path: "relative.txt" })).rejects.toMatchObject({
  331. code: "invalid_path",
  332. });
  333. await expect(
  334. fs.writeTextFile({
  335. path: "relative.txt",
  336. content: "nope",
  337. }),
  338. ).rejects.toMatchObject({
  339. code: "invalid_path",
  340. });
  341. });
  342. it("lists directory entries through FileSystemPort", async () => {
  343. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-list-"));
  344. const fs = createNodeFileSystemAdapter();
  345. try {
  346. await mkdir(join(dir, "src"));
  347. await writeFile(join(dir, "README.md"), "docs", "utf8");
  348. const listed = await fs.listDirectory({ path: dir });
  349. expect(listed.path).toBe(dir);
  350. expect(listed.numEntries).toBe(2);
  351. expect(listed.entries).toEqual([
  352. {
  353. kind: "file",
  354. name: "README.md",
  355. path: join(dir, "README.md"),
  356. },
  357. {
  358. kind: "directory",
  359. name: "src",
  360. path: join(dir, "src"),
  361. },
  362. ]);
  363. await expect(fs.listDirectory({ path: join(dir, "README.md") })).rejects.toMatchObject({
  364. code: "not_file",
  365. });
  366. } finally {
  367. await rm(dir, { force: true, recursive: true });
  368. }
  369. });
  370. it("searches files and text through FileSystemPort", async () => {
  371. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-search-"));
  372. const fs = createNodeFileSystemAdapter();
  373. try {
  374. await mkdir(join(dir, "src"), { recursive: true });
  375. await writeFile(join(dir, "src", "index.ts"), "export const answer = 42;\n");
  376. await writeFile(join(dir, "src", "index.test.ts"), "expect(answer).toBe(42);\n");
  377. await writeFile(join(dir, "README.md"), "# answer\n");
  378. const files = await fs.searchFiles({
  379. path: dir,
  380. pattern: "**/*.ts",
  381. maxResults: 10,
  382. });
  383. const grepFiles = await fs.searchText({
  384. path: dir,
  385. pattern: "answer",
  386. glob: "**/*.ts",
  387. });
  388. const grepContent = await fs.searchText({
  389. path: dir,
  390. pattern: "answer",
  391. outputMode: "content",
  392. headLimit: 1,
  393. });
  394. expect(files.files.map((path) => path.slice(dir.length + 1)).sort()).toEqual([
  395. "src/index.test.ts",
  396. "src/index.ts",
  397. ]);
  398. expect(grepFiles.files.map((path) => path.slice(dir.length + 1)).sort()).toEqual([
  399. "src/index.test.ts",
  400. "src/index.ts",
  401. ]);
  402. expect(grepFiles.numMatches).toBe(2);
  403. expect(grepContent.entries).toHaveLength(1);
  404. expect(grepContent.appliedLimit).toBe(1);
  405. expect(grepContent.truncated).toBe(true);
  406. } finally {
  407. await rm(dir, { force: true, recursive: true });
  408. }
  409. });
  410. it("searches text with bundled ripgrep semantics", async () => {
  411. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-ripgrep-"));
  412. const fs = createNodeFileSystemAdapter();
  413. try {
  414. await mkdir(join(dir, "src"), { recursive: true });
  415. await writeFile(
  416. join(dir, "src", "app.ts"),
  417. [
  418. "before",
  419. "Needle",
  420. "after",
  421. 'const option = "-flagged";',
  422. "start",
  423. "middle",
  424. "end",
  425. "",
  426. ].join("\n"),
  427. );
  428. await writeFile(join(dir, "src", "view.tsx"), "needle in tsx\n");
  429. await writeFile(join(dir, "src", "view.js"), "needle in js\n");
  430. await writeFile(join(dir, "README.md"), "Needle in docs\n");
  431. const counts = await fs.searchText({
  432. path: dir,
  433. pattern: "needle",
  434. outputMode: "count",
  435. ignoreCase: true,
  436. glob: "src/**/*",
  437. type: "ts",
  438. headLimit: 0,
  439. });
  440. const dashed = await fs.searchText({
  441. path: dir,
  442. pattern: "-flagged",
  443. outputMode: "content",
  444. glob: "**/*.ts",
  445. });
  446. const context = await fs.searchText({
  447. path: join(dir, "src", "app.ts"),
  448. pattern: "Needle",
  449. outputMode: "content",
  450. context: 1,
  451. });
  452. const onlyMatching = await fs.searchText({
  453. path: join(dir, "src", "app.ts"),
  454. pattern: "Needle",
  455. outputMode: "content",
  456. onlyMatching: true,
  457. });
  458. const multiline = await fs.searchText({
  459. path: join(dir, "src", "app.ts"),
  460. pattern: "start\nmiddle",
  461. outputMode: "content",
  462. multiline: true,
  463. });
  464. expect(counts.numMatches).toBe(2);
  465. expect(counts.entries.map((entry) => entry.path.slice(dir.length + 1)).sort()).toEqual([
  466. "src/app.ts",
  467. "src/view.tsx",
  468. ]);
  469. expect(dashed.numMatches).toBe(1);
  470. expect(dashed.entries[0]).toMatchObject({
  471. path: join(dir, "src", "app.ts"),
  472. lineNumber: 4,
  473. matched: true,
  474. });
  475. expect(context.entries.map((entry) => [entry.lineNumber, entry.text, entry.matched])).toEqual(
  476. [
  477. [1, "before", false],
  478. [2, "Needle", true],
  479. [3, "after", false],
  480. ],
  481. );
  482. expect(onlyMatching.entries.map((entry) => entry.text)).toEqual(["Needle"]);
  483. expect(multiline.numMatches).toBe(1);
  484. expect(multiline.entries[0]).toMatchObject({
  485. path: join(dir, "src", "app.ts"),
  486. lineNumber: 5,
  487. matched: true,
  488. });
  489. } finally {
  490. await rm(dir, { force: true, recursive: true });
  491. }
  492. });
  493. it("maps ripgrep regex failures to FileSystemPort invalid_pattern errors", async () => {
  494. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-ripgrep-error-"));
  495. const fs = createNodeFileSystemAdapter();
  496. try {
  497. await writeFile(join(dir, "file.txt"), "hello\n");
  498. await expect(
  499. fs.searchText({
  500. path: dir,
  501. pattern: "[",
  502. outputMode: "content",
  503. }),
  504. ).rejects.toMatchObject({
  505. code: "invalid_pattern",
  506. });
  507. } finally {
  508. await rm(dir, { force: true, recursive: true });
  509. }
  510. });
  511. it("ignores onlyMatching outside content mode in JavaScript grep fallback", async () => {
  512. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-grep-only-matching-"));
  513. const fs = createNodeFileSystemAdapter({ textSearchEngine: "javascript" });
  514. try {
  515. await writeFile(join(dir, "file.txt"), "foo foo\nfoo\n");
  516. const result = await fs.searchText({
  517. path: dir,
  518. pattern: "foo",
  519. outputMode: "count",
  520. onlyMatching: true,
  521. headLimit: 0,
  522. });
  523. expect(result.numMatches).toBe(2);
  524. expect(result.entries).toEqual([
  525. {
  526. path: join(dir, "file.txt"),
  527. count: 2,
  528. },
  529. ]);
  530. } finally {
  531. await rm(dir, { force: true, recursive: true });
  532. }
  533. });
  534. it("keeps context lines and match-line count for onlyMatching content", async () => {
  535. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-grep-only-matching-context-"));
  536. const engines = [
  537. createNodeFileSystemAdapter(),
  538. createNodeFileSystemAdapter({ textSearchEngine: "javascript" }),
  539. ];
  540. try {
  541. const path = join(dir, "file.txt");
  542. await writeFile(path, "before\nfoo foo\nafter\n");
  543. for (const fs of engines) {
  544. const result = await fs.searchText({
  545. path,
  546. pattern: "foo",
  547. outputMode: "content",
  548. onlyMatching: true,
  549. context: 1,
  550. headLimit: 0,
  551. });
  552. expect(result.numMatches).toBe(1);
  553. expect(
  554. result.entries.map((entry) => [entry.lineNumber, entry.text, entry.matched]),
  555. ).toEqual([
  556. [1, "before", false],
  557. [2, "foo", true],
  558. [2, "foo", true],
  559. [3, "after", false],
  560. ]);
  561. }
  562. } finally {
  563. await rm(dir, { force: true, recursive: true });
  564. }
  565. });
  566. it("expands multiline onlyMatching content into line entries", async () => {
  567. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-grep-multiline-only-matching-"));
  568. const engines = [
  569. createNodeFileSystemAdapter(),
  570. createNodeFileSystemAdapter({ textSearchEngine: "javascript" }),
  571. ];
  572. const cases = [
  573. {
  574. content: "before\nstart\nmiddle\nend\n",
  575. pattern: "start\nmiddle",
  576. expected: [
  577. [2, "start", true],
  578. [3, "middle", true],
  579. ],
  580. },
  581. {
  582. content: "before\nstart\n\nend\n",
  583. pattern: "start\n\nend",
  584. expected: [
  585. [2, "start", true],
  586. [4, "end", true],
  587. ],
  588. },
  589. {
  590. content: "foo\rbar\r",
  591. pattern: "foo\rbar",
  592. expected: [[1, "foo\rbar", true]],
  593. },
  594. {
  595. content: "before\r\nfoo\r\nbar\r\nafter\r\n",
  596. pattern: "foo\r\nbar",
  597. expected: [
  598. [2, "foo", true],
  599. [3, "bar", true],
  600. ],
  601. },
  602. {
  603. content: "zero\nbefore\nstart\nmiddle\nafter\n",
  604. pattern: "start\nmiddle",
  605. context: 1,
  606. expected: [
  607. [2, "before", false],
  608. [3, "start", true],
  609. [4, "middle", true],
  610. [5, "after", false],
  611. ],
  612. },
  613. ] as const;
  614. try {
  615. for (const [index, testCase] of cases.entries()) {
  616. const path = join(dir, `file-${index}.txt`);
  617. await writeFile(path, testCase.content);
  618. for (const fs of engines) {
  619. const result = await fs.searchText({
  620. path,
  621. pattern: testCase.pattern,
  622. outputMode: "content",
  623. multiline: true,
  624. onlyMatching: true,
  625. context: testCase.context,
  626. headLimit: 0,
  627. });
  628. expect(result.numMatches).toBe(1);
  629. expect(
  630. result.entries.map((entry) => [entry.lineNumber, entry.text, entry.matched]),
  631. ).toEqual(testCase.expected);
  632. }
  633. }
  634. } finally {
  635. await rm(dir, { force: true, recursive: true });
  636. }
  637. });
  638. it("keeps zero-length onlyMatching entries aligned between ripgrep and fallback", async () => {
  639. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-grep-zero-only-matching-"));
  640. const engines = [
  641. createNodeFileSystemAdapter(),
  642. createNodeFileSystemAdapter({ textSearchEngine: "javascript" }),
  643. ];
  644. try {
  645. const path = join(dir, "file.txt");
  646. await writeFile(path, "foo\nbar\n");
  647. for (const fs of engines) {
  648. const result = await fs.searchText({
  649. path,
  650. pattern: "^",
  651. outputMode: "content",
  652. onlyMatching: true,
  653. headLimit: 0,
  654. });
  655. expect(result.numMatches).toBe(2);
  656. expect(
  657. result.entries.map((entry) => [entry.lineNumber, entry.text, entry.matched]),
  658. ).toEqual([
  659. [1, "", true],
  660. [2, "", true],
  661. ]);
  662. }
  663. } finally {
  664. await rm(dir, { force: true, recursive: true });
  665. }
  666. });
  667. it("maps cancelled grep searches to FileSystemPort cancelled errors", async () => {
  668. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-grep-cancel-"));
  669. const fs = createNodeFileSystemAdapter({ textSearchEngine: "javascript" });
  670. const controller = new AbortController();
  671. try {
  672. await writeFile(join(dir, "file.txt"), "hello\n");
  673. controller.abort();
  674. await expect(
  675. fs.searchText(
  676. {
  677. path: dir,
  678. pattern: "hello",
  679. },
  680. { signal: controller.signal },
  681. ),
  682. ).rejects.toMatchObject({
  683. code: "cancelled",
  684. });
  685. } finally {
  686. await rm(dir, { force: true, recursive: true });
  687. }
  688. });
  689. it("terminates bundled ripgrep worker when grep search is cancelled", async () => {
  690. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-ripgrep-cancel-"));
  691. const fs = createNodeFileSystemAdapter();
  692. const controller = new AbortController();
  693. const worker = new HangingRipgrepWorker();
  694. let resolveStarted: () => void = () => undefined;
  695. const started = new Promise<void>((resolve) => {
  696. resolveStarted = resolve;
  697. });
  698. const restoreWorkerFactory = setRipgrepWorkerFactoryForTests(() => {
  699. resolveStarted();
  700. return worker;
  701. });
  702. try {
  703. await writeFile(join(dir, "file.txt"), "hello\n");
  704. const pending = fs.searchText(
  705. {
  706. path: dir,
  707. pattern: "hello",
  708. },
  709. { signal: controller.signal },
  710. );
  711. await started;
  712. controller.abort(new Error("user stopped"));
  713. await expect(pending).rejects.toMatchObject({
  714. code: "cancelled",
  715. });
  716. expect(worker.terminated).toBe(true);
  717. } finally {
  718. restoreWorkerFactory();
  719. await rm(dir, { force: true, recursive: true });
  720. }
  721. });
  722. it("terminates bundled ripgrep worker when grep search times out", async () => {
  723. const dir = await mkdtemp(join(tmpdir(), "zcode-fs-ripgrep-timeout-"));
  724. const fs = createNodeFileSystemAdapter();
  725. const worker = new HangingRipgrepWorker();
  726. let resolveStarted: () => void = () => undefined;
  727. const started = new Promise<void>((resolve) => {
  728. resolveStarted = resolve;
  729. });
  730. const restoreWorkerFactory = setRipgrepWorkerFactoryForTests(() => {
  731. resolveStarted();
  732. return worker;
  733. });
  734. const restoreTimeout = setRipgrepTimeoutMsForTests(10);
  735. try {
  736. await writeFile(join(dir, "file.txt"), "hello\n");
  737. const pending = fs.searchText({
  738. path: dir,
  739. pattern: "hello",
  740. });
  741. await started;
  742. await expect(pending).rejects.toMatchObject({
  743. code: "io_error",
  744. message: expect.stringContaining("timed out"),
  745. });
  746. expect(worker.terminated).toBe(true);
  747. } finally {
  748. restoreTimeout();
  749. restoreWorkerFactory();
  750. await rm(dir, { force: true, recursive: true });
  751. }
  752. });
  753. it("resolves context sources outside core", async () => {
  754. const dir = await mkdtemp(join(tmpdir(), "zcode-context-port-"));
  755. const adapter = createNodeContextSourceAdapter({ env: { HOME: dir, SHELL: "/bin/zsh" } });
  756. try {
  757. await writeFile(join(dir, "package.json"), JSON.stringify({ scripts: { test: "vitest" } }));
  758. await writeFile(join(dir, "pnpm-lock.yaml"), "");
  759. await writeFile(join(dir, "AGENTS.md"), "project instructions");
  760. await mkdir(join(dir, ".git"));
  761. const snapshot = await adapter.resolveContextSources({
  762. workingDirectory: dir,
  763. userInstructions: {
  764. workingDirectory: dir,
  765. },
  766. });
  767. expect(snapshot.envInfo.cwd).toBe(dir);
  768. expect(snapshot.envInfo.shell).toBe("zsh");
  769. // 有 .git 就是仓库,与项目根同一个事实;原来按 git 进程判定,空 .git 被当成"不是仓库"。
  770. expect(snapshot.envInfo.isGitRepository).toBe(true);
  771. expect(snapshot.userInstructions?.content).toBe("project instructions");
  772. expect(snapshot.projectContext).toMatchObject({
  773. type: "node",
  774. packageManager: "pnpm",
  775. scripts: { test: "vitest" },
  776. });
  777. } finally {
  778. await rm(dir, { force: true, recursive: true });
  779. }
  780. });
  781. it("uses the effective Bash shell display name when provided by runtime", async () => {
  782. const dir = await mkdtemp(join(tmpdir(), "zcode-context-shell-"));
  783. const adapter = createNodeContextSourceAdapter({ env: { HOME: dir, SHELL: "/bin/fish" } });
  784. try {
  785. const snapshot = await adapter.resolveContextSources({
  786. effectiveShellDisplayName: "zsh",
  787. workingDirectory: dir,
  788. });
  789. expect(snapshot.envInfo.shell).toBe("zsh");
  790. } finally {
  791. await rm(dir, { force: true, recursive: true });
  792. }
  793. });
  794. it("falls back to ~/.zcode/AGENTS.md when workspace AGENTS.md is absent", async () => {
  795. const home = await mkdtemp(join(tmpdir(), "zcode-context-home-"));
  796. const workspace = join(home, "workspace");
  797. const nestedCwd = join(workspace, "packages", "app");
  798. const adapter = createNodeContextSourceAdapter({
  799. env: { HOME: home, SHELL: "/bin/zsh" },
  800. });
  801. try {
  802. await mkdir(nestedCwd, { recursive: true });
  803. await mkdir(join(workspace, ".git"));
  804. await mkdir(join(home, ".zcode"), { recursive: true });
  805. await writeFile(join(home, ".zcode", "AGENTS.md"), "default user instructions");
  806. const fallbackSnapshot = await adapter.resolveContextSources({
  807. workingDirectory: nestedCwd,
  808. userInstructions: {
  809. workingDirectory: nestedCwd,
  810. },
  811. });
  812. expect(fallbackSnapshot.userInstructions?.filePath).toBe(join(home, ".zcode", "AGENTS.md"));
  813. expect(fallbackSnapshot.userInstructions?.content).toBe("default user instructions");
  814. expect(fallbackSnapshot.userInstructions?.sources).toEqual([
  815. expect.objectContaining({
  816. scope: "user",
  817. filePath: join(home, ".zcode", "AGENTS.md"),
  818. content: "default user instructions",
  819. }),
  820. ]);
  821. await writeFile(join(workspace, "AGENTS.md"), "project instructions");
  822. const projectSnapshot = await adapter.resolveContextSources({
  823. workingDirectory: nestedCwd,
  824. userInstructions: {
  825. workingDirectory: nestedCwd,
  826. },
  827. });
  828. expect(projectSnapshot.userInstructions?.filePath).toBe(join(workspace, "AGENTS.md"));
  829. expect(projectSnapshot.userInstructions?.content).toBe(
  830. "default user instructions\n\nproject instructions",
  831. );
  832. expect(projectSnapshot.userInstructions?.sources).toEqual([
  833. expect.objectContaining({
  834. scope: "user",
  835. filePath: join(home, ".zcode", "AGENTS.md"),
  836. content: "default user instructions",
  837. }),
  838. expect.objectContaining({
  839. scope: "workspace",
  840. filePath: join(workspace, "AGENTS.md"),
  841. content: "project instructions",
  842. }),
  843. ]);
  844. } finally {
  845. await rm(home, { force: true, recursive: true });
  846. }
  847. });
  848. it("uses USERPROFILE for default user instructions when HOME is absent", async () => {
  849. const home = await mkdtemp(join(tmpdir(), "zcode-context-userprofile-"));
  850. const workspace = join(home, "workspace");
  851. const adapter = createNodeContextSourceAdapter({
  852. env: { USERPROFILE: home, SHELL: "/bin/zsh" },
  853. });
  854. try {
  855. await mkdir(workspace, { recursive: true });
  856. await mkdir(join(workspace, ".git"));
  857. await mkdir(join(home, ".zcode"), { recursive: true });
  858. await writeFile(join(home, ".zcode", "AGENTS.md"), "userprofile instructions");
  859. const snapshot = await adapter.resolveContextSources({
  860. workingDirectory: workspace,
  861. userInstructions: {
  862. workingDirectory: workspace,
  863. },
  864. });
  865. expect(snapshot.userInstructions?.filePath).toBe(join(home, ".zcode", "AGENTS.md"));
  866. expect(snapshot.userInstructions?.content).toBe("userprofile instructions");
  867. } finally {
  868. await rm(home, { force: true, recursive: true });
  869. }
  870. });
  871. it("does not use the default AGENTS.md fallback for custom priority files", async () => {
  872. const home = await mkdtemp(join(tmpdir(), "zcode-context-priority-"));
  873. const workspace = join(home, "workspace");
  874. const adapter = createNodeContextSourceAdapter({
  875. env: { HOME: home, SHELL: "/bin/zsh" },
  876. });
  877. try {
  878. await mkdir(workspace, { recursive: true });
  879. await mkdir(join(workspace, ".git"));
  880. await mkdir(join(home, ".zcode"), { recursive: true });
  881. await writeFile(join(home, ".zcode", "AGENTS.md"), "default user instructions");
  882. const snapshot = await adapter.resolveContextSources({
  883. workingDirectory: workspace,
  884. userInstructions: {
  885. workingDirectory: workspace,
  886. priorityFiles: ["CUSTOM.md"],
  887. },
  888. });
  889. expect(snapshot.userInstructions).toBeUndefined();
  890. } finally {
  891. await rm(home, { force: true, recursive: true });
  892. }
  893. });
  894. it("captures a session-start git snapshot when the workspace is a repository", async () => {
  895. const dir = await mkdtemp(join(tmpdir(), "zcode-context-git-"));
  896. const adapter = createNodeContextSourceAdapter({ env: { SHELL: "/bin/zsh" } });
  897. try {
  898. if (!(await hasGitCli())) {
  899. return;
  900. }
  901. await runGit(dir, ["init"]);
  902. await runGit(dir, ["checkout", "-B", "main"]);
  903. await runGit(dir, ["config", "user.name", "ZCode Tester"]);
  904. await runGit(dir, ["config", "user.email", "tester@example.com"]);
  905. await writeFile(join(dir, "tracked.txt"), "first\n");
  906. await runGit(dir, ["add", "tracked.txt"]);
  907. await runGit(dir, ["commit", "-m", "first commit"]);
  908. await writeFile(join(dir, "tracked.txt"), "first\nsecond\n");
  909. await runGit(dir, ["add", "tracked.txt"]);
  910. await runGit(dir, ["commit", "-m", "second commit"]);
  911. await writeFile(join(dir, "tracked.txt"), "dirty\n");
  912. await writeFile(join(dir, "untracked.txt"), "new file\n");
  913. const snapshot = await adapter.resolveContextSources({
  914. workingDirectory: dir,
  915. });
  916. expect(snapshot.envInfo.shell).toBe("zsh");
  917. expect(snapshot.envInfo.isGitRepository).toBe(true);
  918. expect(snapshot.envInfo.gitBranch).toBe("main");
  919. expect(snapshot.envInfo.gitMainBranch).toBe("main");
  920. expect(snapshot.envInfo.gitUser).toBe("ZCode Tester");
  921. expect(snapshot.envInfo.gitStatus).toBe("dirty");
  922. expect(snapshot.envInfo.gitStatusLines).toEqual(
  923. expect.arrayContaining(["M tracked.txt", "?? untracked.txt"]),
  924. );
  925. expect(snapshot.envInfo.recentCommits?.length).toBe(2);
  926. expect(snapshot.envInfo.recentCommits?.[0]).toContain("second commit");
  927. expect(snapshot.envInfo.recentCommits?.[1]).toContain("first commit");
  928. } finally {
  929. await rm(dir, { force: true, recursive: true });
  930. }
  931. });
  932. it("keeps git status entries until the 2k character boundary", async () => {
  933. const dir = await mkdtemp(join(tmpdir(), "zcode-context-git-large-"));
  934. const adapter = createNodeContextSourceAdapter({ env: { SHELL: "/bin/zsh" } });
  935. try {
  936. if (!(await hasGitCli())) {
  937. return;
  938. }
  939. await runGit(dir, ["init"]);
  940. await runGit(dir, ["checkout", "-B", "main"]);
  941. for (let index = 0; index < 30; index += 1) {
  942. await writeFile(join(dir, `file-${String(index).padStart(3, "0")}.txt`), "dirty\n");
  943. }
  944. const snapshot = await adapter.resolveContextSources({
  945. workingDirectory: dir,
  946. });
  947. const statusLines = snapshot.envInfo.gitStatusLines ?? [];
  948. expect(snapshot.envInfo.gitStatus).toBe("dirty");
  949. expect(statusLines).toHaveLength(30);
  950. expect(statusLines[0]).toBe("?? file-000.txt");
  951. expect(statusLines[19]).toBe("?? file-019.txt");
  952. expect(statusLines[29]).toBe("?? file-029.txt");
  953. expect(statusLines.join("\n")).not.toContain("git status truncated after 20 entries");
  954. } finally {
  955. await rm(dir, { force: true, recursive: true });
  956. }
  957. });
  958. it("truncates git status by 2k characters with the truncation prompt text", async () => {
  959. const dir = await mkdtemp(join(tmpdir(), "zcode-context-git-2k-"));
  960. const adapter = createNodeContextSourceAdapter({ env: { SHELL: "/bin/zsh" } });
  961. try {
  962. if (!(await hasGitCli())) {
  963. return;
  964. }
  965. await runGit(dir, ["init"]);
  966. await runGit(dir, ["checkout", "-B", "main"]);
  967. for (let index = 0; index < 100; index += 1) {
  968. await writeFile(
  969. join(dir, `file-${String(index).padStart(3, "0")}-${"x".repeat(32)}.txt`),
  970. "dirty\n",
  971. );
  972. }
  973. const snapshot = await adapter.resolveContextSources({
  974. workingDirectory: dir,
  975. });
  976. const statusText = (snapshot.envInfo.gitStatusLines ?? []).join("\n");
  977. expect(snapshot.envInfo.gitStatus).toBe("dirty");
  978. expect(statusText).toContain("... (truncated because it exceeds 2k characters.");
  979. expect(statusText).toContain('run "git status" using Bash');
  980. expect(statusText).not.toContain("git status truncated after 20 entries");
  981. expect(statusText.length).toBeGreaterThan(2000);
  982. } finally {
  983. await rm(dir, { force: true, recursive: true });
  984. }
  985. });
  986. it("reads the unborn branch from a nested working directory", async () => {
  987. const dir = await mkdtemp(join(tmpdir(), "zcode-context-git-unborn-"));
  988. const adapter = createNodeContextSourceAdapter();
  989. try {
  990. await runGit(dir, ["init"]);
  991. await runGit(dir, ["checkout", "-B", "topic/no-commits"]);
  992. const nested = join(dir, "nested");
  993. await mkdir(nested);
  994. const snapshot = await adapter.resolveContextSources({ workingDirectory: nested });
  995. expect(snapshot.envInfo.gitBranch).toBe("topic/no-commits");
  996. expect(snapshot.envInfo.recentCommits).toEqual([]);
  997. } finally {
  998. await rm(dir, { force: true, recursive: true });
  999. }
  1000. });
  1001. it("reads an unborn branch in a linked worktree instead of the parent repository HEAD", async () => {
  1002. const dir = await mkdtemp(join(tmpdir(), "zcode-context-git-worktree-"));
  1003. const adapter = createNodeContextSourceAdapter();
  1004. try {
  1005. await runGit(dir, ["init"]);
  1006. await runGit(dir, ["checkout", "-B", "main"]);
  1007. await runGit(dir, [
  1008. "-c",
  1009. "user.name=Test",
  1010. "-c",
  1011. "user.email=test@example.com",
  1012. "-c",
  1013. "commit.gpgsign=false",
  1014. "commit",
  1015. "--allow-empty",
  1016. "-m",
  1017. "initial",
  1018. ]);
  1019. const worktree = join(dir, "linked");
  1020. await runGit(dir, ["worktree", "add", "-b", "topic/worktree", worktree]);
  1021. // 链接工作树切到还没有提交的分支:rev-parse --abbrev-ref HEAD 在这里以 128 退出、
  1022. // 回退成 HEAD;只有读本工作树符号引用的 symbolic-ref 能得到分支名。
  1023. // 父仓库仍在有提交的 main 上,结果不能是父仓库的 HEAD。
  1024. await runGit(worktree, ["checkout", "--orphan", "topic/unborn-worktree"]);
  1025. const snapshot = await adapter.resolveContextSources({ workingDirectory: worktree });
  1026. expect(snapshot.envInfo.gitBranch).toBe("topic/unborn-worktree");
  1027. expect(snapshot.envInfo.recentCommits).toEqual([]);
  1028. } finally {
  1029. await rm(dir, { force: true, recursive: true });
  1030. }
  1031. });
  1032. it("uses the main branch fallback when origin HEAD is unavailable", async () => {
  1033. const dir = await mkdtemp(join(tmpdir(), "zcode-context-git-main-fallback-"));
  1034. const adapter = createNodeContextSourceAdapter({ env: { SHELL: "/bin/zsh" } });
  1035. try {
  1036. if (!(await hasGitCli())) {
  1037. return;
  1038. }
  1039. await runGit(dir, ["init"]);
  1040. await runGit(dir, ["checkout", "-B", "feature-only"]);
  1041. await runGit(dir, ["config", "user.name", "ZCode Tester"]);
  1042. await runGit(dir, ["config", "user.email", "tester@example.com"]);
  1043. await writeFile(join(dir, "tracked.txt"), "first\n");
  1044. await runGit(dir, ["add", "tracked.txt"]);
  1045. await runGit(dir, ["commit", "-m", "first commit"]);
  1046. const snapshot = await adapter.resolveContextSources({
  1047. workingDirectory: dir,
  1048. });
  1049. expect(snapshot.envInfo.gitBranch).toBe("feature-only");
  1050. expect(snapshot.envInfo.gitMainBranch).toBe("main");
  1051. } finally {
  1052. await rm(dir, { force: true, recursive: true });
  1053. }
  1054. });
  1055. it("reports HEAD as the current branch in detached checkout", async () => {
  1056. const dir = await mkdtemp(join(tmpdir(), "zcode-context-git-detached-"));
  1057. const adapter = createNodeContextSourceAdapter({ env: { SHELL: "/bin/zsh" } });
  1058. try {
  1059. if (!(await hasGitCli())) {
  1060. return;
  1061. }
  1062. await runGit(dir, ["init"]);
  1063. await runGit(dir, ["checkout", "-B", "main"]);
  1064. await runGit(dir, ["config", "user.name", "ZCode Tester"]);
  1065. await runGit(dir, ["config", "user.email", "tester@example.com"]);
  1066. await writeFile(join(dir, "tracked.txt"), "first\n");
  1067. await runGit(dir, ["add", "tracked.txt"]);
  1068. await runGit(dir, ["commit", "-m", "first commit"]);
  1069. await runGit(dir, ["checkout", "--detach", "HEAD"]);
  1070. const snapshot = await adapter.resolveContextSources({
  1071. workingDirectory: dir,
  1072. });
  1073. expect(snapshot.envInfo.gitBranch).toBe("HEAD");
  1074. } finally {
  1075. await rm(dir, { force: true, recursive: true });
  1076. }
  1077. });
  1078. });
  1079. async function hasGitCli(): Promise<boolean> {
  1080. try {
  1081. await execFile("git", ["--version"], {
  1082. encoding: "utf8",
  1083. windowsHide: true,
  1084. });
  1085. return true;
  1086. } catch {
  1087. return false;
  1088. }
  1089. }
  1090. async function runGit(cwd: string, args: string[]): Promise<void> {
  1091. await execFile("git", args, {
  1092. cwd,
  1093. encoding: "utf8",
  1094. windowsHide: true,
  1095. });
  1096. }