test: vitest tests for puter js (#1693)

This commit is contained in:
Daniel Salazar
2025-10-06 15:00:40 -07:00
committed by GitHub
parent f06bec4cd4
commit e3f9455cae
9 changed files with 211 additions and 8 deletions
+61
View File
@@ -0,0 +1,61 @@
// kv.test.ts - Tests for Puter KV module (set, get, del, incr, decr, list, flush)
import { describe, expect, it } from 'vitest';
import { puter } from './testUtils';
describe('Puter KV Module', () => {
const TEST_KEY = 'test-key';
it('should set a key success', async () => {
await expect(puter.kv.set(TEST_KEY, 0)).resolves.toBe(true);
});
it('should get a key success', async () => {
const getRes = await puter.kv.get(TEST_KEY);
expect(getRes).toBe(0);
});
it('should get empty key', async () => {
const emptyRes = await puter.kv.get('fake' + TEST_KEY)
expect(emptyRes).toBeNull();
});
it('should increment a key success', async () => {
const getRes = await puter.kv.get(TEST_KEY);
expect(getRes).toBe(0);
const incrRes = await puter.kv.incr(TEST_KEY, { '': 5 });
console.log('incrRes', incrRes);
expect(incrRes).toBe(5);
const finalGet = await puter.kv.get(TEST_KEY);
expect(finalGet).toBe(5);
});
it('should decrement a key success', async () => {
const getRes = await puter.kv.get(TEST_KEY);
expect(getRes).toBe(5);
const decrRes = await puter.kv.decr(TEST_KEY, { '': 3 });
console.log('decrRes', decrRes);
expect(decrRes).toBe(2);
const finalGet = await puter.kv.get(TEST_KEY);
expect(finalGet).toBe(2);
});
it('should list keys', async () => {
const listRes = await puter.kv.list();
expect(Array.isArray(listRes)).toBe(true);
expect(listRes.length).toBeGreaterThan(0);
expect((listRes as string[]).includes(TEST_KEY)).toBe(true);
});
// delete ops should go last
it('should flush all keys', async () => {
const flushRes = await puter.kv.flush()
expect(flushRes).toBe(true);
const postFlushList = await puter.kv.list();
expect(Array.isArray(postFlushList)).toBe(true);
expect(postFlushList.length).toBe(0);
});
it('should delete a key success', async () => {
const setRes = await puter.kv.set(TEST_KEY, 'to-be-deleted');
expect(setRes).toBe(true);
const delRes = await puter.kv.del(TEST_KEY);
expect(delRes).toBe(true);
const getRes = await puter.kv.get(TEST_KEY);
expect(getRes).toBeNull();
});
});
+7
View File
@@ -0,0 +1,7 @@
// setup.ts - Vitest global setup for Puter API tests (TypeScript)
import { beforeAll } from 'vitest';
beforeAll(async () => {
// Setup global mocks or environment if needed
// Example: globalThis.fetch = ...
});
+16
View File
@@ -0,0 +1,16 @@
// testUtils.ts - Puter.js API test utilities (TypeScript)
import type { Puter } from '../../src/puter-js';
// Create and configure a global puter instance from environment variables
// Usage: import { puter } from './testUtils'
// Environment variables: PUTER_AUTH_TOKEN, PUTER_API_ORIGIN, PUTER_ORIGIN
// @ts-ignore
const puter: Puter = require('../../src/puter-js/src/index.js').default || globalThis.puter;
globalThis.PUTER_ORIGIN = process.env.PUTER_ORIGIN || 'https://puter.com';
globalThis.PUTER_API_ORIGIN = process.env.PUTER_API_ORIGIN || 'https://api.puter.com';
if (process.env.PUTER_API_ORIGIN) (puter as any).setAPIOrigin(process.env.PUTER_API_ORIGIN);
if (process.env.PUTER_ORIGIN) (puter as any).defaultGUIOrigin = process.env.PUTER_ORIGIN;
if (process.env.PUTER_AUTH_TOKEN) (puter as any).setAuthToken(process.env.PUTER_AUTH_TOKEN);
export { puter };
+23
View File
@@ -0,0 +1,23 @@
// vite.config.ts - Vite configuration for Puter API tests (TypeScript)
import { defineConfig, loadEnv } from 'vite';
import { viteStaticCopy } from 'vite-plugin-static-copy';
export default defineConfig(({ mode }) => ({
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./setup.ts'],
coverage: {
reporter: ['text', 'json', 'html'],
exclude: ['setup.ts', 'testUtils.ts'],
},
env: loadEnv(mode, "", "PUTER_"),
},
plugins: [
viteStaticCopy({
targets: [
{ src: '../src/puter-js/src/index.js', dest: 'puter-js' },
],
}),
],
}));