fix(tapbundle_serverside): add TapNodeTools cleanup hooks to stop SmartNetwork processes after tests

This commit is contained in:
2026-03-27 18:23:42 +00:00
parent 7490da22c0
commit 2946169360
10 changed files with 151 additions and 152 deletions

View File

@@ -1,12 +1,18 @@
import { TestFileProvider } from './classes.testfileprovider.js';
import * as plugins from './plugins.js';
class TapNodeTools {
export interface ITapCleanupTarget {
registerCleanup: (fn: () => Promise<void> | void) => void;
}
export class TapNodeTools {
private smartshellInstance: plugins.smartshell.Smartshell;
private smartnetworkInstance: plugins.smartnetwork.SmartNetwork;
public testFileProvider = new TestFileProvider();
constructor() {}
constructor(tapArg: ITapCleanupTarget) {
tapArg.registerCleanup(() => this.cleanup());
}
private qenv: plugins.qenv.Qenv;
public async getQenv(): Promise<plugins.qenv.Qenv> {
@@ -75,7 +81,7 @@ class TapNodeTools {
/**
* create and return a smartmongo instance
*/
public async createSmartmongo() {
public async createSmartmongo() {
const smartmongoMod = await import('@push.rocks/smartmongo');
const smartmongoInstance = new smartmongoMod.SmartMongo();
await smartmongoInstance.start();
@@ -217,6 +223,15 @@ class TapNodeTools {
`Could not find ${countArg} consecutive free ports in range ${options.startPort}-${options.endPort}`
);
}
}
export const tapNodeTools = new TapNodeTools();
/**
* Clean up long-lived resources (e.g. the SmartNetwork Rust bridge process)
* so the Node.js event loop can drain naturally.
*/
public async cleanup() {
if (this.smartnetworkInstance) {
await this.smartnetworkInstance.stop();
this.smartnetworkInstance = null;
}
}
}

View File

@@ -30,8 +30,11 @@ For reporting bugs, issues, or security vulnerabilities, please visit [community
## Basic Usage
```typescript
import { tapNodeTools } from '@git.zone/tstest/tapbundle_serverside';
import { tap, expect } from '@git.zone/tstest/tapbundle';
import { TapNodeTools } from '@git.zone/tstest/tapbundle_serverside';
// Create an instance and pass `tap` so cleanup is automatic
const tapNodeTools = new TapNodeTools(tap);
tap.test('should start server on free port', async () => {
const port = await tapNodeTools.findFreePort();
@@ -41,11 +44,15 @@ tap.test('should start server on free port', async () => {
export default tap.start();
```
### How cleanup works
`TapNodeTools` accepts any object with a `registerCleanup()` method (the `tap` instance). On construction, it registers a cleanup callback that stops long-lived background processes (e.g. the SmartNetwork Rust bridge). When `tap.start()` finishes running all tests, it calls all registered cleanup functions automatically, so the Node.js event loop can drain and the process exits cleanly.
## API Reference
### tapNodeTools
### TapNodeTools
The main singleton instance providing all Node.js-specific utilities.
Construct with a `tap` instance to enable automatic cleanup:
---