shrimp/src/tests/shrimp.test.ts
2025-11-05 14:18:53 -08:00

53 lines
1.6 KiB
TypeScript

import { describe } from 'bun:test'
import { expect, test } from 'bun:test'
import { Shrimp } from '..'
describe('Shrimp', () => {
test('allows running Shrimp code', async () => {
const shrimp = new Shrimp()
expect(await shrimp.run(`1 + 5`)).toEqual(6)
expect(await shrimp.run(`type 5`)).toEqual('number')
})
test('maintains state across runs', async () => {
const shrimp = new Shrimp()
await shrimp.run(`abc = true`)
expect(shrimp.get('abc')).toEqual(true)
await shrimp.run(`name = Bob`)
expect(shrimp.get('abc')).toEqual(true)
expect(shrimp.get('name')).toEqual('Bob')
await shrimp.run(`abc = false`)
expect(shrimp.get('abc')).toEqual(false)
})
test('allows setting your own globals', async () => {
const shrimp = new Shrimp({ hiya: () => 'hey there' })
await shrimp.run('abc = hiya')
expect(shrimp.get('abc')).toEqual('hey there')
expect(await shrimp.run('type abc')).toEqual('string')
// still there
expect(await shrimp.run('hiya')).toEqual('hey there')
})
test('allows setting your own locals', async () => {
const shrimp = new Shrimp({ 'my-global': () => 'hey there' })
await shrimp.run('abc = my-global')
expect(shrimp.get('abc')).toEqual('hey there')
await shrimp.run('abc = my-global', { 'my-global': 'now a local' })
expect(shrimp.get('abc')).toEqual('now a local')
await shrimp.run('abc = nothing')
expect(shrimp.get('abc')).toEqual('nothing')
await shrimp.run('abc = nothing', { nothing: 'something' })
expect(shrimp.get('abc')).toEqual('something')
await shrimp.run('abc = nothing')
expect(shrimp.get('abc')).toEqual('nothing')
})
})