Browse Source

update tell-it-cypher tests

content-update
Louise Foussat 3 years ago committed by Clément
parent
commit
e6f8444a2f
  1. 192
      js/tests/tell-it-cypher_test.mjs

192
js/tests/tell-it-cypher_test.mjs

@ -1,135 +1,103 @@
import * as cp from 'child_process' import * as cp from 'child_process'
import fs from 'fs/promises' import { mkdir, writeFile, readFile } from 'fs/promises'
import { join, resolve, isAbsolute } from 'path' import { join, isAbsolute } from 'path'
import { tmpdir } from 'os' import { tmpdir } from 'os'
import { promisify } from 'util' import { promisify } from 'util'
const mkdir = fs.mkdir
const rmdir = fs.rmdir
const writeFile = fs.writeFile
const readFile = fs.readFile
const exec = promisify(cp.exec) const exec = promisify(cp.exec)
export const tests = [] export const tests = []
const name = 'tell-it-cypher' const ranStr = () =>
// maybe get the sames from an api? like https://parser.name/ Math.random()
const guests = [ .toString(36)
'Shyam Langley', .substring(7)
'Austin Harwood',
'Reem Morgan', export const setup = async ({ path }) => {
'Neal Chamberlain', const dir = `${tmpdir()}/tell-it-cypher`
'Ryan Walters',
'Ocean Battle', await mkdir(dir)
'Ubaid Ballard',
'Victoria Chan', const run = async (cmd) => {
'Dominika Mullen', const [filename, keyword, newFile] = cmd.split(' ')
'Heath Denton', const filePath = isAbsolute(filename) ? filename : join(dir, filename)
'Lilith Hamilton', const { stdout } = await exec(
'Aisling Bailey', `node ${path} ${filePath} ${keyword} ${newFile || ''}`,
'Maizie Love',
'Nathanial Franco',
'Charmaine Bernard',
'Sohail Downes',
'Rabia Gomez',
'Brendan Brennan',
'Shannen Atherton',
'Esa Villarreal',
'Kayla Wynn',
'Gladys Hardy',
'Laaibah Rogers',
'Zishan Randolph',
'Connor Connolly',
'Arabella Wooten',
'Edna Floyd',
]
const shuffle = (arr) => {
let i = arr.length
let j, tmp
while (--i > 0) {
j = Math.floor(Math.random() * (i + 1))
tmp = arr[j]
arr[j] = arr[i]
arr[i] = tmp
}
return arr
}
const getRandomList = (names) =>
shuffle(names).slice(0, Math.floor(Math.random() * (names.length - 10) + 10))
const getExpected = (list) =>
list
.map((n) =>
n
.split(' ')
.reverse()
.join(' '),
) )
.sort() const newFileName =
.map((g, i) => `${i + 1}. ${g}`) newFile || (keyword === 'encode' ? 'cypher.txt' : 'clear.txt')
.join('\n') const fileContent = await readFile(newFileName, 'utf8').catch((err) =>
err.code === 'ENOENT' ? 'output file not found' : err,
export const setup = async () => { )
const dir = tmpdir() return { data: fileContent }
}
// check if already exists and rm?
await mkdir(`${dir}/${name}`) return { tmpPath: dir, run, encoded, decoded }
const randomList = getRandomList(guests)
const decoded = getExpected(randomList)
const encoded = Buffer.from(decoded).toString('base64')
await writeFile(`${dir}/${name}/encoded.txt`, encoded)
await writeFile(`${dir}/${name}/decoded.txt`, decoded)
return { tmpPath: `${dir}/${name}`, decoded, encoded }
}
const cypherIt = async ({ keyword, newFile, ctx, path, eq }) => {
const scriptPath = join(resolve(), path)
const filename = keyword === "encode" ? "decoded.txt" : "encoded.txt"
await exec(`node ${scriptPath} ${filename} ${keyword} ${newFile ? newFile : ""}`, {
cwd: ctx.tmpPath,
})
const newFileName = newFile || (keyword === "encode" ? "cypher.txt" : "clear.txt")
const out = await readFile(`${ctx.tmpPath}/${newFileName}`, 'utf8').catch((err) =>
err.code === 'ENOENT' ? 'output file not found' : err,
)
await exec(`rm ${newFileName}`, { cwd: ctx.tmpPath })
return eq(out, keyword === "encode" ? ctx.encoded : ctx.decoded)
} }
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {
return cypherIt({ const vips = `1. Langley Shyam
path, 2. Harwood Austin
eq, 3. Morgan Reem
ctx, 4. Chamberlain Neal
keyword: 'encode', 5. Walters Ryan`
}) const fileName = `${ctx.tmpPath}/vip.txt`
await writeFile(fileName, vips)
const { data } = await ctx.run(`${fileName} encode`)
return eq(
data,
'MS4gTGFuZ2xleSBTaHlhbQogIDIuIEhhcndvb2QgQXVzdGluCiAgMy4gTW9yZ2FuIFJlZW0KICA0LiBDaGFtYmVybGFpbiBOZWFsCiAgNS4gV2FsdGVycyBSeWFu',
)
}) })
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {
return cypherIt({ const vips = `1. Wynn Kayla
path, 2. Hardy Gladys
eq, 3. Rogers Laaibah
ctx, 4. Randolph Zishan
keyword: 'decode', 5. Connolly Connor`
}) const fileName = `${ctx.tmpPath}/vip-${ranStr()}.txt`
await writeFile(fileName, vips)
const { data } = await ctx.run(`${fileName} encode mysecret.txt`)
return eq(
data,
'MS4gV3lubiBLYXlsYQogIDIuIEhhcmR5IEdsYWR5cwogIDMuIFJvZ2VycyBMYWFpYmFoCiAgNC4gUmFuZG9scGggWmlzaGFuCiAgNS4gQ29ubm9sbHkgQ29ubm9y',
)
}) })
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {
return cypherIt({ const vipsEncoded = "MS4gVmlsbGFycmVhbCBFc2EKICAyLiBXeW5uIEtheWxhCiAgMy4gSGFyZHkgR2xhZHlzCiAgNC4gUm9nZXJzIExhYWliYWgKICA1LiBSYW5kb2xwaCBaaXNoYW4="
path, const fileName = `${ctx.tmpPath}/vip-encoded-${ranStr()}.txt`
eq, await writeFile(fileName, vipsEncoded)
ctx,
keyword: 'encode', const { data } = await ctx.run(`${fileName} decode`)
newFile: 'mysecret.txt', return eq(
}) data,
`1. Villarreal Esa
2. Wynn Kayla
3. Hardy Gladys
4. Rogers Laaibah
5. Randolph Zishan`,
)
}) })
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {
return cypherIt({ const vipsEncoded = "MS4gQmVybmFyZCBDaGFybWFpbmUKICAyLiBEb3duZXMgU29oYWlsCiAgMy4gR29tZXogUmFiaWEKICA0LiBCcmVubmFuIEJyZW5kYW4KICA1LiBBdGhlcnRvbiBTaGFubmVu"
path, const fileName = `${ctx.tmpPath}/vip-encoded-${ranStr()}.txt`
eq, await writeFile(fileName, vipsEncoded)
ctx,
keyword: 'decode', const { data } = await ctx.run(`${fileName} decode pandora.txt`)
newFile: 'pandora.txt', return eq(
}) data,
`1. Bernard Charmaine
2. Downes Sohail
3. Gomez Rabia
4. Brennan Brendan
5. Atherton Shannen`,
)
}) })
Object.freeze(tests) Object.freeze(tests)

Loading…
Cancel
Save