| 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 | }); |