@jackwener/opencli 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +9 -2
  2. package/README.zh-CN.md +9 -1
  3. package/SKILL.md +24 -0
  4. package/dist/bilibili.d.ts +6 -5
  5. package/dist/browser.d.ts +3 -1
  6. package/dist/browser.js +29 -2
  7. package/dist/cascade.d.ts +3 -2
  8. package/dist/clis/bbc/news.js +42 -0
  9. package/dist/clis/boss/search.d.ts +1 -0
  10. package/dist/clis/boss/search.js +47 -0
  11. package/dist/clis/ctrip/search.d.ts +1 -0
  12. package/dist/clis/ctrip/search.js +62 -0
  13. package/dist/clis/index.d.ts +8 -0
  14. package/dist/clis/index.js +16 -0
  15. package/dist/clis/reuters/search.d.ts +1 -0
  16. package/dist/clis/reuters/search.js +52 -0
  17. package/dist/clis/smzdm/search.d.ts +1 -0
  18. package/dist/clis/smzdm/search.js +66 -0
  19. package/dist/clis/weibo/hot.d.ts +1 -0
  20. package/dist/clis/weibo/hot.js +41 -0
  21. package/dist/clis/yahoo-finance/quote.d.ts +1 -0
  22. package/dist/clis/yahoo-finance/quote.js +74 -0
  23. package/dist/clis/youtube/search.d.ts +1 -0
  24. package/dist/clis/youtube/search.js +60 -0
  25. package/dist/engine.d.ts +2 -1
  26. package/dist/explore.js +1 -1
  27. package/dist/generate.js +2 -1
  28. package/dist/main.js +6 -4
  29. package/dist/output.d.ts +1 -1
  30. package/dist/output.js +12 -8
  31. package/dist/pipeline/executor.d.ts +9 -0
  32. package/dist/pipeline/executor.js +88 -0
  33. package/dist/pipeline/index.d.ts +5 -0
  34. package/dist/pipeline/index.js +5 -0
  35. package/dist/pipeline/steps/browser.d.ts +12 -0
  36. package/dist/pipeline/steps/browser.js +68 -0
  37. package/dist/pipeline/steps/fetch.d.ts +5 -0
  38. package/dist/pipeline/steps/fetch.js +50 -0
  39. package/dist/pipeline/steps/intercept.d.ts +5 -0
  40. package/dist/pipeline/steps/intercept.js +75 -0
  41. package/dist/pipeline/steps/tap.d.ts +12 -0
  42. package/dist/pipeline/steps/tap.js +130 -0
  43. package/dist/pipeline/steps/transform.d.ts +8 -0
  44. package/dist/pipeline/steps/transform.js +53 -0
  45. package/dist/pipeline/template.d.ts +16 -0
  46. package/dist/pipeline/template.js +172 -0
  47. package/dist/pipeline/template.test.d.ts +4 -0
  48. package/dist/pipeline/template.test.js +120 -0
  49. package/dist/pipeline/transform.test.d.ts +4 -0
  50. package/dist/pipeline/transform.test.js +90 -0
  51. package/dist/pipeline.d.ts +5 -7
  52. package/dist/pipeline.js +5 -549
  53. package/dist/registry.d.ts +3 -2
  54. package/dist/runtime.d.ts +2 -1
  55. package/dist/types.d.ts +27 -0
  56. package/dist/types.js +7 -0
  57. package/package.json +6 -3
  58. package/src/bilibili.ts +9 -7
  59. package/src/browser.ts +24 -3
  60. package/src/cascade.ts +3 -2
  61. package/src/clis/bbc/news.ts +42 -0
  62. package/src/clis/boss/search.ts +47 -0
  63. package/src/clis/ctrip/search.ts +62 -0
  64. package/src/clis/index.ts +24 -0
  65. package/src/clis/reuters/search.ts +52 -0
  66. package/src/clis/smzdm/search.ts +66 -0
  67. package/src/clis/weibo/hot.ts +41 -0
  68. package/src/clis/yahoo-finance/quote.ts +74 -0
  69. package/src/clis/youtube/search.ts +60 -0
  70. package/src/engine.ts +2 -1
  71. package/src/explore.ts +1 -1
  72. package/src/generate.ts +3 -1
  73. package/src/main.ts +7 -5
  74. package/src/output.ts +10 -6
  75. package/src/pipeline/executor.ts +98 -0
  76. package/src/pipeline/index.ts +6 -0
  77. package/src/pipeline/steps/browser.ts +67 -0
  78. package/src/pipeline/steps/fetch.ts +60 -0
  79. package/src/pipeline/steps/intercept.ts +78 -0
  80. package/src/pipeline/steps/tap.ts +137 -0
  81. package/src/pipeline/steps/transform.ts +50 -0
  82. package/src/pipeline/template.test.ts +125 -0
  83. package/src/pipeline/template.ts +157 -0
  84. package/src/pipeline/transform.test.ts +107 -0
  85. package/src/pipeline.ts +5 -529
  86. package/src/registry.ts +4 -2
  87. package/src/runtime.ts +3 -1
  88. package/src/types.ts +23 -0
  89. package/vitest.config.ts +7 -0
  90. package/dist/clis/github/search.js +0 -20
  91. package/dist/clis/github/trending.yaml +0 -58
  92. package/dist/promote.d.ts +0 -1
  93. package/dist/promote.js +0 -3
  94. package/dist/register.d.ts +0 -2
  95. package/dist/register.js +0 -2
  96. package/dist/scaffold.d.ts +0 -2
  97. package/dist/scaffold.js +0 -2
  98. package/dist/smoke.d.ts +0 -2
  99. package/dist/smoke.js +0 -2
  100. package/src/clis/github/search.ts +0 -21
  101. package/src/clis/github/trending.yaml +0 -58
  102. package/src/promote.ts +0 -3
  103. package/src/register.ts +0 -2
  104. package/src/scaffold.ts +0 -2
  105. package/src/smoke.ts +0 -2
  106. /package/dist/clis/{github/search.d.ts → bbc/news.d.ts} +0 -0
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Pipeline step: tap — declarative Store Action Bridge.
3
+ *
4
+ * Generates a self-contained IIFE that:
5
+ * 1. Injects fetch + XHR dual interception proxy
6
+ * 2. Finds the Pinia/Vuex store and calls the action
7
+ * 3. Captures the response matching the URL pattern
8
+ * 4. Auto-cleans up interception in finally block
9
+ * 5. Returns the captured data (optionally sub-selected)
10
+ */
11
+ import { render } from '../template.js';
12
+ export async function stepTap(page, params, data, args) {
13
+ const cfg = typeof params === 'object' ? params : {};
14
+ const storeName = String(render(cfg.store ?? '', { args, data }));
15
+ const actionName = String(render(cfg.action ?? '', { args, data }));
16
+ const capturePattern = String(render(cfg.capture ?? '', { args, data }));
17
+ const timeout = cfg.timeout ?? 5;
18
+ const selectPath = cfg.select ? String(render(cfg.select, { args, data })) : null;
19
+ const framework = cfg.framework ?? null;
20
+ const actionArgs = cfg.args ?? [];
21
+ if (!storeName || !actionName)
22
+ throw new Error('tap: store and action are required');
23
+ // Build select chain for the captured response
24
+ const selectChain = selectPath
25
+ ? selectPath.split('.').map((p) => `?.[${JSON.stringify(p)}]`).join('')
26
+ : '';
27
+ // Serialize action arguments
28
+ const actionArgsRendered = actionArgs.map((a) => {
29
+ const rendered = render(a, { args, data });
30
+ return JSON.stringify(rendered);
31
+ });
32
+ const actionCall = actionArgsRendered.length
33
+ ? `store[${JSON.stringify(actionName)}](${actionArgsRendered.join(', ')})`
34
+ : `store[${JSON.stringify(actionName)}]()`;
35
+ const js = `
36
+ async () => {
37
+ // ── 1. Setup capture proxy (fetch + XHR dual interception) ──
38
+ let captured = null;
39
+ const capturePattern = ${JSON.stringify(capturePattern)};
40
+
41
+ // Intercept fetch API
42
+ const origFetch = window.fetch;
43
+ window.fetch = async function(...fetchArgs) {
44
+ const resp = await origFetch.apply(this, fetchArgs);
45
+ try {
46
+ const url = typeof fetchArgs[0] === 'string' ? fetchArgs[0]
47
+ : fetchArgs[0] instanceof Request ? fetchArgs[0].url : String(fetchArgs[0]);
48
+ if (capturePattern && url.includes(capturePattern) && !captured) {
49
+ try { captured = await resp.clone().json(); } catch {}
50
+ }
51
+ } catch {}
52
+ return resp;
53
+ };
54
+
55
+ // Intercept XMLHttpRequest
56
+ const origXhrOpen = XMLHttpRequest.prototype.open;
57
+ const origXhrSend = XMLHttpRequest.prototype.send;
58
+ XMLHttpRequest.prototype.open = function(method, url) {
59
+ this.__tapUrl = String(url);
60
+ return origXhrOpen.apply(this, arguments);
61
+ };
62
+ XMLHttpRequest.prototype.send = function(body) {
63
+ if (capturePattern && this.__tapUrl?.includes(capturePattern)) {
64
+ const xhr = this;
65
+ const origHandler = xhr.onreadystatechange;
66
+ xhr.onreadystatechange = function() {
67
+ if (xhr.readyState === 4 && !captured) {
68
+ try { captured = JSON.parse(xhr.responseText); } catch {}
69
+ }
70
+ if (origHandler) origHandler.apply(this, arguments);
71
+ };
72
+ const origOnload = xhr.onload;
73
+ xhr.onload = function() {
74
+ if (!captured) { try { captured = JSON.parse(xhr.responseText); } catch {} }
75
+ if (origOnload) origOnload.apply(this, arguments);
76
+ };
77
+ }
78
+ return origXhrSend.apply(this, arguments);
79
+ };
80
+
81
+ try {
82
+ // ── 2. Find store ──
83
+ let store = null;
84
+ const storeName = ${JSON.stringify(storeName)};
85
+ const fw = ${JSON.stringify(framework)};
86
+
87
+ const app = document.querySelector('#app');
88
+ if (!fw || fw === 'pinia') {
89
+ try {
90
+ const pinia = app?.__vue_app__?.config?.globalProperties?.$pinia;
91
+ if (pinia?._s) store = pinia._s.get(storeName);
92
+ } catch {}
93
+ }
94
+ if (!store && (!fw || fw === 'vuex')) {
95
+ try {
96
+ const vuexStore = app?.__vue_app__?.config?.globalProperties?.$store
97
+ ?? app?.__vue__?.$store;
98
+ if (vuexStore) {
99
+ store = { [${JSON.stringify(actionName)}]: (...a) => vuexStore.dispatch(storeName + '/' + ${JSON.stringify(actionName)}, ...a) };
100
+ }
101
+ } catch {}
102
+ }
103
+
104
+ if (!store) return { error: 'Store not found: ' + storeName, hint: 'Page may not be fully loaded or store name may be incorrect' };
105
+ if (typeof store[${JSON.stringify(actionName)}] !== 'function') {
106
+ return { error: 'Action not found: ' + ${JSON.stringify(actionName)} + ' on store ' + storeName,
107
+ hint: 'Available: ' + Object.keys(store).filter(k => typeof store[k] === 'function' && !k.startsWith('$') && !k.startsWith('_')).join(', ') };
108
+ }
109
+
110
+ // ── 3. Call store action ──
111
+ await ${actionCall};
112
+
113
+ // ── 4. Wait for network response ──
114
+ const deadline = Date.now() + ${timeout} * 1000;
115
+ while (!captured && Date.now() < deadline) {
116
+ await new Promise(r => setTimeout(r, 200));
117
+ }
118
+ } finally {
119
+ // ── 5. Always restore originals ──
120
+ window.fetch = origFetch;
121
+ XMLHttpRequest.prototype.open = origXhrOpen;
122
+ XMLHttpRequest.prototype.send = origXhrSend;
123
+ }
124
+
125
+ if (!captured) return { error: 'No matching response captured for pattern: ' + capturePattern };
126
+ return captured${selectChain} ?? captured;
127
+ }
128
+ `;
129
+ return page.evaluate(js);
130
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Pipeline steps: data transforms — select, map, filter, sort, limit.
3
+ */
4
+ export declare function stepSelect(_page: any, params: any, data: any, args: Record<string, any>): Promise<any>;
5
+ export declare function stepMap(_page: any, params: any, data: any, args: Record<string, any>): Promise<any>;
6
+ export declare function stepFilter(_page: any, params: any, data: any, args: Record<string, any>): Promise<any>;
7
+ export declare function stepSort(_page: any, params: any, data: any, _args: Record<string, any>): Promise<any>;
8
+ export declare function stepLimit(_page: any, params: any, data: any, args: Record<string, any>): Promise<any>;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Pipeline steps: data transforms — select, map, filter, sort, limit.
3
+ */
4
+ import { render, evalExpr } from '../template.js';
5
+ export async function stepSelect(_page, params, data, args) {
6
+ const pathStr = String(render(params, { args, data }));
7
+ if (data && typeof data === 'object') {
8
+ let current = data;
9
+ for (const part of pathStr.split('.')) {
10
+ if (current && typeof current === 'object' && !Array.isArray(current))
11
+ current = current[part];
12
+ else if (Array.isArray(current) && /^\d+$/.test(part))
13
+ current = current[parseInt(part, 10)];
14
+ else
15
+ return null;
16
+ }
17
+ return current;
18
+ }
19
+ return data;
20
+ }
21
+ export async function stepMap(_page, params, data, args) {
22
+ if (!data || typeof data !== 'object')
23
+ return data;
24
+ let items = Array.isArray(data) ? data : [data];
25
+ if (!Array.isArray(data) && typeof data === 'object' && 'data' in data)
26
+ items = data.data;
27
+ const result = [];
28
+ for (let i = 0; i < items.length; i++) {
29
+ const item = items[i];
30
+ const row = {};
31
+ for (const [key, template] of Object.entries(params))
32
+ row[key] = render(template, { args, data, item, index: i });
33
+ result.push(row);
34
+ }
35
+ return result;
36
+ }
37
+ export async function stepFilter(_page, params, data, args) {
38
+ if (!Array.isArray(data))
39
+ return data;
40
+ return data.filter((item, i) => evalExpr(String(params), { args, item, index: i }));
41
+ }
42
+ export async function stepSort(_page, params, data, _args) {
43
+ if (!Array.isArray(data))
44
+ return data;
45
+ const key = typeof params === 'object' ? (params.by ?? '') : String(params);
46
+ const reverse = typeof params === 'object' ? params.order === 'desc' : false;
47
+ return [...data].sort((a, b) => { const va = a[key] ?? ''; const vb = b[key] ?? ''; const cmp = va < vb ? -1 : va > vb ? 1 : 0; return reverse ? -cmp : cmp; });
48
+ }
49
+ export async function stepLimit(_page, params, data, args) {
50
+ if (!Array.isArray(data))
51
+ return data;
52
+ return data.slice(0, Number(render(params, { args, data })));
53
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Pipeline template engine: ${{ ... }} expression rendering.
3
+ */
4
+ export interface RenderContext {
5
+ args?: Record<string, any>;
6
+ data?: any;
7
+ item?: any;
8
+ index?: number;
9
+ }
10
+ export declare function render(template: any, ctx: RenderContext): any;
11
+ export declare function evalExpr(expr: string, ctx: RenderContext): any;
12
+ export declare function resolvePath(pathStr: string, ctx: RenderContext): any;
13
+ /**
14
+ * Normalize JavaScript source for browser evaluate() calls.
15
+ */
16
+ export declare function normalizeEvaluateSource(source: string): string;
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Pipeline template engine: ${{ ... }} expression rendering.
3
+ */
4
+ export function render(template, ctx) {
5
+ if (typeof template !== 'string')
6
+ return template;
7
+ // Full expression: entire string is a single ${{ ... }}
8
+ // Use [^}] to prevent matching across }} boundaries (e.g. "${{ a }}-${{ b }}")
9
+ const fullMatch = template.match(/^\$\{\{\s*([^}]*(?:\}[^}][^}]*)*)\s*\}\}$/);
10
+ if (fullMatch && !template.includes('}}-') && !template.includes('}}${{'))
11
+ return evalExpr(fullMatch[1].trim(), ctx);
12
+ // Check if the entire string is a single expression (no other text around it)
13
+ const singleExpr = template.match(/^\$\{\{\s*([\s\S]*?)\s*\}\}$/);
14
+ if (singleExpr) {
15
+ // Verify it's truly a single expression (no other ${{ inside)
16
+ const inner = singleExpr[1];
17
+ if (!inner.includes('${{'))
18
+ return evalExpr(inner.trim(), ctx);
19
+ }
20
+ return template.replace(/\$\{\{\s*(.*?)\s*\}\}/g, (_m, expr) => String(evalExpr(expr.trim(), ctx)));
21
+ }
22
+ export function evalExpr(expr, ctx) {
23
+ const args = ctx.args ?? {};
24
+ const item = ctx.item ?? {};
25
+ const data = ctx.data;
26
+ const index = ctx.index ?? 0;
27
+ // ── Pipe filters: expr | filter1(arg) | filter2 ──
28
+ // Supports: default(val), join(sep), upper, lower, truncate(n), trim, replace(old,new)
29
+ if (expr.includes('|') && !expr.includes('||')) {
30
+ const segments = expr.split('|').map(s => s.trim());
31
+ const mainExpr = segments[0];
32
+ let result = resolvePath(mainExpr, { args, item, data, index });
33
+ for (let i = 1; i < segments.length; i++) {
34
+ result = applyFilter(segments[i], result);
35
+ }
36
+ return result;
37
+ }
38
+ // Arithmetic: index + 1
39
+ const arithMatch = expr.match(/^([\w][\w.]*)\s*([+\-*/])\s*(\d+)$/);
40
+ if (arithMatch) {
41
+ const [, varName, op, numStr] = arithMatch;
42
+ const val = resolvePath(varName, { args, item, data, index });
43
+ if (val !== null && val !== undefined) {
44
+ const numVal = Number(val);
45
+ const num = Number(numStr);
46
+ if (!isNaN(numVal)) {
47
+ switch (op) {
48
+ case '+': return numVal + num;
49
+ case '-': return numVal - num;
50
+ case '*': return numVal * num;
51
+ case '/': return num !== 0 ? numVal / num : 0;
52
+ }
53
+ }
54
+ }
55
+ }
56
+ // JS-like fallback expression: item.tweetCount || 'N/A'
57
+ const orMatch = expr.match(/^(.+?)\s*\|\|\s*(.+)$/);
58
+ if (orMatch) {
59
+ const left = evalExpr(orMatch[1].trim(), ctx);
60
+ if (left)
61
+ return left;
62
+ const right = orMatch[2].trim();
63
+ return right.replace(/^['"]|['"]$/g, '');
64
+ }
65
+ return resolvePath(expr, { args, item, data, index });
66
+ }
67
+ /**
68
+ * Apply a named filter to a value.
69
+ * Supported filters:
70
+ * default(val), join(sep), upper, lower, truncate(n), trim,
71
+ * replace(old,new), keys, length, first, last
72
+ */
73
+ function applyFilter(filterExpr, value) {
74
+ const match = filterExpr.match(/^(\w+)(?:\((.+)\))?$/);
75
+ if (!match)
76
+ return value;
77
+ const [, name, rawArgs] = match;
78
+ const filterArg = rawArgs?.replace(/^['"]|['"]$/g, '') ?? '';
79
+ switch (name) {
80
+ case 'default': {
81
+ if (value === null || value === undefined || value === '') {
82
+ const intVal = parseInt(filterArg, 10);
83
+ if (!isNaN(intVal) && String(intVal) === filterArg.trim())
84
+ return intVal;
85
+ return filterArg;
86
+ }
87
+ return value;
88
+ }
89
+ case 'join':
90
+ return Array.isArray(value) ? value.join(filterArg || ', ') : value;
91
+ case 'upper':
92
+ return typeof value === 'string' ? value.toUpperCase() : value;
93
+ case 'lower':
94
+ return typeof value === 'string' ? value.toLowerCase() : value;
95
+ case 'trim':
96
+ return typeof value === 'string' ? value.trim() : value;
97
+ case 'truncate': {
98
+ const n = parseInt(filterArg, 10) || 50;
99
+ return typeof value === 'string' && value.length > n ? value.slice(0, n) + '...' : value;
100
+ }
101
+ case 'replace': {
102
+ if (typeof value !== 'string')
103
+ return value;
104
+ const parts = rawArgs?.split(',').map(s => s.trim().replace(/^['"]|['"]$/g, '')) ?? [];
105
+ return parts.length >= 2 ? value.replaceAll(parts[0], parts[1]) : value;
106
+ }
107
+ case 'keys':
108
+ return value && typeof value === 'object' ? Object.keys(value) : value;
109
+ case 'length':
110
+ return Array.isArray(value) ? value.length : typeof value === 'string' ? value.length : value;
111
+ case 'first':
112
+ return Array.isArray(value) ? value[0] : value;
113
+ case 'last':
114
+ return Array.isArray(value) ? value[value.length - 1] : value;
115
+ default:
116
+ return value;
117
+ }
118
+ }
119
+ export function resolvePath(pathStr, ctx) {
120
+ const args = ctx.args ?? {};
121
+ const item = ctx.item ?? {};
122
+ const data = ctx.data;
123
+ const index = ctx.index ?? 0;
124
+ const parts = pathStr.split('.');
125
+ const rootName = parts[0];
126
+ let obj;
127
+ let rest;
128
+ if (rootName === 'args') {
129
+ obj = args;
130
+ rest = parts.slice(1);
131
+ }
132
+ else if (rootName === 'item') {
133
+ obj = item;
134
+ rest = parts.slice(1);
135
+ }
136
+ else if (rootName === 'data') {
137
+ obj = data;
138
+ rest = parts.slice(1);
139
+ }
140
+ else if (rootName === 'index')
141
+ return index;
142
+ else {
143
+ obj = item;
144
+ rest = parts;
145
+ }
146
+ for (const part of rest) {
147
+ if (obj && typeof obj === 'object' && !Array.isArray(obj))
148
+ obj = obj[part];
149
+ else if (Array.isArray(obj) && /^\d+$/.test(part))
150
+ obj = obj[parseInt(part, 10)];
151
+ else
152
+ return null;
153
+ }
154
+ return obj;
155
+ }
156
+ /**
157
+ * Normalize JavaScript source for browser evaluate() calls.
158
+ */
159
+ export function normalizeEvaluateSource(source) {
160
+ const stripped = source.trim();
161
+ if (!stripped)
162
+ return '() => undefined';
163
+ if (stripped.startsWith('(') && stripped.endsWith(')()'))
164
+ return `() => (${stripped})`;
165
+ if (/^(async\s+)?\([^)]*\)\s*=>/.test(stripped))
166
+ return stripped;
167
+ if (/^(async\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=>/.test(stripped))
168
+ return stripped;
169
+ if (stripped.startsWith('function ') || stripped.startsWith('async function '))
170
+ return stripped;
171
+ return `() => (${stripped})`;
172
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tests for the pipeline template engine: render, evalExpr, resolvePath.
3
+ */
4
+ export {};
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Tests for the pipeline template engine: render, evalExpr, resolvePath.
3
+ */
4
+ import { describe, it, expect } from 'vitest';
5
+ import { render, evalExpr, resolvePath, normalizeEvaluateSource } from './template.js';
6
+ describe('resolvePath', () => {
7
+ it('resolves args path', () => {
8
+ expect(resolvePath('args.limit', { args: { limit: 20 } })).toBe(20);
9
+ });
10
+ it('resolves nested args path', () => {
11
+ expect(resolvePath('args.query.keyword', { args: { query: { keyword: 'test' } } })).toBe('test');
12
+ });
13
+ it('resolves item path', () => {
14
+ expect(resolvePath('item.title', { item: { title: 'Hello' } })).toBe('Hello');
15
+ });
16
+ it('resolves implicit item path (no prefix)', () => {
17
+ expect(resolvePath('title', { item: { title: 'World' } })).toBe('World');
18
+ });
19
+ it('resolves index', () => {
20
+ expect(resolvePath('index', { index: 5 })).toBe(5);
21
+ });
22
+ it('resolves data path', () => {
23
+ expect(resolvePath('data.items', { data: { items: [1, 2, 3] } })).toEqual([1, 2, 3]);
24
+ });
25
+ it('returns null for missing path', () => {
26
+ expect(resolvePath('args.missing', { args: {} })).toBeUndefined();
27
+ });
28
+ it('resolves array index', () => {
29
+ expect(resolvePath('data.0', { data: ['a', 'b'] })).toBe('a');
30
+ });
31
+ });
32
+ describe('evalExpr', () => {
33
+ it('evaluates default filter', () => {
34
+ expect(evalExpr('args.limit | default(20)', { args: {} })).toBe(20);
35
+ });
36
+ it('uses actual value over default', () => {
37
+ expect(evalExpr('args.limit | default(20)', { args: { limit: 10 } })).toBe(10);
38
+ });
39
+ it('evaluates string default', () => {
40
+ expect(evalExpr("args.name | default('unknown')", { args: {} })).toBe('unknown');
41
+ });
42
+ it('evaluates arithmetic: index + 1', () => {
43
+ expect(evalExpr('index + 1', { index: 0 })).toBe(1);
44
+ });
45
+ it('evaluates arithmetic: index * 2', () => {
46
+ expect(evalExpr('index * 2', { index: 5 })).toBe(10);
47
+ });
48
+ it('evaluates || fallback', () => {
49
+ expect(evalExpr("item.name || 'N/A'", { item: {} })).toBe('N/A');
50
+ });
51
+ it('evaluates || with truthy left', () => {
52
+ expect(evalExpr("item.name || 'N/A'", { item: { name: 'Alice' } })).toBe('Alice');
53
+ });
54
+ it('resolves simple path', () => {
55
+ expect(evalExpr('item.title', { item: { title: 'Test' } })).toBe('Test');
56
+ });
57
+ it('applies join filter', () => {
58
+ expect(evalExpr('item.tags | join(,)', { item: { tags: ['a', 'b', 'c'] } })).toBe('a,b,c');
59
+ });
60
+ it('applies upper filter', () => {
61
+ expect(evalExpr('item.name | upper', { item: { name: 'hello' } })).toBe('HELLO');
62
+ });
63
+ it('applies lower filter', () => {
64
+ expect(evalExpr('item.name | lower', { item: { name: 'HELLO' } })).toBe('hello');
65
+ });
66
+ it('applies truncate filter', () => {
67
+ expect(evalExpr('item.text | truncate(5)', { item: { text: 'Hello World!' } })).toBe('Hello...');
68
+ });
69
+ it('chains filters', () => {
70
+ expect(evalExpr('item.name | upper | truncate(3)', { item: { name: 'hello' } })).toBe('HEL...');
71
+ });
72
+ it('applies length filter', () => {
73
+ expect(evalExpr('item.items | length', { item: { items: [1, 2, 3] } })).toBe(3);
74
+ });
75
+ });
76
+ describe('render', () => {
77
+ it('renders full expression', () => {
78
+ expect(render('${{ args.limit }}', { args: { limit: 30 } })).toBe(30);
79
+ });
80
+ it('renders inline expression in string', () => {
81
+ expect(render('Hello ${{ item.name }}!', { item: { name: 'World' } })).toBe('Hello World!');
82
+ });
83
+ it('renders multiple inline expressions', () => {
84
+ expect(render('${{ item.first }}-${{ item.second }}', { item: { first: 'X', second: 'Y' } })).toBe('X-Y');
85
+ });
86
+ it('returns non-string values as-is', () => {
87
+ expect(render(42, {})).toBe(42);
88
+ expect(render(null, {})).toBeNull();
89
+ expect(render(undefined, {})).toBeUndefined();
90
+ });
91
+ it('returns full expression result as native type', () => {
92
+ expect(render('${{ args.list }}', { args: { list: [1, 2, 3] } })).toEqual([1, 2, 3]);
93
+ });
94
+ it('renders URL template', () => {
95
+ expect(render('https://api.example.com/search?q=${{ args.keyword }}', { args: { keyword: 'test' } })).toBe('https://api.example.com/search?q=test');
96
+ });
97
+ });
98
+ describe('normalizeEvaluateSource', () => {
99
+ it('wraps bare expression', () => {
100
+ expect(normalizeEvaluateSource('document.title')).toBe('() => (document.title)');
101
+ });
102
+ it('passes through arrow function', () => {
103
+ expect(normalizeEvaluateSource('() => 42')).toBe('() => 42');
104
+ });
105
+ it('passes through async arrow function', () => {
106
+ const src = 'async () => { return 1; }';
107
+ expect(normalizeEvaluateSource(src)).toBe(src);
108
+ });
109
+ it('passes through named function', () => {
110
+ const src = 'function foo() { return 1; }';
111
+ expect(normalizeEvaluateSource(src)).toBe(src);
112
+ });
113
+ it('wraps IIFE pattern', () => {
114
+ const src = '(async () => { return 1; })()';
115
+ expect(normalizeEvaluateSource(src)).toBe(`() => (${src})`);
116
+ });
117
+ it('handles empty string', () => {
118
+ expect(normalizeEvaluateSource('')).toBe('() => undefined');
119
+ });
120
+ });
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tests for pipeline transform steps: select, map, filter, sort, limit.
3
+ */
4
+ export {};
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Tests for pipeline transform steps: select, map, filter, sort, limit.
3
+ */
4
+ import { describe, it, expect } from 'vitest';
5
+ import { stepSelect, stepMap, stepFilter, stepSort, stepLimit } from './steps/transform.js';
6
+ const SAMPLE_DATA = [
7
+ { title: 'Alpha', score: 10, author: 'Alice' },
8
+ { title: 'Beta', score: 30, author: 'Bob' },
9
+ { title: 'Gamma', score: 20, author: 'Charlie' },
10
+ ];
11
+ describe('stepSelect', () => {
12
+ it('selects nested path', async () => {
13
+ const data = { result: { items: [1, 2, 3] } };
14
+ const result = await stepSelect(null, 'result.items', data, {});
15
+ expect(result).toEqual([1, 2, 3]);
16
+ });
17
+ it('selects array by index', async () => {
18
+ const data = { list: ['a', 'b', 'c'] };
19
+ const result = await stepSelect(null, 'list.1', data, {});
20
+ expect(result).toBe('b');
21
+ });
22
+ it('returns null for missing path', async () => {
23
+ const result = await stepSelect(null, 'missing.path', { foo: 1 }, {});
24
+ expect(result).toBeNull();
25
+ });
26
+ it('returns data as-is for non-object', async () => {
27
+ const result = await stepSelect(null, 'foo', 'string-data', {});
28
+ expect(result).toBe('string-data');
29
+ });
30
+ });
31
+ describe('stepMap', () => {
32
+ it('maps array items', async () => {
33
+ const result = await stepMap(null, {
34
+ name: '${{ item.title }}',
35
+ rank: '${{ index + 1 }}',
36
+ }, SAMPLE_DATA, {});
37
+ expect(result).toEqual([
38
+ { name: 'Alpha', rank: 1 },
39
+ { name: 'Beta', rank: 2 },
40
+ { name: 'Gamma', rank: 3 },
41
+ ]);
42
+ });
43
+ it('handles single object', async () => {
44
+ const result = await stepMap(null, {
45
+ name: '${{ item.title }}',
46
+ }, { title: 'Solo' }, {});
47
+ expect(result).toEqual([{ name: 'Solo' }]);
48
+ });
49
+ it('returns null/undefined as-is', async () => {
50
+ expect(await stepMap(null, { x: '${{ item.x }}' }, null, {})).toBeNull();
51
+ });
52
+ });
53
+ describe('stepFilter', () => {
54
+ it('filters by expression', async () => {
55
+ const result = await stepFilter(null, 'item.score', SAMPLE_DATA, {});
56
+ expect(result).toHaveLength(3); // all truthy
57
+ });
58
+ it('returns non-array as-is', async () => {
59
+ const result = await stepFilter(null, 'item.x', 'not-array', {});
60
+ expect(result).toBe('not-array');
61
+ });
62
+ });
63
+ describe('stepSort', () => {
64
+ it('sorts ascending by key', async () => {
65
+ const result = await stepSort(null, 'score', SAMPLE_DATA, {});
66
+ expect(result.map((r) => r.title)).toEqual(['Alpha', 'Gamma', 'Beta']);
67
+ });
68
+ it('sorts descending', async () => {
69
+ const result = await stepSort(null, { by: 'score', order: 'desc' }, SAMPLE_DATA, {});
70
+ expect(result.map((r) => r.title)).toEqual(['Beta', 'Gamma', 'Alpha']);
71
+ });
72
+ it('does not mutate original', async () => {
73
+ const original = [...SAMPLE_DATA];
74
+ await stepSort(null, 'score', SAMPLE_DATA, {});
75
+ expect(SAMPLE_DATA).toEqual(original);
76
+ });
77
+ });
78
+ describe('stepLimit', () => {
79
+ it('limits array to N items', async () => {
80
+ const result = await stepLimit(null, '2', SAMPLE_DATA, {});
81
+ expect(result).toHaveLength(2);
82
+ });
83
+ it('limits using template expression', async () => {
84
+ const result = await stepLimit(null, '${{ args.limit }}', SAMPLE_DATA, { limit: 1 });
85
+ expect(result).toHaveLength(1);
86
+ });
87
+ it('returns non-array as-is', async () => {
88
+ expect(await stepLimit(null, '5', 'string', {})).toBe('string');
89
+ });
90
+ });
@@ -1,9 +1,7 @@
1
1
  /**
2
- * YAML pipeline executor.
3
- * Steps: fetch, navigate, evaluate, map, filter, sort, limit, select, snapshot, click, type, wait, press, intercept.
2
+ * YAML pipeline executor — re-exports from modular pipeline system.
3
+ *
4
+ * This file exists for backward compatibility. All logic has been
5
+ * refactored into src/pipeline/ with modular step handlers.
4
6
  */
5
- export interface PipelineContext {
6
- args?: Record<string, any>;
7
- debug?: boolean;
8
- }
9
- export declare function executePipeline(page: any, pipeline: any[], ctx?: PipelineContext): Promise<any>;
7
+ export { executePipeline, type PipelineContext } from './pipeline/index.js';