read-file-state.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createReadFileStatePathKey,
  4. findEditableReadFileState,
  5. findStrictFullReadFileState,
  6. normalizeReadFileStateMtimeMs,
  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("uses the newest same-file entry for Edit freshness", () => {
  59. const file = "/tmp/latest.txt";
  60. const olderFull = createEntry(file, "old", {
  61. isPartialView: false,
  62. readAt: new Date(1),
  63. });
  64. const newerPartial = createEntry(file, "new slice", {
  65. isPartialView: true,
  66. limit: 1,
  67. readAt: new Date(2),
  68. });
  69. const state: ReadFileStateMap = new Map([
  70. ["older", olderFull],
  71. ["newer", newerPartial],
  72. ]);
  73. expect(findEditableReadFileState(state, file, "linux")).toBe(newerPartial);
  74. });
  75. it("keeps Write on the newest strict full read only", () => {
  76. const file = "/tmp/full-only.txt";
  77. const olderFull = createEntry(file, "old", {
  78. isPartialView: false,
  79. readAt: new Date(1),
  80. });
  81. const newerPartial = createEntry(file, "new slice", {
  82. isPartialView: true,
  83. limit: 1,
  84. readAt: new Date(2),
  85. });
  86. const state: ReadFileStateMap = new Map([
  87. ["older", olderFull],
  88. ["newer", newerPartial],
  89. ]);
  90. expect(findStrictFullReadFileState(state, file, "linux")).toBe(olderFull);
  91. });
  92. it("normalizes mtime to integer milliseconds", () => {
  93. expect(normalizeReadFileStateMtimeMs(123.987)).toBe(123);
  94. expect(normalizeReadFileStateMtimeMs(undefined)).toBeUndefined();
  95. });
  96. });
  97. function createEntry(
  98. filePath: string,
  99. content: string,
  100. options: {
  101. isPartialView: boolean;
  102. limit?: number;
  103. readAt: Date;
  104. },
  105. ) {
  106. return {
  107. path: filePath,
  108. content,
  109. offset: undefined,
  110. limit: options.limit,
  111. isPartialView: options.isPartialView,
  112. readAt: options.readAt,
  113. revisionId: `rev:${content.length}`,
  114. mtimeMs: 1,
  115. sizeBytes: content.length,
  116. };
  117. }