This commit is contained in:
2025-08-14 17:31:04 +02:00
parent 5fc177a1e7
commit 95be9d8d4a
4 changed files with 42 additions and 73 deletions
+13 -19
View File
@@ -1,9 +1,10 @@
import {expect, test, describe, beforeEach, afterEach} from 'bun:test'
import {type TDate, type TSchema, Type} from "@sinclair/typebox"
import {type TSchema, Type} from "@sinclair/typebox"
import {type FileResult, fileSync} from 'tmp'
import * as fs from 'node:fs/promises'
import {z} from 'zod/v4'
import {read_date, read_object, read_type} from '../src/read_type'
import {read_object, read_type} from '../src/read_type'
import {parse} from '..'
beforeEach(() => process.env = {})
@@ -21,6 +22,16 @@ async function testing<T extends TSchema>(value: string | undefined, scheme: T,
}
}
test('tweak', async () => {
const ts = '2025-06-11 09:36:12'
process.env['NAME'] = 'coucou'
process.env['TS'] = ts
process.env['PORT'] = '212'
const scheme = z.object({name: z.string(), port: z.number(), ts: z.date()})
const res = await read_object(scheme)
expect(res).toEqual({ok: true, data: {name: 'coucou', port: 212, ts: new Date(ts)}})
})
// Most important function : read_object
describe('Object', () => {
test('basic', async () => {
@@ -219,22 +230,5 @@ describe('Enum', () => {
})
// Read specific types
describe('Date', () => {
async function testing<T extends TDate>(value: string | undefined, scheme: T, should_be_successful: boolean, data?: any) {
const key = 'DB_PORT'
if (value !== undefined) process.env[key] = value
const res = await read_date(scheme, key)
if (should_be_successful) {
expect(res).toEqual({ok: true, data})
} else {
expect(res).toEqual({ok: false})
}
}
test('basic', () => testing('2025-06-08 22:40:17', Type.Date(), true, new Date('2025-06-08 22:40:17')))
test('optional', () => testing(undefined, Type.Optional(Type.Date()), true))
})
describe.todo('Array')
describe.todo('Tuple')