test: add test infra to run vitest on all of backend wip (#2023)

This commit is contained in:
Daniel Salazar
2025-11-24 19:20:09 -08:00
committed by GitHub
parent 8eee9ad1df
commit e2f1200ad5
13 changed files with 1084 additions and 63 deletions

View File

@@ -56,10 +56,34 @@ const rules = {
};
export default defineConfig([
// TypeScript support for tests
{
files: ['**/*.test.ts', '**/*.test.mts', '**/*.test.setup.ts'],
ignores: ['tests/playwright/tests/**/*.ts'],
languageOptions: {
parser: tseslintParser,
globals: { ...globals.node, ...globals.vitest },
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: './tests/tsconfig.json',
},
},
plugins: {
'@typescript-eslint': tseslintPlugin,
},
rules: {
// Recommended rules for TypeScript
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', caughtErrors: 'none' }],
'@typescript-eslint/ban-ts-comment': 'warn',
'@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
},
},
// TypeScript support block
{
files: ['**/*.ts'],
ignores: ['tests/**/*.ts', 'extensions/**/*.ts'],
ignores: ['**/*.test.ts', '**/*.test.mts', 'extensions/**/*.ts'],
languageOptions: {
parser: tseslintParser,
parserOptions: {
@@ -99,40 +123,19 @@ export default defineConfig([
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', caughtErrors: 'none' }],
'@typescript-eslint/ban-ts-comment': 'warn',
'@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
} },
// TypeScript support for tests
{
files: ['tests/**/*.ts'],
ignores: ['tests/playwright/tests/**/*.ts'],
languageOptions: {
parser: tseslintParser,
globals: { ...globals.jest, ...globals.node },
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: './tests/tsconfig.json',
},
},
plugins: {
'@typescript-eslint': tseslintPlugin,
},
rules: {
// Recommended rules for TypeScript
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', caughtErrors: 'none' }],
'@typescript-eslint/ban-ts-comment': 'warn',
'@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
} },
},
{
plugins: {
js,
'@stylistic': stylistic,
custom: { rules: {
'control-structure-spacing': controlStructureSpacing,
'bang-space-if': bangSpaceIf,
'space-unary-ops-with-exception': spaceUnaryOpsWithException,
} },
custom: {
rules: {
'control-structure-spacing': controlStructureSpacing,
'bang-space-if': bangSpaceIf,
'space-unary-ops-with-exception': spaceUnaryOpsWithException,
},
},
},
},
{
@@ -143,6 +146,8 @@ export default defineConfig([
],
ignores: [
'**/*.test.js',
'**/*.test.ts',
'**/*.test.mts',
],
languageOptions: { globals: globals.node },
rules,
@@ -155,8 +160,10 @@ export default defineConfig([
{
files: [
'**/*.test.js',
'**/*.test.ts',
'**/*.test.mts',
],
languageOptions: { globals: { ...globals.jest, ...globals.node } },
languageOptions: { globals: { ...globals.node, ...globals.vitest } },
rules,
plugins: {
js,

942
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -42,6 +42,7 @@
"scripts": {
"test": "npx mocha src/phoenix/test && npx vitest run src/backend && node src/backend/tools/test",
"test:puterjs-api": "vitest run tests/puterJsApiTests",
"test:backend": "vitest --config=src/backend/vitest.config.mts src/backend",
"start=gui": "nodemon --exec \"node dev-server.js\" ",
"start": "node ./tools/run-selfhosted.js",
"prestart": "npm run build:ts",

View File

@@ -94,6 +94,7 @@
"devDependencies": {
"@types/node": "^20.5.3",
"chai": "^4.3.7",
"jsdom": "^27.2.0",
"mocha": "^10.2.0",
"nodemon": "^3.1.0",
"nyc": "^15.1.0",

View File

@@ -0,0 +1,9 @@
import { describe, expect } from 'vitest';
import { testKernel } from '../../../test.setup.mjs';
describe('MeteringService', () => {
it('should have some services', () => {
expect(testKernel.services).not.toBeUndefined();
});
});

View File

@@ -0,0 +1,10 @@
// setup.ts - Vitest global setup for Puter API tests (TypeScript)
import { beforeAll } from 'vitest';
import { k } from './tools/test.mjs';
let testKernel = {};
beforeAll(async () => {
console.log("initted with kernel:", k);
testKernel = await k;
});
export { testKernel };
//# sourceMappingURL=test.setup.mjs.map

View File

@@ -0,0 +1,14 @@
// setup.ts - Vitest global setup for Puter API tests (TypeScript)
import { beforeAll } from 'vitest';
// @ts-ignore
import { Kernel } from './src/Kernel.js';
// @ts-ignore
import {k} from './tools/test.mjs';
let testKernel = {} as Kernel;
beforeAll(async () => {
console.log("initted with kernel:" ,k)
testKernel = await k;
});
export { testKernel };

View File

@@ -17,14 +17,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const { AdvancedBase } = require('@heyputer/putility');
const useapi = require('useapi');
const { BaseService, EssentialModules } = require('../exports');
const CoreModule = require('../src/CoreModule');
const { Context } = require('../src/util/context');
const { Kernel } = require('../src/Kernel');
const { HTTPThumbnailService } = require('../src/services/thumbnails/HTTPThumbnailService');
const { RuntimeModuleRegistry } = require('../src/extension/RuntimeModuleRegistry');
import { AdvancedBase } from '@heyputer/putility';
import useapi from 'useapi';
import { BaseService, EssentialModules } from '../exports';
import { RuntimeModuleRegistry } from '../src/extension/RuntimeModuleRegistry';
import { Kernel } from '../src/Kernel';
import { HTTPThumbnailService } from '../src/services/thumbnails/HTTPThumbnailService';
import { Context } from '../src/util/context';
/**
* A simple implementation of the log interface for the test kernel.
@@ -65,7 +64,9 @@ class TestKernel extends AdvancedBase {
* @returns {void}
*/
this.useapi.withuse(() => {
// eslint-disable-next-line no-undef
def('Module', AdvancedBase);
// eslint-disable-next-line no-undef
def('Service', BaseService);
});
@@ -87,7 +88,7 @@ class TestKernel extends AdvancedBase {
const { consoleLogManager } = require('../src/util/consolelog');
consoleLogManager.initialize_proxy_methods();
consoleLogManager.decorate_all(({ manager, replace }, ...a) => {
consoleLogManager.decorate_all(({ _manager, replace }, ...a) => {
replace(...this.logfn_(...a));
});
@@ -151,7 +152,7 @@ class TestKernel extends AdvancedBase {
TestKernel.prototype._create_mod_context =
Kernel.prototype._create_mod_context;
const k = new TestKernel();
export const k = new TestKernel();
for ( const mod of EssentialModules ) {
k.add_module(new mod());
}

View File

@@ -0,0 +1,20 @@
// vite.config.ts - Vite configuration for Puter API tests (TypeScript)
import { loadEnv } from 'vite';
import { defineConfig } from 'vitest/config';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default defineConfig(({ mode }) => ({
test: {
globals: true,
environment: 'jsdom',
setupFiles: [path.resolve(__dirname, './test.setup.mts')],
coverage: {
reporter: ['text', 'json', 'html'],
exclude: [path.resolve(__dirname, './test.setup.mts')],
},
env: loadEnv(mode, '', 'PUTER_'),
},
}));
//# sourceMappingURL=vitest.config.mjs.map

View File

@@ -0,0 +1,21 @@
// vite.config.ts - Vite configuration for Puter API tests (TypeScript)
import {loadEnv} from 'vite';
import { defineConfig } from 'vitest/config';
import { fileURLToPath, URL } from 'node:url';
import path from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default defineConfig(({ mode }) => ({
test: {
globals: true,
environment: 'jsdom',
setupFiles: [path.resolve(__dirname, './test.setup.mts')],
coverage: {
reporter: ['text', 'json', 'html'],
exclude: [path.resolve(__dirname, './test.setup.mts')],
},
env: loadEnv(mode, '', 'PUTER_'),
},
}));

View File

@@ -13,7 +13,7 @@ describe('Puter KV Module', () => {
expect(getRes).toBe(0);
});
it('should get empty key', async () => {
const emptyRes = await puter.kv.get('fake' + TEST_KEY)
const emptyRes = await puter.kv.get(`fake${ TEST_KEY}`);
expect(emptyRes).toBeNull();
});
@@ -39,7 +39,7 @@ describe('Puter KV Module', () => {
expect(incrRes).toBe(1);
const finalGet = await puter.kv.get(TEST_KEY);
expect(finalGet).toBe(1);
})
});
it('should decrement a key with second argument', async () => {
await puter.kv.set(TEST_KEY, 0);
@@ -47,7 +47,7 @@ describe('Puter KV Module', () => {
expect(incrRes).toBe(-1);
const finalGet = await puter.kv.get(TEST_KEY);
expect(finalGet).toBe(-1);
})
});
it('should increment a key with second argument', async () => {
await puter.kv.set(TEST_KEY, 0);
@@ -55,7 +55,7 @@ describe('Puter KV Module', () => {
expect(incrRes).toBe(2);
const finalGet = await puter.kv.get(TEST_KEY);
expect(finalGet).toBe(2);
})
});
it('should decrement a key with second argument', async () => {
await puter.kv.set(TEST_KEY, 0);
@@ -63,7 +63,7 @@ describe('Puter KV Module', () => {
expect(incrRes).toBe(-3);
const finalGet = await puter.kv.get(TEST_KEY);
expect(finalGet).toBe(-3);
})
});
it('should increment a key with nested path', async () => {
await puter.kv.set(TEST_KEY, { a: { b: 0 } });
@@ -71,7 +71,7 @@ describe('Puter KV Module', () => {
expect(incrRes).toEqual({ a: { b: 1 } });
const finalGet = await puter.kv.get(TEST_KEY);
expect(finalGet).toEqual({ a: { b: 1 } });
})
});
it('should decrement a key with nested path', async () => {
await puter.kv.set(TEST_KEY, { a: { b: 0 } });
@@ -79,21 +79,21 @@ describe('Puter KV Module', () => {
expect(incrRes).toEqual({ a: { b: -1 } });
const finalGet = await puter.kv.get(TEST_KEY);
expect(finalGet).toEqual({ a: { b: -1 } });
})
});
it('should increment a nonexistent key with nested path', async () => {
const incrRes = await puter.kv.incr(TEST_KEY + 1, { 'a.b': 1 });
expect(incrRes).toEqual({ a: { b: 1 } });
const finalGet = await puter.kv.get(TEST_KEY + 1);
expect(finalGet).toEqual({ a: { b: 1 } });
})
});
it('should decrement a nonexistent key with nested path', async () => {
const incrRes = await puter.kv.decr(TEST_KEY + 2, { 'a.b': 1 });
expect(incrRes).toEqual({ a: { b: -1 } });
const finalGet = await puter.kv.get(TEST_KEY + 2);
expect(finalGet).toEqual({ a: { b: -1 } });
})
});
it('should list keys', async () => {
const listRes = await puter.kv.list();
@@ -103,7 +103,7 @@ describe('Puter KV Module', () => {
});
// delete ops should go last
it('should flush all keys', async () => {
const flushRes = await puter.kv.flush()
const flushRes = await puter.kv.flush();
expect(flushRes).toBe(true);
const postFlushList = await puter.kv.list();
expect(Array.isArray(postFlushList)).toBe(true);

View File

@@ -1,11 +1,18 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": ".."
"rootDir": "..",
"target": "es6",
"module": "es6",
"moduleResolution": "bundler",
"outDir": "build",
"esModuleInterop": true
},
"include": [
"**/*.ts",
"**/*.tsx"
"../**/*.test.mts",
"../**/*.test.ts",
"../src/backend/test.setup.mts"
],
"exclude": [
"node_modules"

View File

@@ -12,7 +12,13 @@
},
"exclude": [
"**/*.test.ts",
"**/*.test.setup.ts",
"**/*.test.setup.mts",
"**/*.vitest.config.ts",
"**/*.vitest.config.mts",
"**/*.test.mts",
"**/*.spec.ts",
"**/*.spec.mts",
"**/test/**",
"**/tests/**",
"node_modules",