@stacksjs/testing 0.75.575 → 0.75.579

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 (3) hide show
  1. package/dist/hq.d.ts +10 -0
  2. package/dist/hq.js +2 -2
  3. package/package.json +14 -14
package/dist/hq.d.ts CHANGED
@@ -50,6 +50,15 @@ export declare interface FakeHqIssue {
50
50
  first_seen: string
51
51
  last_seen: string
52
52
  }
53
+ export declare interface FakeHqConversation {
54
+ id: string
55
+ project: string
56
+ subject: string
57
+ status: string
58
+ customer_id: string | null
59
+ updated_at: string
60
+ unread_count: number
61
+ }
53
62
  export declare interface FakeHqService {
54
63
  service: string
55
64
  origin: string
@@ -62,5 +71,6 @@ export declare interface FakeHqService {
62
71
  licenses: () => LicenseGrant[]
63
72
  logs: () => FakeHqLog[]
64
73
  issues: () => FakeHqIssue[]
74
+ seedConversation: (conversation: FakeHqConversation) => void
65
75
  requests: () => string[]
66
76
  }
package/dist/hq.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // @bun
2
- import{createHqService as O,createMemoryConnectCodeStore as K}from"@stacksjs/hq/service";function g(n,a=200){return new Response(JSON.stringify(n),{status:a,headers:{"Content-Type":"application/json"}})}function M(n){return/^Bearer\s+(\S+)$/i.exec(n.headers.get("authorization")??"")?.[1]??null}function A(n={}){let a=n.service??"fakehq",m=(n.origin??`https://${a}.test`).replace(/\/+$/,""),q=n.meters??[{meter:"events",used:0,limit:500}],h=n.now??(()=>new Date),d=new Map,C=new Map,v=[],I=new Map,S=[],R=[],P=[],w={id:"fake-user"};function H(e,t){let r=`${e}/${t}`,i=d.get(r);if(!i){let j=crypto.randomUUID();i={owner:e,grant:{project:{id:j,name:t},ingestKey:`${a}_ingest_${crypto.randomUUID()}`,readKey:`${a}_read_${crypto.randomUUID()}`,dashboardUrl:`${n.relativeDashboardUrl?"":m}/projects/${j}`}},d.set(r,i)}return i.grant}function L(e,t){let r=`${e}/${t}`,i=C.get(r);if(!i){let j=crypto.randomUUID();i={owner:e,grant:{project:{id:j,name:t},licenseKey:`rhq_lic_${crypto.randomUUID().replaceAll("-","")}`,dashboardUrl:`${m}/account`}},C.set(r,i)}return i.grant}let y=O({service:a,approveUrl:(e)=>`${m}/cli/connect?code=${encodeURIComponent(e)}`,upgradeUrl:`${m}/pricing`,codes:K(),provision:async(e,{app:t})=>n.license?L(e.id,t):H(e.id,t),...n.license?{}:{async authenticateReadKey(e){for(let{grant:t,owner:r}of d.values())if(t.readKey===e)return{owner:r,project:t.project.id};return null},usage:async()=>q.map((e)=>({...e}))}},{now:h}),x=(e)=>e?[...d.values()].find((t)=>t.grant.ingestKey===e):void 0,_=(e,t)=>{let r=M(e);return[...d.values()].some((i)=>i.grant.project.id===t&&i.grant.readKey===r)},F=(e)=>`${e.slice(0,3)}-${e.slice(3,6)}`.toUpperCase();async function G(e){let t=new URL(e.url),r=t.pathname.replace(/\/+$/,"")||"/",i=e.method.toUpperCase();if(P.push(`${i} ${r}`),(r.startsWith("/api/cli/connect")||r==="/api/v1/usage")&&n.contract===!1)return g({error:"not found"},404);if(i==="POST"&&r==="/api/cli/connect"){let o=await y.startConnect(e),c=await o.json();return v.push(c.code),g(n.confirmCode?{...c,confirmCode:F(c.code)}:c,o.status)}if(i==="GET"&&r.startsWith("/api/cli/connect/")){let o=decodeURIComponent(r.slice(17)),c=(I.get(o)??0)+1;if(I.set(o,c),n.approveOnPoll!==void 0&&c===n.approveOnPoll)await y.approveConnect(o,w);return y.pollConnect(e,o)}if(i==="GET"&&r==="/api/v1/usage")return y.usage(e);if(i==="POST"&&(r==="/logs"||r==="/errors")){let o=await e.json().catch(()=>({})),c=e.headers.get(r==="/logs"?"x-loghq-key":"x-bughq-key"),l=x(c??(typeof o.key==="string"?o.key:null));if(!l)return g({error:"invalid ingest key"},401);let f=h().toISOString();if(r==="/logs"){let u=Array.isArray(o.logs)?o.logs:[o],p=u.filter((s)=>s&&s.message!=null);if(!p.length)return g({error:"no log entries"},400);if(n.storeIngest!==!1)for(let s of p)S.push({id:crypto.randomUUID(),project:l.grant.project.id,level:String(s.level??"info"),message:String(s.message),timestamp:String(s.timestamp??f)});return g({ok:!0,stored:n.storeIngest===!1?0:p.length,dropped:0,skipped:u.length-p.length},201)}if(!o.message)return g({error:"missing message"},400);let k=String(o.type??"Error"),U=`${k}: ${String(o.message).split(`
3
- `)[0]}`;if(n.storeIngest!==!1){let u=R.find((p)=>p.project===l.grant.project.id&&p.title===U);if(u)u.count+=1,u.last_seen=f;else R.push({id:crypto.randomUUID(),project:l.grant.project.id,title:U,error_type:k,level:String(o.level??"error"),status:"unresolved",count:1,first_seen:f,last_seen:f})}return n.storeIngest===!1?g({ok:!0,sampled:!0}):g({ok:!0},201)}let b=/^\/api\/projects\/([^/]+)\/(logs|issues)$/.exec(r);if(i==="GET"&&b){let o=decodeURIComponent(b[1]);if(!_(e,o))return g({error:"not found"},404);if(b[2]==="logs"){let l=(t.searchParams.get("q")??"").toLowerCase(),f=(t.searchParams.get("level")??"").split(",").filter(Boolean),k=t.searchParams.get("before"),U=Math.min(200,Math.max(1,Number(t.searchParams.get("limit"))||100)),u=S.filter((s)=>s.project===o).filter((s)=>!l||s.message.toLowerCase().includes(l)).filter((s)=>!f.length||f.includes(s.level)).filter((s)=>!k||s.timestamp<k).sort((s,D)=>D.timestamp.localeCompare(s.timestamp)),p=u.slice(0,U);return g({logs:p.map((s)=>({id:s.id,level:s.level,message:s.message,channel:null,environment:null,timestamp:s.timestamp,created_at:s.timestamp})),nextCursor:u.length>U?p[p.length-1].timestamp:null})}let c=t.searchParams.get("status");return g({issues:R.filter((l)=>l.project===o&&(!c||l.status===c)).map((l)=>({...l,project:void 0,culprit:null}))})}return g({error:"not found"},404)}return{service:a,origin:m,fetch:(e,t)=>G(e instanceof Request?e:new Request(String(e),t)),approve:async(e)=>{let t=e??v[v.length-1];return t?y.approveConnect(t,w):{ok:!1,reason:"expired"}},project:(e="app")=>H(w.id,e),codes:()=>[...v],confirmCodeFor:(e)=>n.confirmCode&&v.includes(e)?F(e):void 0,grants:()=>[...d.values()].map((e)=>e.grant),licenses:()=>[...C.values()].map((e)=>e.grant),logs:()=>[...S],issues:()=>[...R],requests:()=>[...P]}}function E(...n){return async(a,m)=>{let q=new URL(a instanceof Request?a.url:String(a)),h=n.find((d)=>d.origin===q.origin);if(!h)throw TypeError(`fetch failed: nothing answers ${q.origin}`);return h.fetch(a,m)}}export{A as createFakeHqService,E as fakeHqFetch};
2
+ import{createHqService as K,createMemoryConnectCodeStore as T}from"@stacksjs/hq/service";function g(s,d=200){return new Response(JSON.stringify(s),{status:d,headers:{"Content-Type":"application/json"}})}function E(s){return/^Bearer\s+(\S+)$/i.exec(s.headers.get("authorization")??"")?.[1]??null}function B(s={}){let d=s.service??"fakehq",v=(s.origin??`https://${d}.test`).replace(/\/+$/,""),q=s.meters??[{meter:"events",used:0,limit:500}],y=s.now??(()=>new Date),f=new Map,U=new Map,j=[],I=new Map,b=[],R=[],P=[],H=[],_={id:"fake-user"};function x(e,t){let r=`${e}/${t}`,i=f.get(r);if(!i){let k=crypto.randomUUID();i={owner:e,grant:{project:{id:k,name:t},ingestKey:`${d}_ingest_${crypto.randomUUID()}`,readKey:`${d}_read_${crypto.randomUUID()}`,dashboardUrl:`${s.relativeDashboardUrl?"":v}/projects/${k}`}},f.set(r,i)}return i.grant}function G(e,t){let r=`${e}/${t}`,i=U.get(r);if(!i){let k=crypto.randomUUID();i={owner:e,grant:{project:{id:k,name:t},licenseKey:`rhq_lic_${crypto.randomUUID().replaceAll("-","")}`,dashboardUrl:`${v}/account`}},U.set(r,i)}return i.grant}let C=K({service:d,approveUrl:(e)=>`${v}/cli/connect?code=${encodeURIComponent(e)}`,upgradeUrl:`${v}/pricing`,codes:T(),provision:async(e,{app:t})=>s.license?G(e.id,t):x(e.id,t),...s.license?{}:{async authenticateReadKey(e){for(let{grant:t,owner:r}of f.values())if(t.readKey===e)return{owner:r,project:t.project.id};return null},usage:async()=>q.map((e)=>({...e}))}},{now:y}),D=(e)=>e?[...f.values()].find((t)=>t.grant.ingestKey===e):void 0,F=(e,t)=>{let r=E(e);return[...f.values()].some((i)=>i.grant.project.id===t&&i.grant.readKey===r)},w=(e)=>`${e.slice(0,3)}-${e.slice(3,6)}`.toUpperCase();async function M(e){let t=new URL(e.url),r=t.pathname.replace(/\/+$/,"")||"/",i=e.method.toUpperCase();if(H.push(`${i} ${r}`),(r.startsWith("/api/cli/connect")||r==="/api/v1/usage")&&s.contract===!1)return g({error:"not found"},404);if(i==="POST"&&r==="/api/cli/connect"){let n=await C.startConnect(e),c=await n.json();return j.push(c.code),g(s.confirmCode?{...c,confirmCode:w(c.code)}:c,n.status)}if(i==="GET"&&r.startsWith("/api/cli/connect/")){let n=decodeURIComponent(r.slice(17)),c=(I.get(n)??0)+1;if(I.set(n,c),s.approveOnPoll!==void 0&&c===s.approveOnPoll)await C.approveConnect(n,_);return C.pollConnect(e,n)}if(i==="GET"&&r==="/api/v1/usage")return C.usage(e);if(i==="POST"&&(r==="/logs"||r==="/errors")){let n=await e.json().catch(()=>({})),c=e.headers.get(r==="/logs"?"x-loghq-key":"x-bughq-key"),l=D(c??(typeof n.key==="string"?n.key:null));if(!l)return g({error:"invalid ingest key"},401);let p=y().toISOString();if(r==="/logs"){let o=Array.isArray(n.logs)?n.logs:[n],u=o.filter((a)=>a&&a.message!=null);if(!u.length)return g({error:"no log entries"},400);if(s.storeIngest!==!1)for(let a of u)b.push({id:crypto.randomUUID(),project:l.grant.project.id,level:String(a.level??"info"),message:String(a.message),timestamp:String(a.timestamp??p)});return g({ok:!0,stored:s.storeIngest===!1?0:u.length,dropped:0,skipped:o.length-u.length},201)}if(!n.message)return g({error:"missing message"},400);let h=String(n.type??"Error"),m=`${h}: ${String(n.message).split(`
3
+ `)[0]}`;if(s.storeIngest!==!1){let o=R.find((u)=>u.project===l.grant.project.id&&u.title===m);if(o)o.count+=1,o.last_seen=p;else R.push({id:crypto.randomUUID(),project:l.grant.project.id,title:m,error_type:h,level:String(n.level??"error"),status:"unresolved",count:1,first_seen:p,last_seen:p})}return s.storeIngest===!1?g({ok:!0,sampled:!0}):g({ok:!0},201)}let L=/^\/api\/v1\/projects\/([^/]+)\/conversations$/.exec(r);if(i==="GET"&&L){let n=decodeURIComponent(L[1]);if(!F(e,n))return g({error:"not found"},404);let c=t.searchParams.get("status"),l=t.searchParams.get("before"),p=Math.min(100,Math.max(1,Number(t.searchParams.get("limit"))||100)),h=P.filter((o)=>o.project===n&&(!c||o.status===c)&&(!l||o.updated_at<l)).sort((o,u)=>u.updated_at.localeCompare(o.updated_at)),m=h.slice(0,p);return g({conversations:m.map((o)=>({id:o.id,subject:o.subject,status:o.status,customer_id:o.customer_id,updated_at:o.updated_at,unread_count:o.unread_count})),nextCursor:h.length>p?m[m.length-1].updated_at:null})}let S=/^\/api\/projects\/([^/]+)\/(logs|issues)$/.exec(r);if(i==="GET"&&S){let n=decodeURIComponent(S[1]);if(!F(e,n))return g({error:"not found"},404);if(S[2]==="logs"){let l=(t.searchParams.get("q")??"").toLowerCase(),p=(t.searchParams.get("level")??"").split(",").filter(Boolean),h=t.searchParams.get("before"),m=Math.min(200,Math.max(1,Number(t.searchParams.get("limit"))||100)),o=b.filter((a)=>a.project===n).filter((a)=>!l||a.message.toLowerCase().includes(l)).filter((a)=>!p.length||p.includes(a.level)).filter((a)=>!h||a.timestamp<h).sort((a,O)=>O.timestamp.localeCompare(a.timestamp)),u=o.slice(0,m);return g({logs:u.map((a)=>({id:a.id,level:a.level,message:a.message,channel:null,environment:null,timestamp:a.timestamp,created_at:a.timestamp})),nextCursor:o.length>m?u[u.length-1].timestamp:null})}let c=t.searchParams.get("status");return g({issues:R.filter((l)=>l.project===n&&(!c||l.status===c)).map((l)=>({...l,project:void 0,culprit:null}))})}return g({error:"not found"},404)}return{service:d,origin:v,fetch:(e,t)=>M(e instanceof Request?e:new Request(String(e),t)),approve:async(e)=>{let t=e??j[j.length-1];return t?C.approveConnect(t,_):{ok:!1,reason:"expired"}},project:(e="app")=>x(_.id,e),codes:()=>[...j],confirmCodeFor:(e)=>s.confirmCode&&j.includes(e)?w(e):void 0,grants:()=>[...f.values()].map((e)=>e.grant),licenses:()=>[...U.values()].map((e)=>e.grant),logs:()=>[...b],issues:()=>[...R],seedConversation:(e)=>P.push({...e}),requests:()=>[...H]}}function N(...s){return async(d,v)=>{let q=new URL(d instanceof Request?d.url:String(d)),y=s.find((f)=>f.origin===q.origin);if(!y)throw TypeError(`fetch failed: nothing answers ${q.origin}`);return y.fetch(d,v)}}export{B as createFakeHqService,N as fakeHqFetch};
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/testing",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.575",
5
+ "version": "0.75.579",
6
6
  "description": "The Stacks way of testing.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -91,19 +91,19 @@
91
91
  "prepublishOnly": "bun run build"
92
92
  },
93
93
  "dependencies": {
94
- "@stacksjs/auth": "0.75.575",
95
- "@stacksjs/cache": "0.75.575",
96
- "@stacksjs/cli": "0.75.575",
97
- "@stacksjs/config": "0.75.575",
98
- "@stacksjs/database": "0.75.575",
99
- "@stacksjs/email": "0.75.575",
100
- "@stacksjs/events": "0.75.575",
101
- "@stacksjs/hq": "0.75.575",
102
- "@stacksjs/path": "0.75.575",
103
- "@stacksjs/router": "0.75.575",
104
- "@stacksjs/server": "0.75.575",
105
- "@stacksjs/storage": "0.75.575",
106
- "@stacksjs/strings": "0.75.575",
94
+ "@stacksjs/auth": "0.75.579",
95
+ "@stacksjs/cache": "0.75.579",
96
+ "@stacksjs/cli": "0.75.579",
97
+ "@stacksjs/config": "0.75.579",
98
+ "@stacksjs/database": "0.75.579",
99
+ "@stacksjs/email": "0.75.579",
100
+ "@stacksjs/events": "0.75.579",
101
+ "@stacksjs/hq": "0.75.579",
102
+ "@stacksjs/path": "0.75.579",
103
+ "@stacksjs/router": "0.75.579",
104
+ "@stacksjs/server": "0.75.579",
105
+ "@stacksjs/storage": "0.75.579",
106
+ "@stacksjs/strings": "0.75.579",
107
107
  "@stacksjs/ts-cloud": "^0.16.125",
108
108
  "very-happy-dom": "^0.4.1"
109
109
  },