Skip to content

Commit

Permalink
Add test for JS SDK for file upload/download
Browse files Browse the repository at this point in the history
  • Loading branch information
mlejva committed Oct 12, 2023
1 parent a7fa156 commit 58fa165
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
8 changes: 3 additions & 5 deletions packages/js-sdk/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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`
}
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions packages/js-sdk/test/fileUploadDownload.test.mjs
Original file line number Diff line number Diff line change
@@ -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()
})
15 changes: 7 additions & 8 deletions packages/js-sdk/test/filesystem.test.mjs
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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!')

Expand All @@ -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!')
Expand All @@ -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')
Expand All @@ -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')

Expand Down

0 comments on commit 58fa165

Please sign in to comment.