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