summaryrefslogtreecommitdiffhomepage
path: root/src/features/markdown/adapter/text-file.test.ts
blob: 25972715ef7471653fa908c098652393dbe039eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { describe, expect, it, vi } from "vitest";
import type { TextFileState } from "../types";
import { createTextFileClient } from "./text-file";

const response = (data: unknown, status = 200) => new Response(JSON.stringify(data), { status });
const context = { conversationId: "c", cwd: "/project", computerId: null };
const file = (text = "# Plan") => ({
  path: "/project/a.md",
  text,
  format: "markdown",
  bytes: new TextEncoder().encode(text).length,
  maxBytes: 262144,
});
const flush = async () => {
  for (let i = 0; i < 20; i++) await Promise.resolve();
};
describe("context-bound live-file client", () => {
  it("preflights once, sends both guards and encodes literal path once; dedups repeated references", async () => {
    const fetcher = vi
      .fn<typeof fetch>()
      .mockImplementation(async (url) =>
        response(String(url).endsWith("-context") ? context : file()),
      );
    const client = createTextFileClient("/api", "c", fetcher);
    const a: TextFileState[] = [],
      b: TextFileState[] = [];
    const offA = client.subscribe("notes/100%.md", (s) => a.push(s));
    const offB = client.subscribe("notes/100%.md", (s) => b.push(s));
    await flush();
    expect(fetcher).toHaveBeenCalledTimes(2);
    const url = new URL(String(fetcher.mock.calls[1]?.[0]), "http://localhost");
    expect(Object.fromEntries(url.searchParams)).toEqual({
      path: "notes/100%.md",
      expectedCwd: "/project",
      expectedComputerId: "",
    });
    expect(fetcher.mock.calls[1]?.[1]?.cache).toBe("no-store");
    expect(a.at(-1)?.kind).toBe("ready");
    expect(b.at(-1)?.kind).toBe("ready");
    offA();
    expect(fetcher.mock.calls[1]?.[1]?.signal?.aborted).toBe(false);
    offB();
    expect(fetcher.mock.calls[1]?.[1]?.signal?.aborted).toBe(true);
    client.subscribe("notes/100%.md", () => {});
    await flush();
    expect(fetcher).toHaveBeenCalledTimes(4);
    client.dispose();
  });
  it("never reads a file before context, handles remote inheritance explicitly", async () => {
    const fetcher = vi
      .fn<typeof fetch>()
      .mockResolvedValue(response({ ...context, computerId: "remote" }));
    const client = createTextFileClient("", "c", fetcher);
    const states: TextFileState[] = [];
    client.subscribe("/same-local-path.md", (s) => states.push(s));
    await flush();
    expect(fetcher).toHaveBeenCalledTimes(1);
    expect(states.at(-1)).toMatchObject({
      kind: "error",
      message: expect.stringContaining("remote computers"),
    });
    client.dispose();
  });
  it.each([
    400, 404, 409, 413, 422,
  ])("reports HTTP %i then Retry refreshes authoritative context and live file", async (status) => {
    const fetcher = vi
      .fn<typeof fetch>()
      .mockResolvedValueOnce(response(context))
      .mockResolvedValueOnce(response({ error: `failure ${status}` }, status))
      .mockResolvedValueOnce(response(context))
      .mockResolvedValueOnce(response(file("changed")));
    const client = createTextFileClient("", "c", fetcher);
    const states: TextFileState[] = [];
    client.subscribe("a.md", (s) => states.push(s));
    await flush();
    expect(states.at(-1)).toEqual({ kind: "error", message: `failure ${status}` });
    client.refresh("a.md");
    await flush();
    expect(states.at(-1)).toMatchObject({ kind: "ready", file: { text: "changed" } });
    expect(fetcher).toHaveBeenCalledTimes(4);
    client.dispose();
  });
  it("guards late responses after refresh and disposal", async () => {
    let finish: (r: Response) => void = () => {};
    const fetcher = vi
      .fn<typeof fetch>()
      .mockResolvedValueOnce(response(context))
      .mockImplementationOnce(
        () =>
          new Promise((r) => {
            finish = r;
          }),
      )
      .mockResolvedValueOnce(response(context))
      .mockResolvedValueOnce(response(file("new")));
    const client = createTextFileClient("", "c", fetcher);
    const states: TextFileState[] = [];
    client.subscribe("a.md", (s) => states.push(s));
    await flush();
    client.refresh("a.md");
    await flush();
    finish(response(file("old")));
    await flush();
    expect(states.at(-1)).toMatchObject({ file: { text: "new" } });
    client.dispose();
    expect(fetcher.mock.calls[3]?.[1]?.signal?.aborted).toBe(true);
  });
  it("bounds active cache to32 files/8MiB source and concurrency to4, disposes queue", async () => {
    const fetcher = vi
      .fn<typeof fetch>()
      .mockImplementation(async (url) =>
        String(url).endsWith("-context") ? response(context) : new Promise(() => {}),
      );
    const client = createTextFileClient("", "c", fetcher);
    const overflow: TextFileState[] = [];
    for (let i = 0; i < 40; i++)
      client.subscribe(`${i}.md`, (s) => {
        if (i >= 32) overflow.push(s);
      });
    await flush();
    expect(fetcher).toHaveBeenCalledTimes(5);
    expect(overflow).toHaveLength(8);
    expect(overflow[0]).toMatchObject({ kind: "error" });
    client.dispose();
    for (const call of fetcher.mock.calls) expect(call[1]?.signal?.aborted).toBe(true);
  });
  it("rejects oversized or malformed success data instead of truncating", async () => {
    const fetcher = vi
      .fn<typeof fetch>()
      .mockResolvedValueOnce(response(context))
      .mockResolvedValueOnce(response(file("a".repeat(262145))));
    const client = createTextFileClient("", "c", fetcher);
    const states: TextFileState[] = [];
    client.subscribe("a.md", (s) => states.push(s));
    await flush();
    expect(states.at(-1)?.kind).toBe("error");
    client.dispose();
  });
});

it("invalidates every live document and reloads guards before publishing refreshed context", async () => {
  let generation = 0;
  const fetcher = vi.fn<typeof fetch>().mockImplementation(async (url) => {
    if (String(url).endsWith("-context"))
      return response({ ...context, cwd: generation ? "/new" : "/project" });
    const params = new URL(String(url), "http://localhost").searchParams;
    return response({
      ...file(generation ? "new" : "old"),
      path: `${params.get("expectedCwd")}/a.md`,
    });
  });
  const client = createTextFileClient("", "c", fetcher);
  const a: TextFileState[] = [],
    b: TextFileState[] = [];
  client.subscribe("a.md", (state) => a.push(state));
  client.subscribe("b.md", (state) => b.push(state));
  await flush();
  generation++;
  client.refresh("a.md");
  expect(a.at(-1)?.kind).toBe("loading");
  expect(b.at(-1)?.kind).toBe("loading");
  await flush();
  expect(a.at(-1)).toMatchObject({ kind: "ready", file: { path: "/new/a.md", text: "new" } });
  expect(b.at(-1)).toMatchObject({ kind: "ready", file: { text: "new" } });
  expect(fetcher.mock.calls.filter(([url]) => String(url).endsWith("-context"))).toHaveLength(2);
  client.dispose();
});