Skip to content

Commit

Permalink
Merge pull request #22 from heryTz/feat/add-test-with-other-dir
Browse files Browse the repository at this point in the history
feat: add test with other dir
  • Loading branch information
heryTz committed Jun 27, 2024
2 parents a7dfd27 + 77f3948 commit 97759e7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-steaks-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rcu-back-core": patch
---

add test with other dir
3 changes: 2 additions & 1 deletion packages/back-core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
tmp
tmp
tmp-other
1 change: 1 addition & 0 deletions packages/back-core/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.turbo
tmp
tmp-other
39 changes: 38 additions & 1 deletion packages/back-core/src/__tests__/rcu-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writeFile } from "fs/promises";
import { JsonStoreData, JsonStoreProvider } from "../json-store-provider";
import { RCUService } from "../rcu-service";
import { deleteFile, readOrCreateFile, sleep } from "../util";
import { deleteDir, deleteFile, readOrCreateFile, sleep } from "../util";

const fileBuffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);

Expand Down Expand Up @@ -191,4 +191,41 @@ describe(RCUService.name, () => {
})
).rejects.toThrow("File corrupted");
});

it("upload chunk to other dir", async () => {
const otherDir = "./tmp-other";
await deleteDir(otherDir);
const file = "./tmp/test-service-7.json";
await deleteFile(file);
await readOrCreateFile(
file,
JSON.stringify({
rows: [
{
id: "test-chunk-uploaded-to-other-dir.txt",
chunkCount: 4,
chunkFilenames: [],
lastUploadedChunkNumber: 0,
},
],
} as JsonStoreData)
);
const store = new JsonStoreProvider(file);
await sleep(100);
const service = new RCUService({
store,
tmpDir: otherDir,
outputDir: otherDir,
});
const response = await service.upload({
file: fileBuffer,
fileId: "test-chunk-uploaded-to-other-dir.txt",
chunkCount: 4,
chunkNumber: 1,
chunkSize: 1,
fileSize: 6,
originalFilename: "test-chunk-uploaded-to-other-dir.txt",
});
expect(response.message).toBe("Chunk uploaded");
});
});
9 changes: 8 additions & 1 deletion packages/back-core/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile, access, mkdir, writeFile, unlink } from "fs/promises";
import { readFile, access, mkdir, writeFile, unlink, rm } from "fs/promises";
import path from "path";

export async function readOrCreateFile(
Expand Down Expand Up @@ -34,3 +34,10 @@ export async function createDir(dirPath: string) {
await mkdir(dirPath, { recursive: true });
}
}

export async function deleteDir(dirPath: string) {
try {
await access(dirPath);
await rm(dirPath, { recursive: true });
} catch (error) {}
}

0 comments on commit 97759e7

Please sign in to comment.