@truestamp/client
Version:
## Description
144 lines (118 loc) • 4.06 kB
text/typescript
// Copyright © 2020-2023 Truestamp Inc. All rights reserved.
import { describe, test, expect } from 'vitest'
import { TruestampClient } from '../src/index'
import { dotenvLoad } from 'dotenv-mono'
dotenvLoad()
import { ItemRequest, ItemResponseSchema } from 'types'
describe('createItem', () => {
const truestamp = new TruestampClient({
apiKey: process.env.TRUESTAMP_API_KEY ?? '',
apiBaseUrl: process.env.TRUESTAMP_API_BASE_URL,
})
test('returns a new Item ID', async () => {
const newItem: ItemRequest = {
itemData: [
{
hash: 'c15fbfedf73881e7264ccefbabdcb679d247348e35dea14eba1d906c174c3e8e',
hashType: 'sha-256',
},
],
}
const r = await truestamp.createItem(newItem)
// throws if not valid
ItemResponseSchema.parse(r)
expect(r).toHaveProperty('id')
expect(r.id).toContain('Tx')
})
test('should throw with a clean error if the input is rejected by the client', async () => {
const badItem: ItemRequest = {
itemData: [
{
hash: 'XYZ',
hashType: 'sha-256',
},
],
}
await expect(truestamp.createItem(badItem)).rejects.toThrow(
'Validation error: Invalid at "itemData[0].hash"',
)
})
test('should throw with a clean error if the input is rejected by the server', async () => {
// This hash is 33 bytes long, which is not allowed for sha-256
// The server will return a 400 error.
// The client validation does not reject this since it is a valid hex string between 20 and 64 bytes.
const badItem: ItemRequest = {
itemData: [
{
hash: 'DDc15fbfedf73881e7264ccefbabdcb679d247348e35dea14eba1d906c174c3e8e', // 33 bytes
hashType: 'sha-256',
},
],
}
await expect(truestamp.createItem(badItem)).rejects.toThrow(
'HTTP fetch error : 400 Bad Request : Bad Request : invalid hash length 33 for hashType sha-256',
)
})
})
describe('updateItem', () => {
const truestamp = new TruestampClient({
apiKey: process.env.TRUESTAMP_API_KEY ?? '',
apiBaseUrl: process.env.TRUESTAMP_API_BASE_URL,
})
test('returns an updated Item ID', async () => {
const newItem: ItemRequest = {
itemData: [
{
hash: 'c15fbfedf73881e7264ccefbabdcb679d247348e35dea14eba1d906c174c3e8e',
hashType: 'sha-256',
},
],
}
const r = await truestamp.createItem(newItem)
// throws if not valid
ItemResponseSchema.parse(r)
expect(r).toHaveProperty('id')
expect(r.id).toContain('Tx')
const updatedItem: ItemRequest = {
itemData: [
{
hash: 'c5325f973103c77e5e703ab88ab96f5116f74b3092287b19f95344eb5c7cd177',
hashType: 'sha-256',
},
],
}
const ru = await truestamp.updateItem(r.id, updatedItem)
expect(ru).toHaveProperty('id')
expect(ru.id).toContain('Tx')
expect(r.id).not.toEqual(ru.id)
})
test('should throw with a clean error if the input is rejected by the client', async () => {
const badItem: ItemRequest = {
itemData: [
{
hash: 'XYZ',
hashType: 'sha-256',
},
],
}
await expect(truestamp.createItem(badItem)).rejects.toThrow(
'Validation error: Invalid at "itemData[0].hash"',
)
})
test('should throw with a clean error if the input is rejected by the server', async () => {
// This hash is 33 bytes long, which is not allowed for sha-256
// The server will return a 400 error.
// The client validation does not reject this since it is a valid hex string between 20 and 64 bytes.
const badItem: ItemRequest = {
itemData: [
{
hash: 'DDc15fbfedf73881e7264ccefbabdcb679d247348e35dea14eba1d906c174c3e8e', // 33 bytes
hashType: 'sha-256',
},
],
}
await expect(truestamp.createItem(badItem)).rejects.toThrow(
'HTTP fetch error : 400 Bad Request : Bad Request : invalid hash length 33 for hashType sha-256',
)
})
})