twd-js 0.5.2 → 0.7.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.
package/dist/mock-sw.js CHANGED
@@ -1 +1 @@
1
- function u(e,s,o){return o.find(t=>{const a=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex){const n=new RegExp(t.url);return a&&n.test(s)}const l=t.url===s||s.includes(t.url);return a&&l})}function d(e,s,o){e.forEach(t=>t.postMessage({type:"EXECUTED",alias:s.alias,request:o}))}let c=[];const f=async e=>{const{method:s}=e.request,o=e.request.url,t=u(s,o,c);t&&(console.log("[TWD] Mock hit:",t.alias,s,o),e.respondWith((async()=>{let a=null;const l=e.request.headers.get("content-type")||"application/json";if(l.includes("application/json"))try{a=await e.request.clone().json()}catch{}else if(l.includes("form"))try{const n=await e.request.clone().formData();a={},n.forEach((r,i)=>{a[i]=r})}catch{}else if(l.includes("text"))try{a=await e.request.clone().text()}catch{}else if(l.includes("octet-stream"))try{a=await e.request.clone().arrayBuffer()}catch{}else if(l.includes("image"))try{a=await e.request.clone().blob()}catch{}else try{a=await e.request.clone().text()}catch{}return self.clients.matchAll().then(n=>{d(n,t,a)}),new Response(JSON.stringify(t.response),{status:t.status||200,headers:t.responseHeaders||{"Content-Type":"application/json"}})})()))},h=e=>{const{type:s,rule:o}=e.data||{};s==="ADD_RULE"&&(c=c.filter(t=>t.alias!==o.alias),c.push(o),console.log("[TWD] Rule added:",o)),s==="CLEAR_RULES"&&(c=[],console.log("[TWD] All rules cleared"))};console.log("[TWD] Mock Service Worker loaded - version 0.5.2");self.addEventListener("fetch",f);self.addEventListener("message",h);
1
+ const u=e=>{try{return new RegExp(e),!0}catch{return!1}};function d(e,s,n){return n.find(t=>{const o=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex&&u(t.url)){const a=new RegExp(t.url);return o&&a.test(s)}const l=t.url===s||s.includes(t.url);return o&&l})}function f(e,s,n){e.forEach(t=>t.postMessage({type:"EXECUTED",alias:s.alias,request:n}))}const h=(e,s,n)=>{const o=![204,205,304].includes(s),l=o?JSON.stringify(e):null;return new Response(l,{status:s,headers:o?n||{"Content-Type":"application/json"}:n||{}})};let c=[];const y=async e=>{const{method:s}=e.request,n=e.request.url,t=d(s,n,c);t&&(console.log("[TWD] Mock hit:",t.alias,s,n),e.respondWith((async()=>{let o=null;const l=e.request.headers.get("content-type")||"application/json";if(l.includes("application/json"))try{o=await e.request.clone().json()}catch{}else if(l.includes("form"))try{const a=await e.request.clone().formData();o={},a.forEach((r,i)=>{o[i]=r})}catch{}else if(l.includes("text"))try{o=await e.request.clone().text()}catch{}else if(l.includes("octet-stream"))try{o=await e.request.clone().arrayBuffer()}catch{}else if(l.includes("image"))try{o=await e.request.clone().blob()}catch{}else try{o=await e.request.clone().text()}catch{}return self.clients.matchAll().then(a=>{f(a,t,o)}),h(t.response,t.status??200,t.responseHeaders)})()))},p=e=>{const{type:s,rule:n}=e.data||{};s==="ADD_RULE"&&(c=c.filter(t=>t.alias!==n.alias),c.push(n),console.log("[TWD] Rule added:",n)),s==="CLEAR_RULES"&&(c=[],console.log("[TWD] All rules cleared"))};console.log("[TWD] Mock Service Worker loaded - version 0.7.0");self.addEventListener("fetch",y);self.addEventListener("message",p);
@@ -0,0 +1,18 @@
1
+ import { Plugin } from 'vite';
2
+ /**
3
+ * Vite plugin to remove the mock service worker file from the build output.
4
+ * This is useful for production builds where you don't want the mock service worker to be included.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { removeMockServiceWorker } from 'twd-js';
9
+ *
10
+ * export default defineConfig({
11
+ * plugins: [
12
+ * // ... other plugins
13
+ * removeMockServiceWorker()
14
+ * ]
15
+ * });
16
+ * ```
17
+ */
18
+ export declare function removeMockServiceWorker(): Plugin;
@@ -0,0 +1,5 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=10,C=(r=0)=>e=>`\x1B[${e+r}m`,j=(r=0)=>e=>`\x1B[${38+r};5;${e}m`,M=(r=0)=>(e,o,t)=>`\x1B[${38+r};2;${e};${o};${t}m`,l={modifier:{reset:[0,0],bold:[1,22],dim:[2,22],italic:[3,23],underline:[4,24],overline:[53,55],inverse:[7,27],hidden:[8,28],strikethrough:[9,29]},color:{black:[30,39],red:[31,39],green:[32,39],yellow:[33,39],blue:[34,39],magenta:[35,39],cyan:[36,39],white:[37,39],blackBright:[90,39],gray:[90,39],grey:[90,39],redBright:[91,39],greenBright:[92,39],yellowBright:[93,39],blueBright:[94,39],magentaBright:[95,39],cyanBright:[96,39],whiteBright:[97,39]},bgColor:{bgBlack:[40,49],bgRed:[41,49],bgGreen:[42,49],bgYellow:[43,49],bgBlue:[44,49],bgMagenta:[45,49],bgCyan:[46,49],bgWhite:[47,49],bgBlackBright:[100,49],bgGray:[100,49],bgGrey:[100,49],bgRedBright:[101,49],bgGreenBright:[102,49],bgYellowBright:[103,49],bgBlueBright:[104,49],bgMagentaBright:[105,49],bgCyanBright:[106,49],bgWhiteBright:[107,49]}};Object.keys(l.modifier);const F=Object.keys(l.color),E=Object.keys(l.bgColor);[...F,...E];function N(){const r=new Map;for(const[e,o]of Object.entries(l)){for(const[t,n]of Object.entries(o))l[t]={open:`\x1B[${n[0]}m`,close:`\x1B[${n[1]}m`},o[t]=l[t],r.set(n[0],n[1]);Object.defineProperty(l,e,{value:o,enumerable:!1})}return Object.defineProperty(l,"codes",{value:r,enumerable:!1}),l.color.close="\x1B[39m",l.bgColor.close="\x1B[49m",l.color.ansi=C(),l.color.ansi256=j(),l.color.ansi16m=M(),l.bgColor.ansi=C(v),l.bgColor.ansi256=j(v),l.bgColor.ansi16m=M(v),Object.defineProperties(l,{rgbToAnsi256:{value(e,o,t){return e===o&&o===t?e<8?16:e>248?231:Math.round((e-8)/247*24)+232:16+36*Math.round(e/255*5)+6*Math.round(o/255*5)+Math.round(t/255*5)},enumerable:!1},hexToRgb:{value(e){const o=/[a-f\d]{6}|[a-f\d]{3}/i.exec(e.toString(16));if(!o)return[0,0,0];let[t]=o;t.length===3&&(t=[...t].map(s=>s+s).join(""));const n=Number.parseInt(t,16);return[n>>16&255,n>>8&255,n&255]},enumerable:!1},hexToAnsi256:{value:e=>l.rgbToAnsi256(...l.hexToRgb(e)),enumerable:!1},ansi256ToAnsi:{value(e){if(e<8)return 30+e;if(e<16)return 90+(e-8);let o,t,n;if(e>=232)o=((e-232)*10+8)/255,t=o,n=o;else{e-=16;const c=e%36;o=Math.floor(e/36)/5,t=Math.floor(c/6)/5,n=c%6/5}const s=Math.max(o,t,n)*2;if(s===0)return 30;let i=30+(Math.round(n)<<2|Math.round(t)<<1|Math.round(o));return s===2&&(i+=60),i},enumerable:!1},rgbToAnsi:{value:(e,o,t)=>l.ansi256ToAnsi(l.rgbToAnsi256(e,o,t)),enumerable:!1},hexToAnsi:{value:e=>l.ansi256ToAnsi(l.hexToAnsi256(e)),enumerable:!1}}),l}const a=N(),S=(()=>{if(!("navigator"in globalThis))return 0;if(globalThis.navigator.userAgentData){const r=navigator.userAgentData.brands.find(({brand:e})=>e==="Chromium");if(r&&r.version>93)return 3}return/\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent)?1:0})(),w=S!==0&&{level:S},I={stdout:w,stderr:w};function G(r,e,o){let t=r.indexOf(e);if(t===-1)return r;const n=e.length;let s=0,i="";do i+=r.slice(s,t)+e+o,s=t+n,t=r.indexOf(e,s);while(t!==-1);return i+=r.slice(s),i}function Y(r,e,o,t){let n=0,s="";do{const i=r[t-1]==="\r";s+=r.slice(n,i?t-1:t)+e+(i?`\r
2
+ `:`
3
+ `)+o,n=t+1,t=r.indexOf(`
4
+ `,n)}while(t!==-1);return s+=r.slice(n),s}const{stdout:x,stderr:R}=I,A=Symbol("GENERATOR"),u=Symbol("STYLER"),f=Symbol("IS_EMPTY"),$=["ansi","ansi","ansi256","ansi16m"],b=Object.create(null),L=(r,e={})=>{if(e.level&&!(Number.isInteger(e.level)&&e.level>=0&&e.level<=3))throw new Error("The `level` option should be an integer from 0 to 3");const o=x?x.level:0;r.level=e.level===void 0?o:e.level},D=r=>{const e=(...o)=>o.join(" ");return L(e,r),Object.setPrototypeOf(e,h.prototype),e};function h(r){return D(r)}Object.setPrototypeOf(h.prototype,Function.prototype);for(const[r,e]of Object.entries(a))b[r]={get(){const o=p(this,O(e.open,e.close,this[u]),this[f]);return Object.defineProperty(this,r,{value:o}),o}};b.visible={get(){const r=p(this,this[u],!0);return Object.defineProperty(this,"visible",{value:r}),r}};const B=(r,e,o,...t)=>r==="rgb"?e==="ansi16m"?a[o].ansi16m(...t):e==="ansi256"?a[o].ansi256(a.rgbToAnsi256(...t)):a[o].ansi(a.rgbToAnsi(...t)):r==="hex"?B("rgb",e,o,...a.hexToRgb(...t)):a[o][r](...t),W=["rgb","hex","ansi256"];for(const r of W){b[r]={get(){const{level:o}=this;return function(...t){const n=O(B(r,$[o],"color",...t),a.color.close,this[u]);return p(this,n,this[f])}}};const e="bg"+r[0].toUpperCase()+r.slice(1);b[e]={get(){const{level:o}=this;return function(...t){const n=O(B(r,$[o],"bgColor",...t),a.bgColor.close,this[u]);return p(this,n,this[f])}}}}const U=Object.defineProperties(()=>{},{...b,level:{enumerable:!0,get(){return this[A].level},set(r){this[A].level=r}}}),O=(r,e,o)=>{let t,n;return o===void 0?(t=r,n=e):(t=o.openAll+r,n=e+o.closeAll),{open:r,close:e,openAll:t,closeAll:n,parent:o}},p=(r,e,o)=>{const t=(...n)=>V(t,n.length===1?""+n[0]:n.join(" "));return Object.setPrototypeOf(t,U),t[A]=r,t[u]=e,t[f]=o,t},V=(r,e)=>{if(r.level<=0||!e)return r[f]?"":e;let o=r[u];if(o===void 0)return e;const{openAll:t,closeAll:n}=o;if(e.includes("\x1B"))for(;o!==void 0;)e=G(e,o.close,o.open),o=o.parent;const s=e.indexOf(`
5
+ `);return s!==-1&&(e=Y(e,n,t,s)),t+e+n};Object.defineProperties(h.prototype,b);const d=h();h({level:R?R.level:0});const H=(r,e)=>{const o=[...r].filter(n=>!n.parent),t=(n,s=0)=>{const i=" ".repeat(s),c=e.find(m=>m.id===n.id);let g="",y="";n.type!=="suite"&&(c?.status==="pass"?g=d.green("✓"):c?.status==="fail"?(g=d.red("✗"),y=` - Error: ${c.error}`):g=d.yellow("○"));const k=n.type==="suite"?`${i}${n.name}`:`${i}${g} ${n.name}`;if(console.log(k),y&&console.log(d.red(`${i}${y}`)),n.children)for(const m of n.children){const T=r.find(P=>P.id===m);T&&t(T,s+1)}};for(const n of o)t(n)},K=async()=>{const r=window.__testRunner,e=[],t=await new r({onStart:()=>{},onPass:n=>{e.push({id:n.id,status:"pass"})},onFail:(n,s)=>{e.push({id:n.id,status:"fail",error:s.message})},onSkip:n=>{e.push({id:n.id,status:"skip"})}}).runAll();return{handlers:Array.from(t.values()),testStatus:e}};exports.executeTests=K;exports.reportResults=H;
@@ -0,0 +1,12 @@
1
+ import { Handler } from './runner';
2
+ interface TestResult {
3
+ id: string;
4
+ status: 'pass' | 'fail' | 'skip';
5
+ error?: string;
6
+ }
7
+ export declare const reportResults: (handlers: Handler[], testResults: TestResult[]) => void;
8
+ export declare const executeTests: () => Promise<{
9
+ handlers: Handler[];
10
+ testStatus: TestResult[];
11
+ }>;
12
+ export {};
@@ -0,0 +1,304 @@
1
+ const T = (r = 0) => (e) => `\x1B[${e + r}m`, C = (r = 0) => (e) => `\x1B[${38 + r};5;${e}m`, S = (r = 0) => (e, o, t) => `\x1B[${38 + r};2;${e};${o};${t}m`, l = {
2
+ modifier: {
3
+ reset: [0, 0],
4
+ // 21 isn't widely supported and 22 does the same thing
5
+ bold: [1, 22],
6
+ dim: [2, 22],
7
+ italic: [3, 23],
8
+ underline: [4, 24],
9
+ overline: [53, 55],
10
+ inverse: [7, 27],
11
+ hidden: [8, 28],
12
+ strikethrough: [9, 29]
13
+ },
14
+ color: {
15
+ black: [30, 39],
16
+ red: [31, 39],
17
+ green: [32, 39],
18
+ yellow: [33, 39],
19
+ blue: [34, 39],
20
+ magenta: [35, 39],
21
+ cyan: [36, 39],
22
+ white: [37, 39],
23
+ // Bright color
24
+ blackBright: [90, 39],
25
+ gray: [90, 39],
26
+ // Alias of `blackBright`
27
+ grey: [90, 39],
28
+ // Alias of `blackBright`
29
+ redBright: [91, 39],
30
+ greenBright: [92, 39],
31
+ yellowBright: [93, 39],
32
+ blueBright: [94, 39],
33
+ magentaBright: [95, 39],
34
+ cyanBright: [96, 39],
35
+ whiteBright: [97, 39]
36
+ },
37
+ bgColor: {
38
+ bgBlack: [40, 49],
39
+ bgRed: [41, 49],
40
+ bgGreen: [42, 49],
41
+ bgYellow: [43, 49],
42
+ bgBlue: [44, 49],
43
+ bgMagenta: [45, 49],
44
+ bgCyan: [46, 49],
45
+ bgWhite: [47, 49],
46
+ // Bright color
47
+ bgBlackBright: [100, 49],
48
+ bgGray: [100, 49],
49
+ // Alias of `bgBlackBright`
50
+ bgGrey: [100, 49],
51
+ // Alias of `bgBlackBright`
52
+ bgRedBright: [101, 49],
53
+ bgGreenBright: [102, 49],
54
+ bgYellowBright: [103, 49],
55
+ bgBlueBright: [104, 49],
56
+ bgMagentaBright: [105, 49],
57
+ bgCyanBright: [106, 49],
58
+ bgWhiteBright: [107, 49]
59
+ }
60
+ };
61
+ Object.keys(l.modifier);
62
+ const N = Object.keys(l.color), k = Object.keys(l.bgColor);
63
+ [...N, ...k];
64
+ function E() {
65
+ const r = /* @__PURE__ */ new Map();
66
+ for (const [e, o] of Object.entries(l)) {
67
+ for (const [t, n] of Object.entries(o))
68
+ l[t] = {
69
+ open: `\x1B[${n[0]}m`,
70
+ close: `\x1B[${n[1]}m`
71
+ }, o[t] = l[t], r.set(n[0], n[1]);
72
+ Object.defineProperty(l, e, {
73
+ value: o,
74
+ enumerable: !1
75
+ });
76
+ }
77
+ return Object.defineProperty(l, "codes", {
78
+ value: r,
79
+ enumerable: !1
80
+ }), l.color.close = "\x1B[39m", l.bgColor.close = "\x1B[49m", l.color.ansi = T(), l.color.ansi256 = C(), l.color.ansi16m = S(), l.bgColor.ansi = T(10), l.bgColor.ansi256 = C(10), l.bgColor.ansi16m = S(10), Object.defineProperties(l, {
81
+ rgbToAnsi256: {
82
+ value(e, o, t) {
83
+ return e === o && o === t ? e < 8 ? 16 : e > 248 ? 231 : Math.round((e - 8) / 247 * 24) + 232 : 16 + 36 * Math.round(e / 255 * 5) + 6 * Math.round(o / 255 * 5) + Math.round(t / 255 * 5);
84
+ },
85
+ enumerable: !1
86
+ },
87
+ hexToRgb: {
88
+ value(e) {
89
+ const o = /[a-f\d]{6}|[a-f\d]{3}/i.exec(e.toString(16));
90
+ if (!o)
91
+ return [0, 0, 0];
92
+ let [t] = o;
93
+ t.length === 3 && (t = [...t].map((s) => s + s).join(""));
94
+ const n = Number.parseInt(t, 16);
95
+ return [
96
+ /* eslint-disable no-bitwise */
97
+ n >> 16 & 255,
98
+ n >> 8 & 255,
99
+ n & 255
100
+ /* eslint-enable no-bitwise */
101
+ ];
102
+ },
103
+ enumerable: !1
104
+ },
105
+ hexToAnsi256: {
106
+ value: (e) => l.rgbToAnsi256(...l.hexToRgb(e)),
107
+ enumerable: !1
108
+ },
109
+ ansi256ToAnsi: {
110
+ value(e) {
111
+ if (e < 8)
112
+ return 30 + e;
113
+ if (e < 16)
114
+ return 90 + (e - 8);
115
+ let o, t, n;
116
+ if (e >= 232)
117
+ o = ((e - 232) * 10 + 8) / 255, t = o, n = o;
118
+ else {
119
+ e -= 16;
120
+ const c = e % 36;
121
+ o = Math.floor(e / 36) / 5, t = Math.floor(c / 6) / 5, n = c % 6 / 5;
122
+ }
123
+ const s = Math.max(o, t, n) * 2;
124
+ if (s === 0)
125
+ return 30;
126
+ let i = 30 + (Math.round(n) << 2 | Math.round(t) << 1 | Math.round(o));
127
+ return s === 2 && (i += 60), i;
128
+ },
129
+ enumerable: !1
130
+ },
131
+ rgbToAnsi: {
132
+ value: (e, o, t) => l.ansi256ToAnsi(l.rgbToAnsi256(e, o, t)),
133
+ enumerable: !1
134
+ },
135
+ hexToAnsi: {
136
+ value: (e) => l.ansi256ToAnsi(l.hexToAnsi256(e)),
137
+ enumerable: !1
138
+ }
139
+ }), l;
140
+ }
141
+ const a = E(), R = (() => {
142
+ if (!("navigator" in globalThis))
143
+ return 0;
144
+ if (globalThis.navigator.userAgentData) {
145
+ const r = navigator.userAgentData.brands.find(({ brand: e }) => e === "Chromium");
146
+ if (r && r.version > 93)
147
+ return 3;
148
+ }
149
+ return /\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent) ? 1 : 0;
150
+ })(), j = R !== 0 && {
151
+ level: R
152
+ }, I = {
153
+ stdout: j,
154
+ stderr: j
155
+ };
156
+ function P(r, e, o) {
157
+ let t = r.indexOf(e);
158
+ if (t === -1)
159
+ return r;
160
+ const n = e.length;
161
+ let s = 0, i = "";
162
+ do
163
+ i += r.slice(s, t) + e + o, s = t + n, t = r.indexOf(e, s);
164
+ while (t !== -1);
165
+ return i += r.slice(s), i;
166
+ }
167
+ function G(r, e, o, t) {
168
+ let n = 0, s = "";
169
+ do {
170
+ const i = r[t - 1] === "\r";
171
+ s += r.slice(n, i ? t - 1 : t) + e + (i ? `\r
172
+ ` : `
173
+ `) + o, n = t + 1, t = r.indexOf(`
174
+ `, n);
175
+ } while (t !== -1);
176
+ return s += r.slice(n), s;
177
+ }
178
+ const { stdout: w, stderr: x } = I, v = Symbol("GENERATOR"), u = Symbol("STYLER"), b = Symbol("IS_EMPTY"), M = [
179
+ "ansi",
180
+ "ansi",
181
+ "ansi256",
182
+ "ansi16m"
183
+ ], f = /* @__PURE__ */ Object.create(null), D = (r, e = {}) => {
184
+ if (e.level && !(Number.isInteger(e.level) && e.level >= 0 && e.level <= 3))
185
+ throw new Error("The `level` option should be an integer from 0 to 3");
186
+ const o = w ? w.level : 0;
187
+ r.level = e.level === void 0 ? o : e.level;
188
+ }, Y = (r) => {
189
+ const e = (...o) => o.join(" ");
190
+ return D(e, r), Object.setPrototypeOf(e, h.prototype), e;
191
+ };
192
+ function h(r) {
193
+ return Y(r);
194
+ }
195
+ Object.setPrototypeOf(h.prototype, Function.prototype);
196
+ for (const [r, e] of Object.entries(a))
197
+ f[r] = {
198
+ get() {
199
+ const o = p(this, O(e.open, e.close, this[u]), this[b]);
200
+ return Object.defineProperty(this, r, { value: o }), o;
201
+ }
202
+ };
203
+ f.visible = {
204
+ get() {
205
+ const r = p(this, this[u], !0);
206
+ return Object.defineProperty(this, "visible", { value: r }), r;
207
+ }
208
+ };
209
+ const A = (r, e, o, ...t) => r === "rgb" ? e === "ansi16m" ? a[o].ansi16m(...t) : e === "ansi256" ? a[o].ansi256(a.rgbToAnsi256(...t)) : a[o].ansi(a.rgbToAnsi(...t)) : r === "hex" ? A("rgb", e, o, ...a.hexToRgb(...t)) : a[o][r](...t), _ = ["rgb", "hex", "ansi256"];
210
+ for (const r of _) {
211
+ f[r] = {
212
+ get() {
213
+ const { level: o } = this;
214
+ return function(...t) {
215
+ const n = O(A(r, M[o], "color", ...t), a.color.close, this[u]);
216
+ return p(this, n, this[b]);
217
+ };
218
+ }
219
+ };
220
+ const e = "bg" + r[0].toUpperCase() + r.slice(1);
221
+ f[e] = {
222
+ get() {
223
+ const { level: o } = this;
224
+ return function(...t) {
225
+ const n = O(A(r, M[o], "bgColor", ...t), a.bgColor.close, this[u]);
226
+ return p(this, n, this[b]);
227
+ };
228
+ }
229
+ };
230
+ }
231
+ const L = Object.defineProperties(() => {
232
+ }, {
233
+ ...f,
234
+ level: {
235
+ enumerable: !0,
236
+ get() {
237
+ return this[v].level;
238
+ },
239
+ set(r) {
240
+ this[v].level = r;
241
+ }
242
+ }
243
+ }), O = (r, e, o) => {
244
+ let t, n;
245
+ return o === void 0 ? (t = r, n = e) : (t = o.openAll + r, n = e + o.closeAll), {
246
+ open: r,
247
+ close: e,
248
+ openAll: t,
249
+ closeAll: n,
250
+ parent: o
251
+ };
252
+ }, p = (r, e, o) => {
253
+ const t = (...n) => U(t, n.length === 1 ? "" + n[0] : n.join(" "));
254
+ return Object.setPrototypeOf(t, L), t[v] = r, t[u] = e, t[b] = o, t;
255
+ }, U = (r, e) => {
256
+ if (r.level <= 0 || !e)
257
+ return r[b] ? "" : e;
258
+ let o = r[u];
259
+ if (o === void 0)
260
+ return e;
261
+ const { openAll: t, closeAll: n } = o;
262
+ if (e.includes("\x1B"))
263
+ for (; o !== void 0; )
264
+ e = P(e, o.close, o.open), o = o.parent;
265
+ const s = e.indexOf(`
266
+ `);
267
+ return s !== -1 && (e = G(e, n, t, s)), t + e + n;
268
+ };
269
+ Object.defineProperties(h.prototype, f);
270
+ const d = h();
271
+ h({ level: x ? x.level : 0 });
272
+ const K = (r, e) => {
273
+ const o = [...r].filter((n) => !n.parent), t = (n, s = 0) => {
274
+ const i = " ".repeat(s), c = e.find((y) => y.id === n.id);
275
+ let g = "", m = "";
276
+ n.type !== "suite" && (c?.status === "pass" ? g = d.green("✓") : c?.status === "fail" ? (g = d.red("✗"), m = ` - Error: ${c.error}`) : g = d.yellow("○"));
277
+ const F = n.type === "suite" ? `${i}${n.name}` : `${i}${g} ${n.name}`;
278
+ if (console.log(F), m && console.log(d.red(`${i}${m}`)), n.children)
279
+ for (const y of n.children) {
280
+ const B = r.find(($) => $.id === y);
281
+ B && t(B, s + 1);
282
+ }
283
+ };
284
+ for (const n of o) t(n);
285
+ }, W = async () => {
286
+ const r = window.__testRunner, e = [], t = await new r({
287
+ onStart: () => {
288
+ },
289
+ onPass: (n) => {
290
+ e.push({ id: n.id, status: "pass" });
291
+ },
292
+ onFail: (n, s) => {
293
+ e.push({ id: n.id, status: "fail", error: s.message });
294
+ },
295
+ onSkip: (n) => {
296
+ e.push({ id: n.id, status: "skip" });
297
+ }
298
+ }).runAll();
299
+ return { handlers: Array.from(t.values()), testStatus: e };
300
+ };
301
+ export {
302
+ W as executeTests,
303
+ K as reportResults
304
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=new Map,i=new Map,c=new Map,a=[],h=()=>Math.random().toString(36).substr(2,9),f=(o,t)=>{const s=h(),e=a.at(-1);r.set(s,{id:s,name:o,type:"suite",children:[],logs:[],depth:a.length,parent:e,handler:t}),e&&r.get(e).children.push(s),a.push(s),t(),a.pop()},l=(o,t)=>{const s=h(),e=a.at(-1),n={id:s,name:o,type:"test",depth:a.length,handler:t,logs:[],parent:e};e&&r.get(e).children.push(s),r.set(s,n)};l.only=(o,t)=>{const s=h(),e=a.at(-1),n={id:s,name:o,type:"test",depth:a.length,handler:t,logs:[],parent:e,only:!0};e&&r.get(e).children.push(s),r.set(s,n)};l.skip=(o,t)=>{const s=h(),e=a.at(-1),n={id:s,name:o,type:"test",depth:a.length,handler:t||(()=>{}),logs:[],parent:e,skip:!0};e&&r.get(e).children.push(s),r.set(s,n)};const p=o=>{const t=a.at(-1);if(!t)throw new Error("beforeEach() must be inside a describe()");i.has(t)||i.set(t,[]),i.get(t).push(o)},d=o=>{const t=a.at(-1);if(!t)throw new Error("afterEach() must be inside a describe()");c.has(t)||c.set(t,[]),c.get(t).push(o)},g=o=>{const t=[],s=[];let e=o;for(;e;)i.has(e)&&t.unshift(...i.get(e)),c.has(e)&&s.push(...c.get(e)),e=r.get(e)?.parent;return{before:t,after:s}},y=()=>{r.clear(),i.clear(),c.clear()};class u{events;constructor(t){this.events=t}async runAll(){const t=Array.from(r.values()).filter(e=>!e.parent&&e.type==="suite"),s=Array.from(r.values()).some(e=>e.only);for(const e of t)await this.runSuite(e,s);return r}async runSingle(t){const s=r.get(t);if(!s||s.type!=="test")return;await this.runTest(s,!1)}async runSuite(t,s){this.events.onSuiteStart?.(t);const e=(t.children||[]).map(n=>r.get(n));for(const n of e)n.type==="suite"?await this.runSuite(n,s):n.type==="test"&&await this.runTest(n,s);this.events.onSuiteEnd?.(t)}async runTest(t,s){if(t.skip){this.events.onSkip(t);return}if(s&&!t.only)return;this.events.onStart?.(t);const e=g(t.parent);try{for(const n of e.before)await n();t.logs=[],await t.handler(),this.events.onPass(t)}catch(n){this.events.onFail(t,n)}finally{for(const n of e.after)await n()}}}window.__testRunner=u;exports.TestRunner=u;exports.afterEach=d;exports.beforeEach=p;exports.clearTests=y;exports.describe=f;exports.handlers=r;exports.it=l;
@@ -0,0 +1,41 @@
1
+ export interface Handler {
2
+ id: string;
3
+ name: string;
4
+ parent?: string;
5
+ handler: () => void | Promise<void>;
6
+ children?: string[];
7
+ type: 'suite' | 'test';
8
+ status?: 'idle' | 'pass' | 'fail' | 'skip' | 'running';
9
+ logs: string[];
10
+ depth: number;
11
+ only?: boolean;
12
+ skip?: boolean;
13
+ }
14
+ type HookFn = () => void | Promise<void>;
15
+ export declare const handlers: Map<string, Handler>;
16
+ export declare const describe: (name: string, handler: () => void) => void;
17
+ export declare const it: {
18
+ (name: string, handler: () => void | Promise<void>): void;
19
+ only(name: string, handler: () => void | Promise<void>): void;
20
+ skip(name: string, handler?: () => void | Promise<void>): void;
21
+ };
22
+ export declare const beforeEach: (fn: HookFn) => void;
23
+ export declare const afterEach: (fn: HookFn) => void;
24
+ export declare const clearTests: () => void;
25
+ export interface RunnerEvents {
26
+ onStart: (test: Handler) => void;
27
+ onPass: (test: Handler) => void;
28
+ onFail: (test: Handler, error: Error) => void;
29
+ onSkip: (test: Handler) => void;
30
+ onSuiteStart?: (suite: Handler) => void;
31
+ onSuiteEnd?: (suite: Handler) => void;
32
+ }
33
+ export declare class TestRunner {
34
+ private events;
35
+ constructor(events: RunnerEvents);
36
+ runAll(): Promise<Map<string, Handler>>;
37
+ runSingle(id: string): Promise<void>;
38
+ private runSuite;
39
+ private runTest;
40
+ }
41
+ export {};
@@ -0,0 +1,121 @@
1
+ const r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), a = [], h = () => Math.random().toString(36).substr(2, 9), p = (o, t) => {
2
+ const s = h(), e = a.at(-1);
3
+ r.set(s, {
4
+ id: s,
5
+ name: o,
6
+ type: "suite",
7
+ children: [],
8
+ logs: [],
9
+ depth: a.length,
10
+ parent: e,
11
+ handler: t
12
+ }), e && r.get(e).children.push(s), a.push(s), t(), a.pop();
13
+ }, l = (o, t) => {
14
+ const s = h(), e = a.at(-1), n = {
15
+ id: s,
16
+ name: o,
17
+ type: "test",
18
+ depth: a.length,
19
+ handler: t,
20
+ logs: [],
21
+ parent: e
22
+ };
23
+ e && r.get(e).children.push(s), r.set(s, n);
24
+ };
25
+ l.only = (o, t) => {
26
+ const s = h(), e = a.at(-1), n = {
27
+ id: s,
28
+ name: o,
29
+ type: "test",
30
+ depth: a.length,
31
+ handler: t,
32
+ logs: [],
33
+ parent: e,
34
+ only: !0
35
+ };
36
+ e && r.get(e).children.push(s), r.set(s, n);
37
+ };
38
+ l.skip = (o, t) => {
39
+ const s = h(), e = a.at(-1), n = {
40
+ id: s,
41
+ name: o,
42
+ type: "test",
43
+ depth: a.length,
44
+ handler: t || (() => {
45
+ }),
46
+ logs: [],
47
+ parent: e,
48
+ skip: !0
49
+ };
50
+ e && r.get(e).children.push(s), r.set(s, n);
51
+ };
52
+ const d = (o) => {
53
+ const t = a.at(-1);
54
+ if (!t) throw new Error("beforeEach() must be inside a describe()");
55
+ i.has(t) || i.set(t, []), i.get(t).push(o);
56
+ }, g = (o) => {
57
+ const t = a.at(-1);
58
+ if (!t) throw new Error("afterEach() must be inside a describe()");
59
+ c.has(t) || c.set(t, []), c.get(t).push(o);
60
+ }, u = (o) => {
61
+ const t = [], s = [];
62
+ let e = o;
63
+ for (; e; )
64
+ i.has(e) && t.unshift(...i.get(e)), c.has(e) && s.push(...c.get(e)), e = r.get(e)?.parent;
65
+ return { before: t, after: s };
66
+ }, y = () => {
67
+ r.clear(), i.clear(), c.clear();
68
+ };
69
+ class f {
70
+ events;
71
+ constructor(t) {
72
+ this.events = t;
73
+ }
74
+ async runAll() {
75
+ const t = Array.from(r.values()).filter(
76
+ (e) => !e.parent && e.type === "suite"
77
+ ), s = Array.from(r.values()).some((e) => e.only);
78
+ for (const e of t)
79
+ await this.runSuite(e, s);
80
+ return r;
81
+ }
82
+ async runSingle(t) {
83
+ const s = r.get(t);
84
+ if (!s || s.type !== "test") return;
85
+ await this.runTest(s, !1);
86
+ }
87
+ async runSuite(t, s) {
88
+ this.events.onSuiteStart?.(t);
89
+ const e = (t.children || []).map((n) => r.get(n));
90
+ for (const n of e)
91
+ n.type === "suite" ? await this.runSuite(n, s) : n.type === "test" && await this.runTest(n, s);
92
+ this.events.onSuiteEnd?.(t);
93
+ }
94
+ async runTest(t, s) {
95
+ if (t.skip) {
96
+ this.events.onSkip(t);
97
+ return;
98
+ }
99
+ if (s && !t.only) return;
100
+ this.events.onStart?.(t);
101
+ const e = u(t.parent);
102
+ try {
103
+ for (const n of e.before) await n();
104
+ t.logs = [], await t.handler(), this.events.onPass(t);
105
+ } catch (n) {
106
+ this.events.onFail(t, n);
107
+ } finally {
108
+ for (const n of e.after) await n();
109
+ }
110
+ }
111
+ }
112
+ window.__testRunner = f;
113
+ export {
114
+ f as TestRunner,
115
+ g as afterEach,
116
+ d as beforeEach,
117
+ y as clearTests,
118
+ p as describe,
119
+ r as handlers,
120
+ l as it
121
+ };
package/dist/twd.d.ts CHANGED
@@ -1,40 +1,6 @@
1
1
  import { Options, Rule } from './commands/mockBridge';
2
2
  import { TWDElemAPI } from './twd-types';
3
3
  import { URLCommandAPI } from './commands/url';
4
- /**
5
- * Stores the function to run before each test.
6
- */
7
- declare let beforeEachFn: (() => void | Promise<void>) | null;
8
- /**
9
- * Registers a function to run before each test.
10
- * @example
11
- * beforeEach(() => { ... });
12
- */
13
- export declare const beforeEach: (fn: typeof beforeEachFn) => void;
14
- /**
15
- * Groups related tests together.
16
- * @example
17
- * describe("My group", () => { ... });
18
- */
19
- export declare const describe: (name: string, fn: () => void) => void;
20
- /**
21
- * Defines a test case.
22
- * @example
23
- * it("does something", async () => { ... });
24
- */
25
- export declare const it: (name: string, fn: () => Promise<void> | void) => void;
26
- /**
27
- * Defines an exclusive test case (only this runs).
28
- * @example
29
- * itOnly("runs only this", async () => { ... });
30
- */
31
- export declare const itOnly: (name: string, fn: () => Promise<void> | void) => void;
32
- /**
33
- * Skips a test case.
34
- * @example
35
- * itSkip("skipped test", () => { ... });
36
- */
37
- export declare const itSkip: (name: string, _fn: () => Promise<void> | void) => void;
38
4
  interface TWDAPI {
39
5
  /**
40
6
  * Finds an element by selector and returns the TWD API for it.
@@ -49,6 +15,19 @@ interface TWDAPI {
49
15
  *
50
16
  */
51
17
  get: (selector: string) => Promise<TWDElemAPI>;
18
+ /**
19
+ * Sets the value of an input element and dispatches an input event. We recommend using this only for range, color, time inputs.
20
+ * @param el The input element
21
+ * @param value The value to set
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const input = await twd.get("input[type='time']");
26
+ * twd.setInputValue(input.el, "13:30");
27
+ *
28
+ * ```
29
+ */
30
+ setInputValue: (el: Element, value: string) => void;
52
31
  /**
53
32
  * Finds multiple elements by selector and returns an array of TWD APIs for them.
54
33
  * @param selector CSS selector
@@ -1,7 +1,17 @@
1
- import { TestCase } from '../twdRegistry';
1
+ interface Test {
2
+ name: string;
3
+ depth: number;
4
+ status?: "idle" | "pass" | "fail" | "skip" | "running";
5
+ logs?: string[];
6
+ parent?: string;
7
+ id: string;
8
+ type: "test" | "suite";
9
+ only?: boolean;
10
+ skip?: boolean;
11
+ }
2
12
  interface TestListProps {
3
- runTest: (i: number) => Promise<void>;
4
- tests: TestCase[];
13
+ runTest: (id: string) => Promise<void>;
14
+ tests: Test[];
5
15
  }
6
- export declare const TestList: ({ runTest, tests }: TestListProps) => import("react/jsx-runtime").JSX.Element;
16
+ export declare const TestList: ({ tests, runTest }: TestListProps) => import("react/jsx-runtime").JSX.Element;
7
17
  export {};
@@ -1,11 +1,21 @@
1
- import { TestCase } from '../twdRegistry';
1
+ interface Test {
2
+ name: string;
3
+ depth: number;
4
+ status?: "idle" | "pass" | "fail" | "skip" | "running";
5
+ logs?: string[];
6
+ id: string;
7
+ parent?: string;
8
+ type: "test" | "suite";
9
+ only?: boolean;
10
+ skip?: boolean;
11
+ }
2
12
  interface TestListItemProps {
3
- node: TestCase;
13
+ node: Test;
4
14
  depth: number;
5
- idx: number;
6
- runTest: (i: number) => void;
15
+ id: string;
16
+ runTest: (i: string) => void;
7
17
  }
8
- export declare const statusStyles: (node: TestCase) => {
18
+ export declare const statusStyles: (node: Test) => {
9
19
  item: {
10
20
  background: string;
11
21
  };
@@ -25,5 +35,5 @@ export declare const assertStyles: (text: string) => {
25
35
  color?: undefined;
26
36
  fontWeight?: undefined;
27
37
  };
28
- export declare const TestListItem: ({ node, depth, idx, runTest }: TestListItemProps) => import("react/jsx-runtime").JSX.Element;
38
+ export declare const TestListItem: ({ node, depth, id, runTest, }: TestListItemProps) => import("react/jsx-runtime").JSX.Element;
29
39
  export {};
@@ -0,0 +1,14 @@
1
+ interface Test {
2
+ name: string;
3
+ depth: number;
4
+ status?: "idle" | "pass" | "fail" | "skip" | "running";
5
+ logs?: string[];
6
+ id: string;
7
+ parent?: string;
8
+ type: "test" | "suite";
9
+ }
10
+ export type Node = Test & {
11
+ childrenNodes?: Node[];
12
+ };
13
+ export declare const buildTreeFromHandlers: (handlers: Test[]) => Node[];
14
+ export {};
@@ -0,0 +1,6 @@
1
+ interface UseLayoutProps {
2
+ isOpen: boolean;
3
+ position: "left" | "right";
4
+ }
5
+ export declare const useLayout: ({ isOpen, position }: UseLayoutProps) => void;
6
+ export {};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("fs"),o=require("path");function r(){return{name:"remove-mock-sw",apply:"build",closeBundle(){try{e.rmSync(o.resolve("dist/mock-sw.js")),console.log("🧹 Removed mock-sw.js from build")}catch{console.log("🧹 No mock-sw.js found in build")}}}}exports.removeMockServiceWorker=r;
@@ -0,0 +1 @@
1
+ export { removeMockServiceWorker } from './plugin/removeMockServiceWorker';