| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import { describe, expect, it } from "vitest";
- import {
- createReadFileStatePathKey,
- findLatestReadFileState,
- normalizeReadFileStateMtimeMs,
- setReadFileStateEntry,
- } from "../src/tool/read-file-state.js";
- import type { ReadFileStateMap } from "../src/tool/types.js";
- describe("read file state path keys", () => {
- it("treats equivalent Windows drive-letter paths as the same read-state file", () => {
- expect(createReadFileStatePathKey("c:/zcode-readwrite-repro/repro.txt", "win32")).toBe(
- createReadFileStatePathKey("C:\\zcode-readwrite-repro\\repro.txt", "win32"),
- );
- });
- it("normalizes Windows extended drive prefixes before comparing read-state files", () => {
- expect(createReadFileStatePathKey("\\\\?\\c:\\Repo\\File.txt", "win32")).toBe(
- createReadFileStatePathKey("C:\\Repo\\File.txt", "win32"),
- );
- });
- it("preserves non-drive Windows device namespace paths", () => {
- expect(createReadFileStatePathKey("\\\\?\\Volume{123}\\Repo\\File.txt", "win32")).toBe(
- "\\\\?\\Volume{123}\\Repo\\File.txt",
- );
- expect(
- createReadFileStatePathKey(
- "\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\File.txt",
- "win32",
- ),
- ).toBe("\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\File.txt");
- });
- it("normalizes Windows POSIX drive aliases", () => {
- expect(createReadFileStatePathKey("/c/Repo/File.txt", "win32")).toBe(
- createReadFileStatePathKey("C:\\Repo\\File.txt", "win32"),
- );
- });
- it("does not normalize cygdrive aliases outside the read-state path key", () => {
- expect(createReadFileStatePathKey("/cygdrive/c/Repo/File.txt", "win32")).not.toBe(
- createReadFileStatePathKey("C:\\Repo\\File.txt", "win32"),
- );
- });
- it("does not case-fold Windows path segments without a canonical filesystem identity", () => {
- expect(createReadFileStatePathKey("C:\\Repo\\File.txt", "win32")).not.toBe(
- createReadFileStatePathKey("C:\\repo\\File.txt", "win32"),
- );
- });
- it("does not case-fold non-Windows paths", () => {
- expect(createReadFileStatePathKey("/tmp/ZCode/File.txt", "linux")).not.toBe(
- createReadFileStatePathKey("/tmp/zcode/file.txt", "linux"),
- );
- });
- it("Unicode-normalizes paths on all platforms", () => {
- expect(createReadFileStatePathKey("/tmp/\u00e9.txt", "linux")).toBe(
- createReadFileStatePathKey("/tmp/e\u0301.txt", "linux"),
- );
- });
- });
- describe("read file state lookup", () => {
- it("keeps one entry per file; the newest view replaces the older one", () => {
- const olderFull = createEntry("c:/repo/latest.txt", "old", {
- isPartialView: false,
- readAt: new Date(1),
- });
- const newerRange = createEntry("C:\\repo\\latest.txt", "new slice", {
- isPartialView: false,
- limit: 1,
- offset: 2,
- readAt: new Date(2),
- });
- const state: ReadFileStateMap = new Map();
- setReadFileStateEntry(state, olderFull, "win32");
- setReadFileStateEntry(state, newerRange, "win32");
- // 同一文件的不同写法落到同一条;查找直接按路径取,不遍历整张表。
- expect(state.size).toBe(1);
- expect(findLatestReadFileState(state, "C:/repo/latest.txt", "win32")).toBe(newerRange);
- expect(findLatestReadFileState(state, "C:/repo/other.txt", "win32")).toBeUndefined();
- });
- it("normalizes mtime to integer milliseconds", () => {
- expect(normalizeReadFileStateMtimeMs(123.987)).toBe(123);
- expect(normalizeReadFileStateMtimeMs(undefined)).toBeUndefined();
- });
- });
- function createEntry(
- filePath: string,
- content: string,
- options: {
- isPartialView: boolean;
- limit?: number;
- offset?: number;
- readAt: Date;
- },
- ) {
- return {
- path: filePath,
- content,
- offset: options.offset,
- limit: options.limit,
- isPartialView: options.isPartialView,
- readAt: options.readAt,
- revisionId: `rev:${content.length}`,
- mtimeMs: 1,
- sizeBytes: content.length,
- };
- }
|