2021-08-17 16:27:02 +03:00
|
|
|
import { jest } from '@jest/globals'
|
|
|
|
import path from 'path'
|
|
|
|
import http from 'http'
|
|
|
|
import { fileURLToPath } from 'url'
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2021-08-17 16:27:02 +03:00
|
|
|
const currentDirName = path.dirname(fileURLToPath(import.meta.url))
|
|
|
|
const mockFixtureDir = path.resolve(currentDirName, '../fixtures')
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2021-08-17 16:27:02 +03:00
|
|
|
export const fixtureDir = mockFixtureDir
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2021-08-17 16:27:02 +03:00
|
|
|
export const initJest = (mockFixture) => {
|
2021-04-11 01:09:09 +03:00
|
|
|
jest.setTimeout(1200000)
|
2020-03-10 03:27:54 +03:00
|
|
|
|
2021-08-17 16:27:02 +03:00
|
|
|
const mockAppDir = path.join(mockFixtureDir, mockFixture)
|
|
|
|
process.env.__TAURI_TEST_APP_DIR = mockAppDir
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|
|
|
|
|
2021-08-17 16:27:02 +03:00
|
|
|
export const startServer = (onSuccess) => {
|
2020-03-30 05:41:45 +03:00
|
|
|
const responses = {
|
|
|
|
writeFile: null,
|
|
|
|
readFile: null,
|
|
|
|
writeFileWithDir: null,
|
|
|
|
readFileWithDir: null,
|
|
|
|
readDir: null,
|
|
|
|
readDirWithDir: null,
|
|
|
|
copyFile: null,
|
|
|
|
copyFileWithDir: null,
|
|
|
|
createDir: null,
|
|
|
|
createDirWithDir: null,
|
|
|
|
removeDir: null,
|
|
|
|
removeDirWithDir: null,
|
|
|
|
renameFile: null,
|
|
|
|
renameFileWithDir: null,
|
|
|
|
removeFile: null,
|
|
|
|
renameFileWithDir: null,
|
|
|
|
listen: null
|
|
|
|
}
|
2020-07-09 16:40:31 +03:00
|
|
|
|
|
|
|
function addResponse(response) {
|
2020-03-30 05:41:45 +03:00
|
|
|
responses[response.cmd] = true
|
2020-08-19 05:36:46 +03:00
|
|
|
if (!Object.values(responses).some((c) => c === null)) {
|
2020-03-30 05:41:45 +03:00
|
|
|
server.close(onSuccess)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:40:24 +03:00
|
|
|
const app = http.createServer((req, res) => {
|
|
|
|
// Set CORS headers
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*')
|
|
|
|
res.setHeader('Access-Control-Request-Method', '*')
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', '*')
|
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2020-03-30 05:41:45 +03:00
|
|
|
let body = ''
|
2020-08-19 05:36:46 +03:00
|
|
|
req.on('data', (chunk) => {
|
2020-03-30 05:41:45 +03:00
|
|
|
body += chunk.toString()
|
|
|
|
})
|
2020-03-07 19:40:24 +03:00
|
|
|
if (req.url === '/reply') {
|
2020-03-30 05:41:45 +03:00
|
|
|
req.on('end', () => {
|
|
|
|
const json = JSON.parse(body)
|
|
|
|
addResponse(json)
|
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
2020-03-07 19:40:24 +03:00
|
|
|
})
|
2020-03-30 05:41:45 +03:00
|
|
|
}
|
|
|
|
if (req.url === '/error') {
|
2020-03-07 19:40:24 +03:00
|
|
|
req.on('end', () => {
|
2020-03-30 05:41:45 +03:00
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
|
|
|
throw new Error(body)
|
2020-03-07 19:40:24 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const port = 7000
|
|
|
|
const server = app.listen(port)
|
2020-07-09 16:40:31 +03:00
|
|
|
return {
|
|
|
|
server,
|
|
|
|
responses
|
|
|
|
}
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|