|
1 | 1 | import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
2 | 2 | import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs" |
3 | 3 | import { join } from "node:path" |
4 | | -import { ZipJson, listFromFile, unzipFromFile } from "../../src/index.js" |
| 4 | +import { |
| 5 | + ZipJson, |
| 6 | + listFromFile, |
| 7 | + unzipFromFile, |
| 8 | + zip, |
| 9 | + zipToFile, |
| 10 | +} from "../../src/index.js" |
5 | 11 |
|
6 | 12 | describe("Index API Tests", () => { |
7 | 13 | const testDir = join(process.cwd(), "test-index") |
@@ -78,3 +84,138 @@ describe("Index API Tests", () => { |
78 | 84 | expect(files[0].isDirectory).toBe(false) |
79 | 85 | }) |
80 | 86 | }) |
| 87 | + |
| 88 | +describe("Pattern Validation Tests", () => { |
| 89 | + const testDir = join(process.cwd(), "test-validation") |
| 90 | + const zipJson = new ZipJson() |
| 91 | + |
| 92 | + beforeEach(() => { |
| 93 | + mkdirSync(testDir, { recursive: true }) |
| 94 | + writeFileSync(join(testDir, "test.txt"), "test content") |
| 95 | + }) |
| 96 | + |
| 97 | + afterEach(() => { |
| 98 | + if (existsSync(testDir)) { |
| 99 | + rmSync(testDir, { recursive: true, force: true }) |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + describe("ZipJson class methods", () => { |
| 104 | + test("zip() validates patterns parameter", async () => { |
| 105 | + // Valid cases |
| 106 | + await expect(zipJson.zip([])).resolves.toBeDefined() |
| 107 | + await expect(zipJson.zip(["*.txt"])).resolves.toBeDefined() |
| 108 | + |
| 109 | + // Invalid cases |
| 110 | + await expect(zipJson.zip(null as any)).rejects.toThrow( |
| 111 | + "patterns must be an array of strings", |
| 112 | + ) |
| 113 | + await expect(zipJson.zip(undefined as any)).rejects.toThrow( |
| 114 | + "patterns must be an array of strings", |
| 115 | + ) |
| 116 | + await expect(zipJson.zip("*.txt" as any)).rejects.toThrow( |
| 117 | + "patterns must be an array of strings", |
| 118 | + ) |
| 119 | + await expect(zipJson.zip([123] as any)).rejects.toThrow( |
| 120 | + "patterns[0] must be a string, got number", |
| 121 | + ) |
| 122 | + await expect(zipJson.zip([""])).rejects.toThrow( |
| 123 | + "patterns[0] must be a non-empty string", |
| 124 | + ) |
| 125 | + await expect(zipJson.zip([" "])).rejects.toThrow( |
| 126 | + "patterns[0] must be a non-empty string", |
| 127 | + ) |
| 128 | + }) |
| 129 | + |
| 130 | + test("zipToFile() validates patterns parameter", async () => { |
| 131 | + const outputPath = join(testDir, "test.json") |
| 132 | + |
| 133 | + // Valid cases |
| 134 | + await expect(zipJson.zipToFile([], outputPath)).resolves.toBeUndefined() |
| 135 | + await expect( |
| 136 | + zipJson.zipToFile(["*.txt"], outputPath), |
| 137 | + ).resolves.toBeUndefined() |
| 138 | + |
| 139 | + // Invalid cases |
| 140 | + await expect(zipJson.zipToFile(null as any, outputPath)).rejects.toThrow( |
| 141 | + "patterns must be an array of strings", |
| 142 | + ) |
| 143 | + await expect( |
| 144 | + zipJson.zipToFile(undefined as any, outputPath), |
| 145 | + ).rejects.toThrow("patterns must be an array of strings") |
| 146 | + await expect( |
| 147 | + zipJson.zipToFile("*.txt" as any, outputPath), |
| 148 | + ).rejects.toThrow("patterns must be an array of strings") |
| 149 | + await expect( |
| 150 | + zipJson.zipToFile([null] as any, outputPath), |
| 151 | + ).rejects.toThrow("patterns[0] must be a string, got object") |
| 152 | + await expect(zipJson.zipToFile([""], outputPath)).rejects.toThrow( |
| 153 | + "patterns[0] must be a non-empty string", |
| 154 | + ) |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + describe("Standalone functions", () => { |
| 159 | + test("zip() function validates patterns parameter", async () => { |
| 160 | + // Valid cases |
| 161 | + await expect(zip([])).resolves.toBeDefined() |
| 162 | + await expect(zip(["*.txt"])).resolves.toBeDefined() |
| 163 | + |
| 164 | + // Invalid cases |
| 165 | + await expect(zip(null as any)).rejects.toThrow( |
| 166 | + "patterns must be an array of strings", |
| 167 | + ) |
| 168 | + await expect(zip(undefined as any)).rejects.toThrow( |
| 169 | + "patterns must be an array of strings", |
| 170 | + ) |
| 171 | + await expect(zip({} as any)).rejects.toThrow( |
| 172 | + "patterns must be an array of strings", |
| 173 | + ) |
| 174 | + await expect(zip([{}] as any)).rejects.toThrow( |
| 175 | + "patterns[0] must be a string, got object", |
| 176 | + ) |
| 177 | + await expect(zip(["\t\n"])).rejects.toThrow( |
| 178 | + "patterns[0] must be a non-empty string", |
| 179 | + ) |
| 180 | + }) |
| 181 | + |
| 182 | + test("zipToFile() function validates patterns parameter", async () => { |
| 183 | + const outputPath = join(testDir, "standalone.json") |
| 184 | + |
| 185 | + // Valid cases |
| 186 | + await expect(zipToFile([], outputPath)).resolves.toBeUndefined() |
| 187 | + await expect(zipToFile(["*.txt"], outputPath)).resolves.toBeUndefined() |
| 188 | + |
| 189 | + // Invalid cases |
| 190 | + await expect(zipToFile(123 as any, outputPath)).rejects.toThrow( |
| 191 | + "patterns must be an array of strings", |
| 192 | + ) |
| 193 | + await expect( |
| 194 | + zipToFile(["valid", 456] as any, outputPath), |
| 195 | + ).rejects.toThrow("patterns[1] must be a string, got number") |
| 196 | + await expect(zipToFile(["valid", ""], outputPath)).rejects.toThrow( |
| 197 | + "patterns[1] must be a non-empty string", |
| 198 | + ) |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + describe("Mixed valid and invalid patterns", () => { |
| 203 | + test("identifies first invalid pattern in mixed array", async () => { |
| 204 | + await expect(zip(["*.js", "", "*.ts"])).rejects.toThrow( |
| 205 | + "patterns[1] must be a non-empty string", |
| 206 | + ) |
| 207 | + await expect(zip(["*.js", 123, "*.ts"] as any)).rejects.toThrow( |
| 208 | + "patterns[1] must be a string, got number", |
| 209 | + ) |
| 210 | + await expect(zip(["*.js", "*.ts", null] as any)).rejects.toThrow( |
| 211 | + "patterns[2] must be a string, got object", |
| 212 | + ) |
| 213 | + }) |
| 214 | + |
| 215 | + test("passes for all valid patterns", async () => { |
| 216 | + await expect( |
| 217 | + zip(["*.js", "src/**/*.ts", "!node_modules/**", "*.{json,yml}"]), |
| 218 | + ).resolves.toBeDefined() |
| 219 | + }) |
| 220 | + }) |
| 221 | +}) |
0 commit comments