read-file-state.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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("keeps cygdrive alias casing strict", () => {
  42. expect(createReadFileStatePathKey("/Cygdrive/c/Repo/File.txt", "win32")).not.toBe(
  43. createReadFileStatePathKey("C:\\Repo\\File.txt", "win32"),
  44. );
  45. });
  46. it("does not case-fold Windows path segments without a canonical filesystem identity", () => {
  47. expect(createReadFileStatePathKey("C:\\Repo\\File.txt", "win32")).not.toBe(
  48. createReadFileStatePathKey("C:\\repo\\File.txt", "win32"),
  49. );
  50. });
  51. it("does not case-fold non-Windows paths", () => {
  52. expect(createReadFileStatePathKey("/tmp/ZCode/File.txt", "linux")).not.toBe(
  53. createReadFileStatePathKey("/tmp/zcode/file.txt", "linux"),
  54. );
  55. });
  56. it("Unicode-normalizes paths on all platforms", () => {
  57. expect(createReadFileStatePathKey("/tmp/\u00e9.txt", "linux")).toBe(
  58. createReadFileStatePathKey("/tmp/e\u0301.txt", "linux"),
  59. );
  60. });
  61. });
  62. describe("read file state lookup", () => {
  63. it("uses the newest same-file entry for Edit freshness", () => {
  64. const file = "/tmp/latest.txt";
  65. const olderFull = createEntry(file, "old", {
  66. isPartialView: false,
  67. readAt: new Date(1),
  68. });
  69. const newerPartial = createEntry(file, "new slice", {
  70. isPartialView: true,
  71. limit: 1,
  72. readAt: new Date(2),
  73. });
  74. const state: ReadFileStateMap = new Map([
  75. ["older", olderFull],
  76. ["newer", newerPartial],
  77. ]);
  78. expect(findEditableReadFileState(state, file, "linux")).toBe(newerPartial);
  79. });
  80. it("keeps Write on the newest strict full read only", () => {
  81. const file = "/tmp/full-only.txt";
  82. const olderFull = createEntry(file, "old", {
  83. isPartialView: false,
  84. readAt: new Date(1),
  85. });
  86. const newerPartial = createEntry(file, "new slice", {
  87. isPartialView: true,
  88. limit: 1,
  89. readAt: new Date(2),
  90. });
  91. const state: ReadFileStateMap = new Map([
  92. ["older", olderFull],
  93. ["newer", newerPartial],
  94. ]);
  95. expect(findStrictFullReadFileState(state, file, "linux")).toBe(olderFull);
  96. });
  97. it("normalizes mtime to integer milliseconds", () => {
  98. expect(normalizeReadFileStateMtimeMs(123.987)).toBe(123);
  99. expect(normalizeReadFileStateMtimeMs(undefined)).toBeUndefined();
  100. });
  101. });
  102. function createEntry(
  103. filePath: string,
  104. content: string,
  105. options: {
  106. isPartialView: boolean;
  107. limit?: number;
  108. readAt: Date;
  109. },
  110. ) {
  111. return {
  112. path: filePath,
  113. content,
  114. offset: undefined,
  115. limit: options.limit,
  116. isPartialView: options.isPartialView,
  117. readAt: options.readAt,
  118. revisionId: `rev:${content.length}`,
  119. mtimeMs: 1,
  120. sizeBytes: content.length,
  121. };
  122. }