Files
ponytail/tests/opencode-plugin.test.js
Dang Trung AnandEmeriko 17a466013e feat: publish ponytail as an installable npm package for OpenCode and Pi (#197)
* feat: register slash commands from .opencode/command/*.md

Add parseCommandFile() to read frontmatter-described markdown files
and wire them into opencode's config.command during init.

Extend the config hook to scan .opencode/command/ and register each
.md file as a named slash command.

Update the plugin doc comment to reflect the npm install path
(opencode-ponytail) vs the old relative path.

* chore: rename package to opencode-ponytail

Align package name with npm convention for opencode plugins.
Update keywords to include opencode-plugin and opencode tags.
Fix description back to original correct wording (grammatical
regression introduced during editing).

* chore: add npm metadata and publish workflow

Add author, homepage, repository, bugs, main, exports, files, and
publishConfig fields to package.json for npm publishing.

Add .github/workflows/publish.yml to auto-publish to npm on version
tags (v*) with provenance.

* docs: add npm plugin install for opencode-ponytail

---------

Co-authored-by: Emeriko <dietrich.gebert@gmail.com>
2026-06-24 02:09:40 +02:00

84 lines
3.6 KiB
JavaScript

#!/usr/bin/env node
// Smoke test for the OpenCode adapter: the plugin's hooks behave against the
// real (structural) OpenCode hook shapes. No live OpenCode needed.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { pathToFileURL } = require('url');
// Point the plugin's mode-flag at a temp config home BEFORE it loads — the
// plugin resolves its state path once at load (as it does under a real OpenCode
// process, where XDG_CONFIG_HOME is already set). The dynamic import below runs
// after this assignment, so the ordering holds.
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ponytail-opencode-'));
process.env.XDG_CONFIG_HOME = tmp;
delete process.env.PONYTAIL_DEFAULT_MODE;
const statePath = path.join(tmp, 'opencode', '.ponytail-active');
let loadPlugin, parseCommandFile;
test.before(async () => {
const url = pathToFileURL(path.join(__dirname, '..', '.opencode', 'plugins', 'ponytail.mjs'));
const mod = await import(url);
loadPlugin = mod.default;
parseCommandFile = mod.parseCommandFile;
});
function transform(hooks) {
const output = { system: [] };
return hooks['experimental.chat.system.transform']({ model: {} }, output).then(() => output.system);
}
test('system.transform injects the ruleset at the default mode (full)', async () => {
try { fs.unlinkSync(statePath); } catch (e) {}
const hooks = await loadPlugin({});
const system = await transform(hooks);
assert.equal(system.length, 1);
assert.match(system[0], /PONYTAIL MODE ACTIVE — level: full/);
assert.match(system[0], /lazy senior developer/);
});
test('command.execute.before persists /ponytail ultra, transform follows it', async () => {
const hooks = await loadPlugin({});
await hooks['command.execute.before']({ command: 'ponytail', arguments: 'ultra', sessionID: 's' });
assert.equal(fs.readFileSync(statePath, 'utf8'), 'ultra');
const system = await transform(hooks);
assert.match(system[0], /PONYTAIL MODE ACTIVE — level: ultra/);
});
test('/ponytail off persists off and transform injects nothing', async () => {
const hooks = await loadPlugin({});
await hooks['command.execute.before']({ command: 'ponytail', arguments: 'off', sessionID: 's' });
assert.equal(fs.readFileSync(statePath, 'utf8'), 'off');
const system = await transform(hooks);
assert.deepEqual(system, []);
});
test('unrelated commands do not touch the flag', async () => {
try { fs.unlinkSync(statePath); } catch (e) {}
const hooks = await loadPlugin({});
await hooks['command.execute.before']({ command: 'commit', arguments: 'x', sessionID: 's' });
assert.equal(fs.existsSync(statePath), false);
});
test('parseCommandFile reads frontmatter description + body, LF and CRLF', () => {
const lf = path.join(tmp, 'cmd-lf.md');
fs.writeFileSync(lf, '---\ndescription: do a thing\n---\n\nthe template body\n');
assert.deepEqual(parseCommandFile(lf), { description: 'do a thing', template: 'the template body' });
// Windows checkouts (autocrlf) deliver CRLF — the parser must still match.
const crlf = path.join(tmp, 'cmd-crlf.md');
fs.writeFileSync(crlf, '---\r\ndescription: do a thing\r\n---\r\n\r\nthe template body\r\n');
assert.deepEqual(parseCommandFile(crlf), { description: 'do a thing', template: 'the template body' });
});
test('parseCommandFile returns null when there is no frontmatter', () => {
const bare = path.join(tmp, 'cmd-bare.md');
fs.writeFileSync(bare, 'no frontmatter here\n');
assert.equal(parseCommandFile(bare), null);
});
test.after(() => fs.rmSync(tmp, { recursive: true, force: true }));