| 1 | import * as fs from "node:fs"; |
| 2 | import * as http from "node:http"; |
| 3 | import { AddressInfo } from "node:net"; |
| 4 | import * as os from "node:os"; |
| 5 | import * as path from "node:path"; |
| 6 | import { pathToFileURL } from "url"; |
| 7 | import { PackageIdentifier } from "./interfaces/slugs.js"; |
| 8 | import { RegistryClient } from "./registryClient.js"; |
| 9 | |
| 10 | describe("RegistryClient", () => { |
| 11 | // Create a temp directory for test files |
| 12 | let tempDir: string; |
| 13 | // Setup a test server |
| 14 | let server: http.Server; |
| 15 | let apiBaseUrl: string; |
| 16 | beforeAll(async () => { |
| 17 | // Create a temporary directory for test files |
| 18 | tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "registry-client-test-")); |
| 19 | |
| 20 | // Create a simple HTTP server for API testing |
| 21 | server = http.createServer((req, res) => { |
| 22 | if (req.url?.startsWith("/registry/v1/")) { |
| 23 | const authHeader = req.headers.authorization; |
| 24 | const data = { |
| 25 | content: authHeader |
| 26 | ? `auth content for ${req.url}` |
| 27 | : `content for ${req.url}`, |
| 28 | }; |
| 29 | |
| 30 | res.writeHead(200, { "Content-Type": "application/json" }); |
| 31 | res.end(JSON.stringify(data)); |
| 32 | } else { |
| 33 | res.writeHead(404); |
| 34 | res.end(); |
| 35 | } |
| 36 | }); |
| 37 | |
| 38 | // Start server on a random port |
| 39 | await new Promise<void>((resolve) => { |
| 40 | server.listen(0, "localhost", () => resolve()); |
| 41 | }); |
| 42 | |
| 43 | const address = server.address() as AddressInfo; |
| 44 | apiBaseUrl = `http://localhost:${address.port}`; |
| 45 | }); |
| 46 | |
| 47 | afterAll(async () => { |
| 48 | // Clean up temp directory |
| 49 | fs.rmSync(tempDir, { recursive: true, force: true }); |
| 50 | |
| 51 | // Close the test server |
| 52 | await new Promise<void>((resolve) => { |
| 53 | server.close(() => resolve()); |
| 54 | }); |
| 55 | }); |
| 56 | |
| 57 | describe("constructor", () => { |
| 58 | it("should use default apiBase if not provided", () => { |
| 59 | const client = new RegistryClient(); |
| 60 | expect((client as any).apiBase).toBe("https://api.continue.dev/"); |
| 61 | }); |
| 62 | |
| 63 | it("should append trailing slash to apiBase if missing", () => { |
| 64 | const client = new RegistryClient({ apiBase: "https://example.com" }); |
| 65 | expect((client as any).apiBase).toBe("https://example.com/"); |
| 66 | }); |
| 67 | |
| 68 | it("should keep trailing slash if apiBase already has one", () => { |
| 69 | const client = new RegistryClient({ apiBase: "https://example.com/" }); |
| 70 | expect((client as any).apiBase).toBe("https://example.com/"); |
| 71 | }); |
| 72 | |
| 73 | it("should store the accessToken if provided", () => { |
| 74 | const client = new RegistryClient({ accessToken: "test-token" }); |
| 75 | expect((client as any).accessToken).toBe("test-token"); |
| 76 | }); |
| 77 | |
| 78 | it("should store the rootPath if provided", () => { |
| 79 | const client = new RegistryClient({ rootPath: "/test-path" }); |
| 80 | expect((client as any).rootPath).toBe("/test-path"); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | describe("getContent", () => { |
| 85 | let testFilePath: string; |
| 86 | |
| 87 | beforeEach(() => { |
| 88 | // Create a test file for each test |
| 89 | testFilePath = path.join(tempDir, "test-file.yaml"); |
| 90 | fs.writeFileSync(testFilePath, "file content", "utf8"); |
| 91 | }); |
| 92 | |
| 93 | it("should get content from file path for file uriType", async () => { |
| 94 | const client = new RegistryClient(); |
| 95 | |
| 96 | const id: PackageIdentifier = { |
| 97 | uriType: "file", |
| 98 | fileUri: testFilePath, |
| 99 | }; |
| 100 | |
| 101 | const result = await client.getContent(id); |
| 102 | expect(result).toBe("file content"); |
| 103 | }); |
| 104 | |
| 105 | it("should get content from slug for slug uriType", async () => { |
| 106 | const client = new RegistryClient({ |
| 107 | apiBase: apiBaseUrl, |
| 108 | }); |
| 109 | |
| 110 | const id: PackageIdentifier = { |
| 111 | uriType: "slug", |
| 112 | fullSlug: { |
| 113 | ownerSlug: "owner", |
| 114 | packageSlug: "package", |
| 115 | versionSlug: "1.0.0", |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | const result = await client.getContent(id); |
| 120 | expect(result).toBe("content for /registry/v1/owner/package/1.0.0"); |
| 121 | }); |
| 122 | |
| 123 | it("should throw error for unknown uriType", async () => { |
| 124 | const client = new RegistryClient(); |
| 125 | |
| 126 | const id = { |
| 127 | uriType: "unknown", |
| 128 | } as any; |
| 129 | |
| 130 | await expect(client.getContent(id)).rejects.toThrow( |
| 131 | "Unknown package identifier type: unknown", |
| 132 | ); |
| 133 | }); |
| 134 | }); |
| 135 | |
| 136 | describe("getContentFromFilePath", () => { |
| 137 | let absoluteFilePath: string; |
| 138 | let fileUrl: string; |
| 139 | let relativeFilePath: string; |
| 140 | |
| 141 | beforeEach(() => { |
| 142 | // Create test files |
| 143 | absoluteFilePath = path.join(tempDir, "absolute-path.yaml"); |
| 144 | fs.writeFileSync(absoluteFilePath, "absolute file content", "utf8"); |
| 145 | |
| 146 | const urlFilePath = path.join(tempDir, "file-url-path.yaml"); |
| 147 | fs.writeFileSync(urlFilePath, "file:// file content", "utf8"); |
| 148 | const url = pathToFileURL(urlFilePath); |
| 149 | fileUrl = url.toString(); |
| 150 | |
| 151 | // Create a subdirectory and file in the temp directory |
| 152 | const subDir = path.join(tempDir, "sub"); |
| 153 | fs.mkdirSync(subDir, { recursive: true }); |
| 154 | relativeFilePath = "sub/relative-path.yaml"; |
| 155 | fs.writeFileSync( |
| 156 | path.join(tempDir, relativeFilePath), |
| 157 | "relative file content", |
| 158 | "utf8", |
| 159 | ); |
| 160 | }); |
| 161 | |
| 162 | it("should read from absolute path directly", () => { |
| 163 | const client = new RegistryClient(); |
| 164 | |
| 165 | const result = (client as any).getContentFromFilePath(absoluteFilePath); |
| 166 | |
| 167 | expect(result).toBe("absolute file content"); |
| 168 | }); |
| 169 | |
| 170 | it("should read from local file url directly", () => { |
| 171 | const client = new RegistryClient(); |
| 172 | |
| 173 | const result = (client as any).getContentFromFilePath(fileUrl); |
| 174 | |
| 175 | expect(result).toBe("file:// file content"); |
| 176 | }); |
| 177 | |
| 178 | it("should use rootPath for relative paths when provided", () => { |
| 179 | const client = new RegistryClient({ rootPath: tempDir }); |
| 180 | |
| 181 | const result = (client as any).getContentFromFilePath(relativeFilePath); |
| 182 | |
| 183 | expect(result).toBe("relative file content"); |
| 184 | }); |
| 185 | |
| 186 | it("should throw error for relative path without rootPath", () => { |
| 187 | const client = new RegistryClient(); |
| 188 | |
| 189 | expect(() => { |
| 190 | (client as any).getContentFromFilePath("relative/path.yaml"); |
| 191 | }).toThrow("No rootPath provided for relative file path"); |
| 192 | }); |
| 193 | }); |
| 194 | |
| 195 | describe("getContentFromSlug", () => { |
| 196 | it("should fetch content from API without auth token", async () => { |
| 197 | const client = new RegistryClient({ |
| 198 | apiBase: apiBaseUrl, |
| 199 | }); |
| 200 | const result = await (client as any).getContentFromSlug({ |
| 201 | ownerSlug: "owner", |
| 202 | packageSlug: "package", |
| 203 | versionSlug: "1.0.0", |
| 204 | }); |
| 205 | |
| 206 | expect(result).toBe("content for /registry/v1/owner/package/1.0.0"); |
| 207 | }); |
| 208 | |
| 209 | it("should include auth token in request when provided", async () => { |
| 210 | const client = new RegistryClient({ |
| 211 | apiBase: apiBaseUrl, |
| 212 | accessToken: "test-token", |
| 213 | }); |
| 214 | |
| 215 | const result = await (client as any).getContentFromSlug({ |
| 216 | ownerSlug: "owner", |
| 217 | packageSlug: "package", |
| 218 | versionSlug: "1.0.0", |
| 219 | }); |
| 220 | |
| 221 | expect(result).toBe("auth content for /registry/v1/owner/package/1.0.0"); |
| 222 | }); |
| 223 | }); |
| 224 | }); |