| 1 | import { |
| 2 | decodeFQSN, |
| 3 | decodeFullSlug, |
| 4 | decodePackageIdentifier, |
| 5 | decodePackageSlug, |
| 6 | encodeFQSN, |
| 7 | encodeFullSlug, |
| 8 | encodePackageIdentifier, |
| 9 | encodePackageSlug, |
| 10 | VirtualTags, |
| 11 | } from "./slugs.js"; |
| 12 | |
| 13 | describe("PackageSlug", () => { |
| 14 | it("should encode/decode package slugs", () => { |
| 15 | const testSlug = { |
| 16 | ownerSlug: "test-owner", |
| 17 | packageSlug: "test-package", |
| 18 | }; |
| 19 | const encoded = encodePackageSlug(testSlug); |
| 20 | expect(encoded).toBe("test-owner/test-package"); |
| 21 | const decoded = decodePackageSlug(encoded); |
| 22 | expect(decoded).toEqual(testSlug); |
| 23 | }); |
| 24 | |
| 25 | it("should encode/decode full slugs", () => { |
| 26 | const testFullSlug = { |
| 27 | ownerSlug: "test-owner", |
| 28 | packageSlug: "test-package", |
| 29 | versionSlug: "1.0.0", |
| 30 | }; |
| 31 | const encoded = encodeFullSlug(testFullSlug); |
| 32 | expect(encoded).toBe("test-owner/test-package@1.0.0"); |
| 33 | const decoded = decodeFullSlug(encoded); |
| 34 | expect(decoded).toEqual(testFullSlug); |
| 35 | }); |
| 36 | |
| 37 | it("should use latest tag when no version provided", () => { |
| 38 | const encoded = "test-owner/test-package"; |
| 39 | const decoded = decodeFullSlug(encoded); |
| 40 | expect(decoded.versionSlug).toBe(VirtualTags.Latest); |
| 41 | }); |
| 42 | |
| 43 | it("should encode/decode FQSN with single package", () => { |
| 44 | const testFQSN = { |
| 45 | packageSlugs: [ |
| 46 | { |
| 47 | ownerSlug: "test-owner", |
| 48 | packageSlug: "test-package", |
| 49 | }, |
| 50 | ], |
| 51 | secretName: "test-secret", |
| 52 | }; |
| 53 | const encoded = encodeFQSN(testFQSN); |
| 54 | expect(encoded).toBe("test-owner/test-package/test-secret"); |
| 55 | const decoded = decodeFQSN(encoded); |
| 56 | expect(decoded).toEqual(testFQSN); |
| 57 | }); |
| 58 | |
| 59 | it("should encode/decode FQSN with multiple packages", () => { |
| 60 | const testFQSN = { |
| 61 | packageSlugs: [ |
| 62 | { |
| 63 | ownerSlug: "owner1", |
| 64 | packageSlug: "package1", |
| 65 | }, |
| 66 | { |
| 67 | ownerSlug: "owner2", |
| 68 | packageSlug: "package2", |
| 69 | }, |
| 70 | ], |
| 71 | secretName: "test-secret", |
| 72 | }; |
| 73 | const encoded = encodeFQSN(testFQSN); |
| 74 | expect(encoded).toBe("owner1/package1/owner2/package2/test-secret"); |
| 75 | const decoded = decodeFQSN(encoded); |
| 76 | expect(decoded).toEqual(testFQSN); |
| 77 | }); |
| 78 | |
| 79 | it("should throw error for invalid FQSN format", () => { |
| 80 | expect(() => decodeFQSN("owner1/package1/owner2/test-secret")).toThrow( |
| 81 | "Invalid FQSN format: package slug must have two parts", |
| 82 | ); |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | describe("PackageIdentifier", () => { |
| 87 | it("should encode/decode slug-based package identifier", () => { |
| 88 | const testIdentifier = { |
| 89 | uriType: "slug" as const, |
| 90 | fullSlug: { |
| 91 | ownerSlug: "test-owner", |
| 92 | packageSlug: "test-package", |
| 93 | versionSlug: "2.1.0", |
| 94 | }, |
| 95 | }; |
| 96 | const encoded = encodePackageIdentifier(testIdentifier); |
| 97 | expect(encoded).toBe("test-owner/test-package@2.1.0"); |
| 98 | |
| 99 | const decoded = decodePackageIdentifier(encoded); |
| 100 | expect(decoded).toEqual(testIdentifier); |
| 101 | }); |
| 102 | |
| 103 | it("should encode/decode slug-based package identifier without version", () => { |
| 104 | const encoded = "test-owner/test-package"; |
| 105 | const decoded = decodePackageIdentifier(encoded); |
| 106 | |
| 107 | expect(decoded).toEqual({ |
| 108 | uriType: "slug", |
| 109 | fullSlug: { |
| 110 | ownerSlug: "test-owner", |
| 111 | packageSlug: "test-package", |
| 112 | versionSlug: VirtualTags.Latest, |
| 113 | }, |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | it("should encode/decode file-based package identifier with relative path", () => { |
| 118 | const testIdentifier = { |
| 119 | uriType: "file" as const, |
| 120 | fileUri: "./path/to/package.yaml", |
| 121 | }; |
| 122 | const encoded = encodePackageIdentifier(testIdentifier); |
| 123 | expect(encoded).toBe("./path/to/package.yaml"); |
| 124 | |
| 125 | const decoded = decodePackageIdentifier(encoded); |
| 126 | expect(decoded).toEqual(testIdentifier); |
| 127 | }); |
| 128 | |
| 129 | it("should encode/decode file-based package identifier with absolute path", () => { |
| 130 | const testIdentifier = { |
| 131 | uriType: "file" as const, |
| 132 | fileUri: "/absolute/path/to/package.yaml", |
| 133 | }; |
| 134 | const encoded = encodePackageIdentifier(testIdentifier); |
| 135 | expect(encoded).toBe("/absolute/path/to/package.yaml"); |
| 136 | |
| 137 | const decoded = decodePackageIdentifier(encoded); |
| 138 | expect(decoded).toEqual(testIdentifier); |
| 139 | }); |
| 140 | |
| 141 | it("should decode file-based package identifier with file:// protocol", () => { |
| 142 | const encoded = "file:///path/to/package.yaml"; |
| 143 | const decoded = decodePackageIdentifier(encoded); |
| 144 | |
| 145 | expect(decoded).toEqual({ |
| 146 | uriType: "file" as const, |
| 147 | fileUri: "/path/to/package.yaml", |
| 148 | }); |
| 149 | }); |
| 150 | |
| 151 | it("should throw error for unknown URI type", () => { |
| 152 | const invalidIdentifier = { |
| 153 | uriType: "invalid", |
| 154 | data: "something", |
| 155 | }; |
| 156 | |
| 157 | expect(() => encodePackageIdentifier(invalidIdentifier as any)).toThrow( |
| 158 | "Unknown URI type: invalid", |
| 159 | ); |
| 160 | }); |
| 161 | |
| 162 | it("should expand leading tilde when HOME is available", () => { |
| 163 | const originalHome = process.env.HOME; |
| 164 | const originalUserProfile = process.env.USERPROFILE; |
| 165 | |
| 166 | try { |
| 167 | process.env.HOME = "/tmp/test-home"; |
| 168 | delete process.env.USERPROFILE; |
| 169 | |
| 170 | const decoded = decodePackageIdentifier("~/package.yaml"); |
| 171 | |
| 172 | expect(decoded).toEqual({ |
| 173 | uriType: "file" as const, |
| 174 | fileUri: "/tmp/test-home/package.yaml", |
| 175 | }); |
| 176 | } finally { |
| 177 | if (originalHome === undefined) { |
| 178 | delete process.env.HOME; |
| 179 | } else { |
| 180 | process.env.HOME = originalHome; |
| 181 | } |
| 182 | |
| 183 | if (originalUserProfile === undefined) { |
| 184 | delete process.env.USERPROFILE; |
| 185 | } else { |
| 186 | process.env.USERPROFILE = originalUserProfile; |
| 187 | } |
| 188 | } |
| 189 | }); |
| 190 | |
| 191 | it("should leave leading tilde when no home directory is available", () => { |
| 192 | const originalHome = process.env.HOME; |
| 193 | const originalUserProfile = process.env.USERPROFILE; |
| 194 | |
| 195 | try { |
| 196 | delete process.env.HOME; |
| 197 | delete process.env.USERPROFILE; |
| 198 | |
| 199 | const decoded = decodePackageIdentifier("~/package.yaml"); |
| 200 | |
| 201 | expect(decoded).toEqual({ |
| 202 | uriType: "file" as const, |
| 203 | fileUri: "~/package.yaml", |
| 204 | }); |
| 205 | } finally { |
| 206 | if (originalHome === undefined) { |
| 207 | delete process.env.HOME; |
| 208 | } else { |
| 209 | process.env.HOME = originalHome; |
| 210 | } |
| 211 | |
| 212 | if (originalUserProfile === undefined) { |
| 213 | delete process.env.USERPROFILE; |
| 214 | } else { |
| 215 | process.env.USERPROFILE = originalUserProfile; |
| 216 | } |
| 217 | } |
| 218 | }); |
| 219 | }); |