Files
zeekay b4f8d7b2f4 feat(shopify): Hanzo AI for Shopify — embedded admin app on the shared foundation
Add packages/shopify (@hanzo/shopify): a Shopify embedded admin app that puts
Hanzo AI over the store as system of record.

- Product content: generate/rewrite descriptions + SEO title/meta from product
  attributes, then write back via the productUpdate mutation (only changed fields).
- Orders/support: summarize an order, draft a customer reply, extract issues.
- Ask: freeform question over a product or order, grounded in its data.

Server (src/server, holds the only secrets):
- oauth.ts   Shopify OAuth request shaping + OAuth-callback HMAC verify (hex over
             Shopify-canonical URLSearchParams form) + state (CSRF) check.
- shopify-api.ts  Admin GraphQL wrappers (getProduct/getOrder/updateProduct) —
                  pure request shaping + parsing + productUpdate userErrors.
- hanzo.ts   thin over the PUBLISHED @hanzo/ai createAiClient; product/order
             context assembly + prompt building. No transport reimplemented.
- actions.ts product + order AI action catalogs, one prompt each over ask.
- webhooks.ts  webhook HMAC verify (base64 over RAW body) + topic routing
               (orders/create, app/uninstalled, the 3 GDPR topics).
- server.ts  OAuth + /v1/* proxy the frontend calls + /webhooks. HMAC verified
             before any param/body is trusted; shop validated to *.myshopify.com.

Frontend (src/app): Polaris + App Bridge React panel — api.ts (proxy client,
no secrets), context.ts (launch context → GID), session-fetch.ts (App Bridge
session-token fetch), App/Assistant/main. App Bridge loaded from Shopify CDN;
public API key stamped into the app, secret never bundled.

Plus shopify.app.toml, an admin-link extension, build.js (esbuild: app bundle +
Node server), tsconfig, vitest config, README. 108 vitest tests (pure), tsc
--noEmit clean, build green. Model calls via @hanzo/ai to api.hanzo.ai/v1 only.

Register packages/shopify in pnpm-workspace.yaml.
2026-07-03 20:53:10 -07:00

46 lines
1.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { toGid, parseLaunchContext } from '../src/app/context';
describe('toGid', () => {
it('wraps a bare numeric id into a GID for the kind', () => {
expect(toGid('Product', '123')).toBe('gid://shopify/Product/123');
expect(toGid('Order', '9')).toBe('gid://shopify/Order/9');
});
it('leaves an existing GID unchanged', () => {
expect(toGid('Product', 'gid://shopify/Product/123')).toBe('gid://shopify/Product/123');
});
it('returns empty for empty input', () => {
expect(toGid('Product', '')).toBe('');
expect(toGid('Product', ' ')).toBe('');
});
});
describe('parseLaunchContext', () => {
it('reads shop + host', () => {
const ctx = parseLaunchContext('?shop=my-store.myshopify.com&host=abc123');
expect(ctx.shop).toBe('my-store.myshopify.com');
expect(ctx.host).toBe('abc123');
expect(ctx.resource).toBe('');
expect(ctx.resourceId).toBe('');
});
it('normalizes a bare product id to a GID', () => {
const ctx = parseLaunchContext('?shop=s.myshopify.com&resource=product&id=555');
expect(ctx.resource).toBe('product');
expect(ctx.resourceId).toBe('gid://shopify/Product/555');
});
it('normalizes a bare order id to a GID', () => {
const ctx = parseLaunchContext('?resource=order&id=777');
expect(ctx.resource).toBe('order');
expect(ctx.resourceId).toBe('gid://shopify/Order/777');
});
it('preserves a GID that is already passed', () => {
const ctx = parseLaunchContext('?resource=product&id=gid://shopify/Product/1');
expect(ctx.resourceId).toBe('gid://shopify/Product/1');
});
it('ignores an unknown resource type', () => {
const ctx = parseLaunchContext('?resource=collection&id=1');
expect(ctx.resource).toBe('');
expect(ctx.resourceId).toBe('1');
});
});