@factorialco/shadowdog
Version:
<img src="https://raw.githubusercontent.com/factorialco/shadowdog/refs/heads/main/logo.png" alt="drawing" width="100"/>
510 lines (509 loc) • 21.9 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const events_1 = require("../events");
const shadowdog_mcp_1 = __importDefault(require("./shadowdog-mcp"));
const fs_1 = require("fs");
const path = __importStar(require("path"));
(0, vitest_1.describe)('shadowdog-mcp', () => {
let eventEmitter;
let testDir;
(0, vitest_1.beforeEach)(() => {
eventEmitter = new events_1.ShadowdogEventEmitter();
testDir = path.join(process.cwd(), 'test-shadowdog-mcp');
// Create test directory
if (!(0, fs_1.existsSync)(testDir)) {
(0, fs_1.mkdirSync)(testDir, { recursive: true });
}
// Change to test directory
process.chdir(testDir);
});
(0, vitest_1.afterEach)(() => {
// Clean up test directory
process.chdir(path.dirname(testDir));
if ((0, fs_1.existsSync)(testDir)) {
(0, fs_1.rmSync)(testDir, { recursive: true, force: true });
}
});
(0, vitest_1.describe)('pause-resume with file change tracking', () => {
(0, vitest_1.it)('should track file changes when paused and replay them on resume', async () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
// Create a test file
const testFile = path.join(testDir, 'test-file.txt');
(0, fs_1.writeFileSync)(testFile, 'initial content');
// Initialize the plugin
eventEmitter.emit('initialized');
// Wait a bit for initialization
await new Promise((resolve) => setTimeout(resolve, 100));
// Simulate pausing shadowdog
eventEmitter.emit('pause');
// Simulate file changes while paused
eventEmitter.emit('changed', { path: testFile, type: 'change' });
eventEmitter.emit('changed', { path: testFile, type: 'change' }); // Duplicate should be ignored
// Simulate resuming shadowdog
eventEmitter.emit('resume');
// The file should have been touched (we can verify this by checking if the file exists and was modified)
(0, vitest_1.expect)((0, fs_1.existsSync)(testFile)).toBe(true);
});
(0, vitest_1.it)('should not track file changes when not paused', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
// Create a test file
const testFile = path.join(testDir, 'test-file.txt');
(0, fs_1.writeFileSync)(testFile, 'initial content');
// Initialize the plugin
eventEmitter.emit('initialized');
// Simulate file changes when not paused (should not be tracked)
eventEmitter.emit('changed', { path: testFile, type: 'change' });
// Simulate resuming (should not replay anything)
eventEmitter.emit('resume');
// File should still exist
(0, vitest_1.expect)((0, fs_1.existsSync)(testFile)).toBe(true);
});
});
(0, vitest_1.describe)('plugin initialization', () => {
(0, vitest_1.it)('should register event listeners when initialized', () => {
const options = {};
(0, vitest_1.expect)(() => {
shadowdog_mcp_1.default.listener(eventEmitter, options);
}).not.toThrow();
});
(0, vitest_1.it)('should handle configLoaded event', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git', '**/node_modules'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: ['NODE_ENV'],
ignored: [],
commands: [
{
command: 'echo test',
workingDirectory: '',
tags: [],
artifacts: [
{
output: 'dist/output.js',
},
],
},
],
},
],
};
(0, vitest_1.expect)(() => {
eventEmitter.emit('configLoaded', { config: mockConfig });
}).not.toThrow();
});
});
(0, vitest_1.describe)('pause/resume functionality', () => {
(0, vitest_1.it)('should handle begin event when not paused', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockArtifacts = [{ output: 'dist/output.js' }];
// Should not throw when emitting begin event
(0, vitest_1.expect)(() => {
eventEmitter.emit('begin', { artifacts: mockArtifacts });
}).not.toThrow();
});
(0, vitest_1.it)('should handle config with multiple watchers', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: [],
ignored: [],
commands: [
{
command: 'tsc',
workingDirectory: '',
tags: [],
artifacts: [{ output: 'dist/app.js' }],
},
],
},
{
enabled: true,
files: ['styles/**/*.css'],
environment: [],
ignored: [],
commands: [
{
command: 'postcss',
workingDirectory: '',
tags: [],
artifacts: [{ output: 'dist/styles.css' }],
},
],
},
],
};
eventEmitter.emit('configLoaded', { config: mockConfig });
(0, vitest_1.expect)(() => {
eventEmitter.emit('begin', { artifacts: [{ output: 'dist/app.js' }] });
}).not.toThrow();
});
});
(0, vitest_1.describe)('lock file integration', () => {
(0, vitest_1.it)('should handle missing lock file gracefully', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: [],
ignored: [],
commands: [
{
command: 'echo test',
workingDirectory: '',
tags: [],
artifacts: [{ output: 'dist/output.js' }],
},
],
},
],
};
eventEmitter.emit('configLoaded', { config: mockConfig });
// Should not throw even though lock file doesn't exist
(0, vitest_1.expect)(() => {
eventEmitter.emit('initialized');
}).not.toThrow();
});
(0, vitest_1.it)('should read lock file when it exists', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
// Create a mock lock file
const lockFile = {
version: '0.8.5',
nodeVersion: process.version,
artifacts: [
{
output: 'dist/output.js',
outputSha: 'abc123',
cacheIdentifier: 'cache-123',
fileManifest: {
watchedFilesCount: 1,
watchedFiles: ['src/index.ts'],
environment: {},
command: 'tsc',
},
},
],
};
(0, fs_1.writeFileSync)('shadowdog-lock.json', JSON.stringify(lockFile, null, 2));
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: [],
ignored: [],
commands: [
{
command: 'tsc',
workingDirectory: '',
tags: [],
artifacts: [{ output: 'dist/output.js' }],
},
],
},
],
};
eventEmitter.emit('configLoaded', { config: mockConfig });
// Should not throw when reading lock file
(0, vitest_1.expect)((0, fs_1.existsSync)('shadowdog-lock.json')).toBe(true);
});
});
(0, vitest_1.describe)('event handling', () => {
(0, vitest_1.it)('should handle exit event', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
(0, vitest_1.expect)(() => {
eventEmitter.emit('exit', 0);
}).not.toThrow();
});
(0, vitest_1.it)('should handle end event', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
(0, vitest_1.expect)(() => {
eventEmitter.emit('end', { artifacts: [{ output: 'dist/output.js' }] });
}).not.toThrow();
});
(0, vitest_1.it)('should handle error event', () => {
const options = {};
// Add an error handler to prevent unhandled error
eventEmitter.on('error', () => {
// Error handled
});
shadowdog_mcp_1.default.listener(eventEmitter, options);
(0, vitest_1.expect)(() => {
eventEmitter.emit('error', {
artifacts: [{ output: 'dist/output.js' }],
errorMessage: 'Test error',
});
}).not.toThrow();
});
});
(0, vitest_1.describe)('artifact tracking', () => {
(0, vitest_1.it)('should track artifacts from config', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: ['NODE_ENV'],
ignored: [],
commands: [
{
command: 'tsc',
workingDirectory: '',
tags: [],
artifacts: [
{ output: 'dist/output1.js' },
{ output: 'dist/output2.js', description: 'Second output' },
],
},
],
},
],
};
eventEmitter.emit('configLoaded', { config: mockConfig });
// Verify artifacts are tracked (implicitly through no errors)
(0, vitest_1.expect)(() => {
eventEmitter.emit('begin', { artifacts: mockConfig.watchers[0].commands[0].artifacts });
}).not.toThrow();
});
(0, vitest_1.it)('should handle artifacts with descriptions', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: [],
ignored: [],
commands: [
{
command: 'tsc',
workingDirectory: '',
tags: [],
artifacts: [
{
output: 'dist/output.js',
description: 'Compiled TypeScript output',
},
],
},
],
},
],
};
eventEmitter.emit('configLoaded', { config: mockConfig });
(0, vitest_1.expect)(() => {
eventEmitter.emit('begin', { artifacts: mockConfig.watchers[0].commands[0].artifacts });
}).not.toThrow();
});
});
(0, vitest_1.describe)('integration with other events', () => {
(0, vitest_1.it)('should handle allTasksComplete event', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
(0, vitest_1.expect)(() => {
eventEmitter.emit('allTasksComplete');
}).not.toThrow();
});
(0, vitest_1.it)('should handle generateStarted event', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
(0, vitest_1.expect)(() => {
eventEmitter.emit('generateStarted');
}).not.toThrow();
});
(0, vitest_1.it)('should handle multiple sequential events', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: [],
ignored: [],
commands: [
{
command: 'tsc',
workingDirectory: '',
tags: [],
artifacts: [{ output: 'dist/output.js' }],
},
],
},
],
};
(0, vitest_1.expect)(() => {
eventEmitter.emit('configLoaded', { config: mockConfig });
eventEmitter.emit('generateStarted');
eventEmitter.emit('begin', { artifacts: [{ output: 'dist/output.js' }] });
eventEmitter.emit('end', { artifacts: [{ output: 'dist/output.js' }] });
eventEmitter.emit('allTasksComplete');
}).not.toThrow();
});
});
(0, vitest_1.describe)('compute-all-artifacts functionality', () => {
(0, vitest_1.it)('should handle computeAllArtifacts event when daemon is available', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: [],
ignored: [],
commands: [
{
command: 'tsc',
workingDirectory: '',
tags: [],
artifacts: [{ output: 'dist/app.js' }, { output: 'dist/types.d.ts' }],
},
],
},
],
};
eventEmitter.emit('configLoaded', { config: mockConfig });
// Should not throw when emitting computeAllArtifacts event
(0, vitest_1.expect)(() => {
eventEmitter.emit('computeAllArtifacts', {
artifacts: [{ output: 'dist/app.js' }, { output: 'dist/types.d.ts' }],
});
}).not.toThrow();
});
(0, vitest_1.it)('should handle computeAllArtifacts event with multiple watchers', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
const mockConfig = {
debounceTime: 2000,
defaultIgnoredFiles: ['.git'],
plugins: [],
watchers: [
{
enabled: true,
files: ['src/**/*.ts'],
environment: [],
ignored: [],
commands: [
{
command: 'tsc',
workingDirectory: '',
tags: [],
artifacts: [{ output: 'dist/app.js' }],
},
],
},
{
enabled: true,
files: ['styles/**/*.css'],
environment: [],
ignored: [],
commands: [
{
command: 'postcss',
workingDirectory: '',
tags: [],
artifacts: [{ output: 'dist/styles.css' }],
},
],
},
],
};
eventEmitter.emit('configLoaded', { config: mockConfig });
// Should not throw when emitting computeAllArtifacts event with multiple watchers
(0, vitest_1.expect)(() => {
eventEmitter.emit('computeAllArtifacts', {
artifacts: [{ output: 'dist/app.js' }, { output: 'dist/styles.css' }],
});
}).not.toThrow();
});
(0, vitest_1.it)('should handle computeAllArtifacts event with empty artifacts array', () => {
const options = {};
shadowdog_mcp_1.default.listener(eventEmitter, options);
// Should not throw when emitting computeAllArtifacts event with empty array
(0, vitest_1.expect)(() => {
eventEmitter.emit('computeAllArtifacts', { artifacts: [] });
}).not.toThrow();
});
});
});