From 58fa16566ce4d0054f4e225d4dc3e467e416ab40 Mon Sep 17 00:00:00 2001 From: Vasek Mlejnsky Date: Thu, 12 Oct 2023 15:48:39 -0700 Subject: [PATCH] Add test for JS SDK for file upload/download --- packages/js-sdk/src/session/index.ts | 8 +++----- .../js-sdk/test/fileUploadDownload.test.mjs | 19 +++++++++++++++++++ packages/js-sdk/test/filesystem.test.mjs | 15 +++++++-------- 3 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 packages/js-sdk/test/fileUploadDownload.test.mjs diff --git a/packages/js-sdk/src/session/index.ts b/packages/js-sdk/src/session/index.ts index 7725d4c53..86d2e72a3 100644 --- a/packages/js-sdk/src/session/index.ts +++ b/packages/js-sdk/src/session/index.ts @@ -2,9 +2,7 @@ import normalizePath from 'normalize-path' import FormData from 'form-data' import fetch from 'node-fetch' -import { - ENVD_PORT, -} from '../constants' +import { ENVD_PORT } from '../constants' import { components } from '../api' import { id } from '../utils/id' import { createDeferredPromise, formatSettledErrors, withTimeout } from '../utils/promise' @@ -367,7 +365,6 @@ export class Session extends SessionConnection { */ public get fileURL() { const hostname = this.getHostname(this.opts.__debug_port) - console.log('HOSTNAME', hostname) const protocol = this.opts.__debug_devEnv === 'local' ? 'http' : 'https' return `${protocol}://${ENVD_PORT}-${hostname}/file` } @@ -415,7 +412,6 @@ export class Session extends SessionConnection { contentType: contentType || 'application/octet-stream', }) - console.log(this.fileURL) const response = await fetch(this.fileURL, { method: 'POST', body: formData, @@ -425,6 +421,8 @@ export class Session extends SessionConnection { const text = await response.text() throw new Error(`Failed to upload file: ${text}`) } + + return `/home/user/${filename}` } async downloadFile(remotePath: string) { diff --git a/packages/js-sdk/test/fileUploadDownload.test.mjs b/packages/js-sdk/test/fileUploadDownload.test.mjs new file mode 100644 index 000000000..a03656aca --- /dev/null +++ b/packages/js-sdk/test/fileUploadDownload.test.mjs @@ -0,0 +1,19 @@ +import { Session } from '../src' +import { expect, test } from 'vitest' +import * as fs from 'node:fs' +import * as path from 'node:path' + +test('upload and download file', async () => { + const session = await Session.create({ id: 'Nodejs' }) + + const localFile = fs.readFileSync(path.join(__dirname, '/assets/video.webm')) + + const uploadedFilePath = await session.uploadFile(localFile, 'video.webm') + + const response = await session.downloadFile(uploadedFilePath) + const downloadedFile = await response.buffer() + + expect(localFile).toEqual(downloadedFile) + + await session.close() +}) diff --git a/packages/js-sdk/test/filesystem.test.mjs b/packages/js-sdk/test/filesystem.test.mjs index 223d7a741..ada63e0a0 100644 --- a/packages/js-sdk/test/filesystem.test.mjs +++ b/packages/js-sdk/test/filesystem.test.mjs @@ -1,10 +1,10 @@ -import {Session} from '../src' -import {expect, test} from 'vitest' +import { Session } from '../src' +import { expect, test } from 'vitest' import * as fs from 'node:fs' import * as path from 'node:path' test('list files', async () => { - const session = await Session.create({id: 'Nodejs'}) + const session = await Session.create({ id: 'Nodejs' }) await session.filesystem.makeDir('/test/new') const ls = await session.filesystem.list('/test') @@ -13,9 +13,8 @@ test('list files', async () => { await session.close() }) - test('create file', async () => { - const session = await Session.create({id: 'Nodejs'}) + const session = await Session.create({ id: 'Nodejs' }) await session.filesystem.makeDir('/test') await session.filesystem.write('/test/test.txt', 'Hello World!') @@ -26,7 +25,7 @@ test('create file', async () => { }) test('read and write', async () => { - const session = await Session.create({id: 'Nodejs'}) + const session = await Session.create({ id: 'Nodejs' }) // String await session.filesystem.write('/tmp/test.txt', 'Hello World!') @@ -43,7 +42,7 @@ test('read and write', async () => { }) test('list delete files', async () => { - const session = await Session.create({id: 'Nodejs'}) + const session = await Session.create({ id: 'Nodejs' }) await session.filesystem.makeDir('/test/new') let ls = await session.filesystem.list('/test') @@ -58,7 +57,7 @@ test('list delete files', async () => { }) test('watch dir', async () => { - const session = new Session({id: 'Nodejs'}) + const session = new Session({ id: 'Nodejs' }) await session.open() await session.filesystem.write('/tmp/test.txt', 'Hello')