|
1 | 1 | import { describe, expect, it } from "vitest" |
2 | 2 |
|
3 | | -import { getDependencyFromModuleSpecifier, isLocalFile, isUrl } from "./utils" |
| 3 | +import { |
| 4 | + getDependencyFromModuleSpecifier, |
| 5 | + isLocalFile, |
| 6 | + isUniversalRegistryItem, |
| 7 | + isUrl, |
| 8 | +} from "./utils" |
4 | 9 |
|
5 | 10 | describe("getDependencyFromModuleSpecifier", () => { |
6 | 11 | it("should return the first part of a non-scoped package with path", () => { |
@@ -130,3 +135,139 @@ describe("isLocalFile", () => { |
130 | 135 | expect(isLocalFile("/absolute/path")).toBe(false) |
131 | 136 | }) |
132 | 137 | }) |
| 138 | + |
| 139 | +describe("isUniversalRegistryItem", () => { |
| 140 | + it("should return true when all files have targets", () => { |
| 141 | + const registryItem = { |
| 142 | + files: [ |
| 143 | + { |
| 144 | + path: "file1.ts", |
| 145 | + target: "src/file1.ts", |
| 146 | + type: "registry:lib" as const, |
| 147 | + }, |
| 148 | + { |
| 149 | + path: "file2.ts", |
| 150 | + target: "src/utils/file2.ts", |
| 151 | + type: "registry:lib" as const, |
| 152 | + }, |
| 153 | + ], |
| 154 | + } |
| 155 | + expect(isUniversalRegistryItem(registryItem)).toBe(true) |
| 156 | + }) |
| 157 | + |
| 158 | + it("should return false when some files lack targets", () => { |
| 159 | + const registryItem = { |
| 160 | + files: [ |
| 161 | + { |
| 162 | + path: "file1.ts", |
| 163 | + target: "src/file1.ts", |
| 164 | + type: "registry:lib" as const, |
| 165 | + }, |
| 166 | + { path: "file2.ts", target: "", type: "registry:lib" as const }, |
| 167 | + ], |
| 168 | + } |
| 169 | + expect(isUniversalRegistryItem(registryItem)).toBe(false) |
| 170 | + }) |
| 171 | + |
| 172 | + it("should return false when no files have targets", () => { |
| 173 | + const registryItem = { |
| 174 | + files: [ |
| 175 | + { path: "file1.ts", target: "", type: "registry:lib" as const }, |
| 176 | + { path: "file2.ts", target: "", type: "registry:lib" as const }, |
| 177 | + ], |
| 178 | + } |
| 179 | + expect(isUniversalRegistryItem(registryItem)).toBe(false) |
| 180 | + }) |
| 181 | + |
| 182 | + it("should return false when files array is empty", () => { |
| 183 | + const registryItem = { |
| 184 | + files: [], |
| 185 | + } |
| 186 | + expect(isUniversalRegistryItem(registryItem)).toBe(false) |
| 187 | + }) |
| 188 | + |
| 189 | + it("should return false when files is undefined", () => { |
| 190 | + const registryItem = {} |
| 191 | + expect(isUniversalRegistryItem(registryItem)).toBe(false) |
| 192 | + }) |
| 193 | + |
| 194 | + it("should return false when registryItem is null", () => { |
| 195 | + expect(isUniversalRegistryItem(null)).toBe(false) |
| 196 | + }) |
| 197 | + |
| 198 | + it("should return false when registryItem is undefined", () => { |
| 199 | + expect(isUniversalRegistryItem(undefined)).toBe(false) |
| 200 | + }) |
| 201 | + |
| 202 | + it("should return false when target is null", () => { |
| 203 | + const registryItem = { |
| 204 | + files: [ |
| 205 | + { |
| 206 | + path: "file1.ts", |
| 207 | + target: null as any, |
| 208 | + type: "registry:lib" as const, |
| 209 | + }, |
| 210 | + ], |
| 211 | + } |
| 212 | + expect(isUniversalRegistryItem(registryItem)).toBe(false) |
| 213 | + }) |
| 214 | + |
| 215 | + it("should return false when target is undefined", () => { |
| 216 | + const registryItem = { |
| 217 | + files: [{ path: "file1.ts", type: "registry:lib" as const }], |
| 218 | + } |
| 219 | + expect(isUniversalRegistryItem(registryItem)).toBe(false) |
| 220 | + }) |
| 221 | + |
| 222 | + it("should handle mixed file types correctly", () => { |
| 223 | + const registryItem = { |
| 224 | + files: [ |
| 225 | + { |
| 226 | + path: "component.tsx", |
| 227 | + target: "components/ui/component.tsx", |
| 228 | + type: "registry:ui" as const, |
| 229 | + }, |
| 230 | + { |
| 231 | + path: "utils.ts", |
| 232 | + target: "lib/utils.ts", |
| 233 | + type: "registry:lib" as const, |
| 234 | + }, |
| 235 | + { |
| 236 | + path: "hook.ts", |
| 237 | + target: "hooks/use-something.ts", |
| 238 | + type: "registry:hook" as const, |
| 239 | + }, |
| 240 | + ], |
| 241 | + } |
| 242 | + expect(isUniversalRegistryItem(registryItem)).toBe(true) |
| 243 | + }) |
| 244 | + |
| 245 | + it("should return true when all targets are non-empty strings", () => { |
| 246 | + const registryItem = { |
| 247 | + files: [ |
| 248 | + { path: "file1.ts", target: " ", type: "registry:lib" as const }, // whitespace is truthy |
| 249 | + { path: "file2.ts", target: "0", type: "registry:lib" as const }, // "0" is truthy |
| 250 | + ], |
| 251 | + } |
| 252 | + expect(isUniversalRegistryItem(registryItem)).toBe(true) |
| 253 | + }) |
| 254 | + |
| 255 | + it("should handle real-world example with path traversal attempts", () => { |
| 256 | + const registryItem = { |
| 257 | + files: [ |
| 258 | + { |
| 259 | + path: "malicious.ts", |
| 260 | + target: "../../../etc/passwd", |
| 261 | + type: "registry:lib" as const, |
| 262 | + }, |
| 263 | + { |
| 264 | + path: "normal.ts", |
| 265 | + target: "src/normal.ts", |
| 266 | + type: "registry:lib" as const, |
| 267 | + }, |
| 268 | + ], |
| 269 | + } |
| 270 | + // The function should still return true - path validation is handled elsewhere |
| 271 | + expect(isUniversalRegistryItem(registryItem)).toBe(true) |
| 272 | + }) |
| 273 | +}) |
0 commit comments