Open History to see the last commit
1import * as NodeAssert from "node:assert/strict";
2
3import { describe, it } from "vite-plus/test";
4
5import { buildOpenCodeSdkClientConfig } from "./opencodeRuntime.ts";
6
7const WINDOWS_DIRECTORY = "C:\\Users\\someone\\code\\project";
8
9describe("buildOpenCodeSdkClientConfig", () => {
10 it("omits the directory for an external server on another host", () => {
11 const config = buildOpenCodeSdkClientConfig({
12 baseUrl: "http://10.0.0.5:4096",
13 directory: WINDOWS_DIRECTORY,
14 external: true,
15 });
16
17 NodeAssert.equal("directory" in config, false);
18 NodeAssert.equal(config.baseUrl, "http://10.0.0.5:4096");
19 });
20
21 it("keeps the directory for a managed server", () => {
22 const config = buildOpenCodeSdkClientConfig({
23 baseUrl: "http://127.0.0.1:51234",
24 directory: WINDOWS_DIRECTORY,
25 external: false,
26 });
27
28 NodeAssert.equal(config.directory, WINDOWS_DIRECTORY);
29 });
30
31 it("keeps the directory for an external server on this machine", () => {
32 for (const baseUrl of [
33 "http://localhost:4096",
34 "http://LOCALHOST:4096",
35 // A fully qualified `localhost` keeps its root label through URL parsing.
36 "http://localhost.:4096",
37 // RFC 6761 reserves the whole `.localhost` tree for loopback.
38 "http://name.localhost:4096",
39 "http://foo.bar.localhost:4096",
40 "http://127.0.0.1:4096",
41 // Shorthand and trailing-dot IPv4 both canonicalise to 127.0.0.1.
42 "http://127.1:4096",
43 "http://127.0.0.1.:4096",
44 "http://127.255.255.254:4096",
45 "http://[::1]:4096",
46 // IPv4-mapped loopback, which serialises as [::ffff:7f00:1].
47 "http://[::ffff:127.0.0.1]:4096",
48 "http://0.0.0.0:4096",
49 "http://[::]:4096",
50 ]) {
51 const config = buildOpenCodeSdkClientConfig({
52 baseUrl,
53 directory: WINDOWS_DIRECTORY,
54 external: true,
55 });
56
57 NodeAssert.equal(config.directory, WINDOWS_DIRECTORY, `expected directory for ${baseUrl}`);
58 }
59 });
60
61 it("treats a domain that merely looks like a loopback address as remote", () => {
62 // `127.example.com` is somebody else's server. Reading the leading `127.` as an
63 // address is what sends a local Windows path to it.
64 for (const baseUrl of [
65 "http://127.example.com:4096",
66 "http://localhost.example.com:4096",
67 "http://notlocalhost:4096",
68 "http://127.0.0.1.example.com:4096",
69 ]) {
70 const config = buildOpenCodeSdkClientConfig({
71 baseUrl,
72 directory: WINDOWS_DIRECTORY,
73 external: true,
74 });
75
76 NodeAssert.equal("directory" in config, false, `expected no directory for ${baseUrl}`);
77 }
78 });
79
80 it("keeps the directory when the base URL cannot be parsed", () => {
81 const config = buildOpenCodeSdkClientConfig({
82 baseUrl: "not a url",
83 directory: WINDOWS_DIRECTORY,
84 external: true,
85 });
86
87 NodeAssert.equal(config.directory, WINDOWS_DIRECTORY);
88 });
89
90 it("still sends the authorization header when the directory is dropped", () => {
91 const config = buildOpenCodeSdkClientConfig({
92 baseUrl: "http://build-server.internal:4096",
93 directory: WINDOWS_DIRECTORY,
94 external: true,
95 serverPassword: "hunter2",
96 });
97
98 NodeAssert.equal("directory" in config, false);
99 NodeAssert.equal(
100 config.headers?.Authorization,
101 `Basic ${Buffer.from("opencode:hunter2", "utf8").toString("base64")}`,
102 );
103 });
104});