Files
jackwener__opencli/clis/twitter/delete.test.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

74 lines
3.3 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import { CommandExecutionError } from '@jackwener/opencli/errors';
import { getRegistry } from '@jackwener/opencli/registry';
import { __test__ } from './delete.js';
import './delete.js';
describe('twitter delete command', () => {
it('extracts tweet ids from both user and i/status URLs', () => {
expect(__test__.extractTweetId('https://x.com/alice/status/2040254679301718161?s=20')).toBe('2040254679301718161');
expect(__test__.extractTweetId('https://x.com/i/status/2040318731105313143')).toBe('2040318731105313143');
});
it('targets the matched tweet article instead of the first More button on the page', async () => {
const cmd = getRegistry().get('twitter/delete');
expect(cmd?.func).toBeTypeOf('function');
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn().mockResolvedValue({ ok: true, message: 'Tweet successfully deleted.' }),
};
const result = await cmd.func(page, {
url: 'https://x.com/alice/status/2040254679301718161?s=20',
});
expect(page.goto).toHaveBeenCalledWith('https://x.com/alice/status/2040254679301718161?s=20');
expect(page.wait).toHaveBeenNthCalledWith(1, { selector: '[data-testid="primaryColumn"]' });
expect(page.wait).toHaveBeenNthCalledWith(2, 2);
const script = page.evaluate.mock.calls[0][0];
expect(script).toContain("document.querySelectorAll('article')");
expect(script).toContain("'/status/' + tweetId");
expect(script).toContain("targetArticle.querySelectorAll('button,[role=\"button\"]')");
expect(result).toEqual([
{
status: 'success',
message: 'Tweet successfully deleted.',
},
]);
});
it('passes through matched-tweet lookup failures', async () => {
const cmd = getRegistry().get('twitter/delete');
expect(cmd?.func).toBeTypeOf('function');
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn().mockResolvedValue({
ok: false,
message: 'Could not find the tweet card matching the requested URL.',
}),
};
const result = await cmd.func(page, {
url: 'https://x.com/alice/status/2040254679301718161',
});
expect(result).toEqual([
{
status: 'failed',
message: 'Could not find the tweet card matching the requested URL.',
},
]);
expect(page.wait).toHaveBeenCalledTimes(1);
});
it('normalizes invalid tweet URLs into CommandExecutionError', async () => {
const cmd = getRegistry().get('twitter/delete');
expect(cmd?.func).toBeTypeOf('function');
const page = {
goto: vi.fn(),
wait: vi.fn(),
evaluate: vi.fn(),
};
await expect(cmd.func(page, {
url: 'https://x.com/alice/home',
})).rejects.toThrow(CommandExecutionError);
expect(page.goto).not.toHaveBeenCalled();
expect(page.wait).not.toHaveBeenCalled();
expect(page.evaluate).not.toHaveBeenCalled();
});
});