read-file-state.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createReadFileStatePathKey,
  4. findLatestReadFileState,
  5. normalizeReadFileStateMtimeMs,
  6. setReadFileStateEntry,
  7. } from "../src/tool/read-file-state.js";
  8. import type { ReadFileStateMap } from "../src/tool/types.js";
  9. describe("read file state path keys", () => {
  10. it("treats equivalent Windows drive-letter paths as the same read-state file", () => {
  11. expect(createReadFileStatePathKey("c:/zcode-readwrite-repro/repro.txt", "win32")).toBe(
  12. createReadFileStatePathKey("C:\\zcode-readwrite-repro\\repro.txt", "win32"),
  13. );
  14. });
  15. it("normalizes Windows extended drive prefixes before comparing read-state files", () => {
  16. expect(createReadFileStatePathKey("\\\\?\\c:\\Repo\\File.txt", "win32")).toBe(
  17. createReadFileStatePathKey("C:\\Repo\\File.txt", "win32"),
  18. );
  19. });
  20. it("preserves non-drive Windows device namespace paths", () => {
  21. expect(createReadFileStatePathKey("\\\\?\\Volume{123}\\Repo\\File.txt", "win32")).toBe(
  22. "\\\\?\\Volume{123}\\Repo\\File.txt",
  23. );
  24. expect(
  25. createReadFileStatePathKey(
  26. "\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\File.txt",
  27. "win32",
  28. ),
  29. ).toBe("\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\File.txt");
  30. });
  31. it("normalizes Windows POSIX drive aliases", () => {
  32. expect(createReadFileStatePathKey("/c/Repo/File.txt", "win32")).toBe(
  33. createReadFileStatePathKey("C:\\Repo\\File.txt", "win32"),
  34. );
  35. });
  36. it("does not normalize cygdrive aliases outside the read-state path key", () => {
  37. expect(createReadFileStatePathKey("/cygdrive/c/Repo/File.txt", "win32")).not.toBe(
  38. createReadFileStatePathKey("C:\\Repo\\File.txt", "win32"),
  39. );
  40. });
  41. it("does not case-fold Windows path segments without a canonical filesystem identity", () => {
  42. expect(createReadFileStatePathKey("C:\\Repo\\File.txt", "win32")).not.toBe(
  43. createReadFileStatePathKey("C:\\repo\\File.txt", "win32"),
  44. );
  45. });
  46. it("does not case-fold non-Windows paths", () => {
  47. expect(createReadFileStatePathKey("/tmp/ZCode/File.txt", "linux")).not.toBe(
  48. createReadFileStatePathKey("/tmp/zcode/file.txt", "linux"),
  49. );
  50. });
  51. it("Unicode-normalizes paths on all platforms", () => {
  52. expect(createReadFileStatePathKey("/tmp/\u00e9.txt", "linux")).toBe(
  53. createReadFileStatePathKey("/tmp/e\u0301.txt", "linux"),
  54. );
  55. });
  56. });
  57. describe("read file state lookup", () => {
  58. it("keeps one entry per file; the newest view replaces the older one", () => {
  59. const olderFull = createEntry("c:/repo/latest.txt", "old", {
  60. isPartialView: false,
  61. readAt: new Date(1),
  62. });
  63. const newerRange = createEntry("C:\\repo\\latest.txt", "new slice", {
  64. isPartialView: false,
  65. limit: 1,
  66. offset: 2,
  67. readAt: new Date(2),
  68. });
  69. const state: ReadFileStateMap = new Map();
  70. setReadFileStateEntry(state, olderFull, "win32");
  71. setReadFileStateEntry(state, newerRange, "win32");
  72. // 同一文件的不同写法落到同一条;查找直接按路径取,不遍历整张表。
  73. expect(state.size).toBe(1);
  74. expect(findLatestReadFileState(state, "C:/repo/latest.txt", "win32")).toBe(newerRange);
  75. expect(findLatestReadFileState(state, "C:/repo/other.txt", "win32")).toBeUndefined();
  76. });
  77. it("normalizes mtime to integer milliseconds", () => {
  78. expect(normalizeReadFileStateMtimeMs(123.987)).toBe(123);
  79. expect(normalizeReadFileStateMtimeMs(undefined)).toBeUndefined();
  80. });
  81. });
  82. function createEntry(
  83. filePath: string,
  84. content: string,
  85. options: {
  86. isPartialView: boolean;
  87. limit?: number;
  88. offset?: number;
  89. readAt: Date;
  90. },
  91. ) {
  92. return {
  93. path: filePath,
  94. content,
  95. offset: options.offset,
  96. limit: options.limit,
  97. isPartialView: options.isPartialView,
  98. readAt: options.readAt,
  99. revisionId: `rev:${content.length}`,
  100. mtimeMs: 1,
  101. sizeBytes: content.length,
  102. };
  103. }