-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmockwaveenv.test.ts
More file actions
119 lines (98 loc) · 4.96 KB
/
mockwaveenv.test.ts
File metadata and controls
119 lines (98 loc) · 4.96 KB
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
import { base64ToArray, base64ToString } from "@/util/util";
import { describe, expect, it, vi } from "vitest";
import { DefaultMockFilesystem } from "./mockfilesystem";
const { showPreviewContextMenu } = vi.hoisted(() => ({
showPreviewContextMenu: vi.fn(),
}));
vi.mock("../preview-contextmenu", () => ({
showPreviewContextMenu,
}));
describe("makeMockWaveEnv", () => {
it("uses the preview context menu by default", async () => {
const { makeMockWaveEnv } = await import("./mockwaveenv");
const env = makeMockWaveEnv();
const menu = [{ label: "Open" }];
const event = { stopPropagation: vi.fn() } as any;
env.showContextMenu(menu, event);
expect(showPreviewContextMenu).toHaveBeenCalledWith(menu, event);
});
it("provides a populated mock filesystem rooted at /Users/mike", () => {
expect(DefaultMockFilesystem.homePath).toBe("/Users/mike");
expect(DefaultMockFilesystem.fileCount).toBeGreaterThanOrEqual(100);
expect(DefaultMockFilesystem.directoryCount).toBeGreaterThanOrEqual(10);
});
it("implements file info, read, list, and join commands", async () => {
const { makeMockWaveEnv } = await import("./mockwaveenv");
const env = makeMockWaveEnv();
const bashrcInfo = await env.rpc.FileInfoCommand(null as any, {
info: { path: "wsh://local//Users/mike/.bashrc" },
});
expect(bashrcInfo.path).toBe("/Users/mike/.bashrc");
expect(bashrcInfo.mimetype).toBe("text/plain");
const bashrcData = await env.rpc.FileReadCommand(null as any, {
info: { path: "wsh://local//Users/mike/.bashrc" },
});
expect(base64ToString(bashrcData.data64)).toContain('alias gs="git status -sb"');
const visibleHomeEntries = await env.rpc.FileListCommand(null as any, {
path: "/Users/mike",
});
expect(visibleHomeEntries.some((entry) => entry.name === ".bashrc")).toBe(false);
expect(visibleHomeEntries.some((entry) => entry.name === "waveterm")).toBe(true);
const allHomeEntries = await env.rpc.FileListCommand(null as any, {
path: "/Users/mike",
opts: { all: true },
});
expect(allHomeEntries.some((entry) => entry.name === ".bashrc")).toBe(true);
const dirRead = await env.rpc.FileReadCommand(null as any, {
info: { path: "/Users/mike/waveterm" },
});
expect(dirRead.entries.some((entry) => entry.name === "docs" && entry.isdir)).toBe(true);
const joined = await env.rpc.FileJoinCommand(null as any, [
"wsh://local//Users/mike/Documents",
"../waveterm/docs",
"preview-notes.md",
]);
expect(joined.path).toBe("/Users/mike/waveterm/docs/preview-notes.md");
expect(joined.mimetype).toBe("text/markdown");
});
it("implements file list and read stream commands", async () => {
const { makeMockWaveEnv } = await import("./mockwaveenv");
const env = makeMockWaveEnv();
const listPackets: CommandRemoteListEntriesRtnData[] = [];
for await (const packet of env.rpc.FileListStreamCommand(null as any, {
path: "/Users/mike",
opts: { all: true, limit: 4 },
})) {
listPackets.push(packet);
}
expect(listPackets).toHaveLength(1);
expect(listPackets[0].fileinfo).toHaveLength(4);
const readPackets: FileData[] = [];
for await (const packet of env.rpc.FileReadStreamCommand(null as any, {
info: { path: "/Users/mike/Pictures/beach-sunrise.png" },
})) {
readPackets.push(packet);
}
expect(readPackets[0].info?.path).toBe("/Users/mike/Pictures/beach-sunrise.png");
const imageBytes = base64ToArray(readPackets[1].data64);
expect(Array.from(imageBytes.slice(0, 4))).toEqual([0x89, 0x50, 0x4e, 0x47]);
});
it("implements secrets commands with in-memory storage", async () => {
const { makeMockWaveEnv } = await import("./mockwaveenv");
const env = makeMockWaveEnv({ platform: "linux" });
await env.rpc.SetSecretsCommand(null as any, {
OPENAI_API_KEY: "sk-test",
ANTHROPIC_API_KEY: "anthropic-test",
} as any);
expect(await env.rpc.GetSecretsLinuxStorageBackendCommand(null as any)).toBe("libsecret");
expect(await env.rpc.GetSecretsNamesCommand(null as any)).toEqual(["ANTHROPIC_API_KEY", "OPENAI_API_KEY"]);
expect(await env.rpc.GetSecretsCommand(null as any, ["OPENAI_API_KEY", "MISSING_SECRET"])).toEqual({
OPENAI_API_KEY: "sk-test",
});
await env.rpc.SetSecretsCommand(null as any, { OPENAI_API_KEY: null } as any);
expect(await env.rpc.GetSecretsNamesCommand(null as any)).toEqual(["ANTHROPIC_API_KEY"]);
expect(await env.rpc.GetSecretsCommand(null as any, ["OPENAI_API_KEY", "ANTHROPIC_API_KEY"])).toEqual({
ANTHROPIC_API_KEY: "anthropic-test",
});
});
});