BREAKING CHANGE(tswatch): refactor tswatch to a config-driven design (load config from npmextra.json) and add interactive init wizard; change TsWatch public API and enhance Watcher behavior

This commit is contained in:
2026-01-24 11:29:55 +00:00
parent 5a702055f4
commit 9fb3f6a872
20 changed files with 3686 additions and 4431 deletions

View File

@@ -1,20 +1,105 @@
// tslint:disable-next-line: no-implicit-dependencies
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as tswatch from '../ts/index.js';
let testTsWatchInstance: tswatch.TsWatch;
// ============================================
// ConfigHandler Tests
// ============================================
tap.test('should create a valid TsWatch instance', async () => {
testTsWatchInstance = new tswatch.TsWatch('echo');
tap.test('ConfigHandler: should return all preset names', async () => {
const configHandler = new tswatch.ConfigHandler();
const presetNames = configHandler.getPresetNames();
expect(presetNames).toContain('npm');
expect(presetNames).toContain('test');
expect(presetNames).toContain('service');
expect(presetNames).toContain('element');
expect(presetNames).toContain('website');
expect(presetNames.length).toEqual(5);
});
tap.test('should start the tswatch instance', async () => {
await testTsWatchInstance.start();
tap.test('ConfigHandler: should return npm preset with watchers', async () => {
const configHandler = new tswatch.ConfigHandler();
const preset = configHandler.getPreset('npm');
expect(preset).toBeTruthy();
expect(preset.watchers).toBeTruthy();
expect(preset.watchers.length).toBeGreaterThan(0);
expect(preset.watchers[0].name).toEqual('npm-test');
expect(preset.watchers[0].command).toEqual('npm run test');
});
tap.test('should stop the instance', async (tools) => {
tools.delayFor(2000);
testTsWatchInstance.stop();
tap.test('ConfigHandler: should return element preset with server', async () => {
const configHandler = new tswatch.ConfigHandler();
const preset = configHandler.getPreset('element');
expect(preset).toBeTruthy();
expect(preset.server).toBeTruthy();
expect(preset.server.enabled).toBeTrue();
expect(preset.server.port).toEqual(3002);
expect(preset.bundles).toBeTruthy();
expect(preset.bundles.length).toBeGreaterThan(0);
});
tap.start();
tap.test('ConfigHandler: should return null for invalid preset', async () => {
const configHandler = new tswatch.ConfigHandler();
const preset = configHandler.getPreset('invalid');
expect(preset).toBeNull();
});
tap.test('ConfigHandler: should return correct config key', async () => {
const configHandler = new tswatch.ConfigHandler();
expect(configHandler.getConfigKey()).toEqual('@git.zone/tswatch');
});
// ============================================
// Watcher Tests
// ============================================
tap.test('Watcher: should create from config with defaults', async () => {
const watcher = tswatch.Watcher.fromConfig({
name: 'test-watcher',
watch: './ts/**/*',
command: 'echo test',
});
expect(watcher).toBeInstanceOf(tswatch.Watcher);
});
tap.test('Watcher: should handle array watch patterns', async () => {
const watcher = tswatch.Watcher.fromConfig({
name: 'multi-watch',
watch: ['./ts/**/*', './test/**/*'],
command: 'echo test',
});
expect(watcher).toBeInstanceOf(tswatch.Watcher);
});
// ============================================
// TsWatch Tests
// ============================================
let testTsWatch: tswatch.TsWatch;
tap.test('TsWatch: should create with minimal config', async () => {
testTsWatch = new tswatch.TsWatch({
watchers: [
{
name: 'echo-test',
watch: './ts/**/*',
command: 'echo "test"',
runOnStart: false,
},
],
});
expect(testTsWatch).toBeInstanceOf(tswatch.TsWatch);
expect(testTsWatch.config.watchers).toBeTruthy();
expect(testTsWatch.config.watchers.length).toEqual(1);
});
tap.test('TsWatch: should start and populate watcherMap', async () => {
await testTsWatch.start();
expect(testTsWatch.watcherMap.getArray().length).toBeGreaterThan(0);
});
tap.test('TsWatch: should stop cleanly', async (tools) => {
await tools.delayFor(500);
await testTsWatch.stop();
});
export default tap.start();