Files

134 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

2024-02-29 17:02:56 -08:00
import fs from 'fs/promises'
import os from 'os'
import path from 'path'
import { predicateFromInputs, PredicateInputs } from '../src/predicate'
2024-02-29 17:02:56 -08:00
describe('subjectFromInputs', () => {
const blankInputs: PredicateInputs = {
predicateType: '',
predicate: '',
predicatePath: ''
}
2024-02-29 17:02:56 -08:00
describe('when no inputs are provided', () => {
2026-02-13 15:20:42 -08:00
it('throws an error', async () => {
await expect(predicateFromInputs(blankInputs)).rejects.toThrow(
/predicate-type/i
)
2024-02-29 17:02:56 -08:00
})
})
describe('when neither predicate path nor predicate are provided', () => {
2026-02-13 15:20:42 -08:00
it('throws an error', async () => {
const inputs: PredicateInputs = {
...blankInputs,
predicateType: 'https://example.com/predicate'
}
2026-02-13 15:20:42 -08:00
await expect(predicateFromInputs(inputs)).rejects.toThrow(
2024-02-29 17:02:56 -08:00
/one of predicate-path or predicate must be provided/i
)
})
})
describe('when both predicate path and predicate are provided', () => {
2026-02-13 15:20:42 -08:00
it('throws an error', async () => {
const inputs: PredicateInputs = {
predicateType: 'https://example.com/predicate',
predicate: '{}',
predicatePath: 'path/to/predicate'
}
2026-02-13 15:20:42 -08:00
await expect(predicateFromInputs(inputs)).rejects.toThrow(
2024-02-29 17:02:56 -08:00
/only one of predicate-path or predicate may be provided/i
)
})
})
describe('when specifying a predicate path', () => {
const predicateType = 'https://example.com/predicate'
2024-02-29 17:02:56 -08:00
const content = '{}'
let predicatePath = ''
2024-02-29 17:02:56 -08:00
beforeEach(async () => {
// Set-up temp directory
const tmpDir = await fs.realpath(os.tmpdir())
const dir = await fs.mkdtemp(tmpDir + path.sep)
const filename = 'subject'
predicatePath = path.join(dir, filename)
2024-02-29 17:02:56 -08:00
// Write file to temp directory
await fs.writeFile(predicatePath, content)
2024-02-29 17:02:56 -08:00
})
afterEach(async () => {
// Clean-up temp directory
await fs.rm(path.parse(predicatePath).dir, { recursive: true })
2024-02-29 17:02:56 -08:00
})
2026-02-13 15:20:42 -08:00
it('returns the predicate', async () => {
const inputs: PredicateInputs = {
...blankInputs,
predicateType,
predicatePath
}
2026-02-13 15:20:42 -08:00
await expect(predicateFromInputs(inputs)).resolves.toEqual({
type: predicateType,
params: JSON.parse(content)
2024-02-29 17:02:56 -08:00
})
})
})
2024-06-03 09:41:25 -07:00
describe('when specifying a predicate path that does not exist', () => {
const predicateType = 'https://example.com/predicate'
const predicatePath = 'foo'
2026-02-13 15:20:42 -08:00
it('returns the predicate', async () => {
2024-06-03 09:41:25 -07:00
const inputs: PredicateInputs = {
...blankInputs,
predicateType,
predicatePath
}
2026-02-13 15:20:42 -08:00
await expect(predicateFromInputs(inputs)).rejects.toThrow(
/file not found/
)
2024-06-03 09:41:25 -07:00
})
})
2024-02-29 17:02:56 -08:00
describe('when specifying a predicate value', () => {
const predicateType = 'https://example.com/predicate'
2024-02-29 17:02:56 -08:00
const content = '{}'
2026-02-13 15:20:42 -08:00
it('returns the predicate', async () => {
const inputs: PredicateInputs = {
...blankInputs,
predicateType,
predicate: content
}
2026-02-13 15:20:42 -08:00
await expect(predicateFromInputs(inputs)).resolves.toEqual({
type: predicateType,
params: JSON.parse(content)
2024-02-29 17:02:56 -08:00
})
})
})
2024-06-03 09:41:25 -07:00
describe('when specifying a predicate value exceeding the max size', () => {
const predicateType = 'https://example.com/predicate'
const content = JSON.stringify({ a: 'a'.repeat(16 * 1024 * 1024) })
2026-02-13 15:20:42 -08:00
it('throws an error', async () => {
2024-06-03 09:41:25 -07:00
const inputs: PredicateInputs = {
...blankInputs,
predicateType,
predicate: content
}
2026-02-13 15:20:42 -08:00
await expect(predicateFromInputs(inputs)).rejects.toThrow(
2024-06-03 09:41:25 -07:00
/predicate string exceeds maximum/
)
})
})
2024-02-29 17:02:56 -08:00
})