← back to Inline Editor

test/pilot.js

90 lines

#!/usr/bin/env node
/**
 * Local smoke-test pilot — spins up a tiny Express app that serves a sample
 * sister-site page, mounts @dw/inline-editor with site=test.local, and
 * uses HS256 dev-secret auth.
 *
 * Usage:
 *   node test/pilot.js
 *   Then open http://127.0.0.1:9791 with a dev admin cookie:
 *     curl http://127.0.0.1:9791/_devlogin > /dev/null --cookie-jar /tmp/dw.cookies
 *     # then visit http://127.0.0.1:9791/ in your browser with that cookie
 */
const path = require('path');
const express = require('express');
const jwt = require('jsonwebtoken');
const inlineEditor = require('../src/middleware');

const PORT = 9791;
const SITE = 'test.local';
const DEV_SECRET = 'pilot-dev-secret';
process.env.AUTH_DEV_SECRET = DEV_SECRET;

const app = express();
const uploadDir = path.join(__dirname, '..', 'tmp', 'uploads');
app.use('/uploads', express.static(uploadDir));

// Dev login — sets a signed HS256 JWT cookie as 'admin@dw'.
app.get('/_devlogin', (_req, res) => {
  const token = jwt.sign({
    email: 'admin@dw',
    role: 'admin',
    iss: 'https://auth.agentabrams.com'
  }, DEV_SECRET, { algorithm: 'HS256', expiresIn: '8h' });
  res.cookie('dw_auth', token, { httpOnly: false });
  res.type('text/plain').send(`Logged in as admin@dw. Refresh /\n`);
});
app.get('/_devlogout', (_req, res) => {
  res.clearCookie('dw_auth'); res.type('text/plain').send('Logged out\n');
});

// Mount the inline editor (BEFORE the page handler that calls res.send)
app.use(inlineEditor({
  site: SITE,
  uploadDir,
  authMode: 'dev',
  devSecret: DEV_SECRET
}));

// Sample sister-site page (kept simple — same structure as a stripped corkwallcovering or retrowalls front)
app.get('/', (_req, res) => {
  res.type('text/html').send(`<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><title>retrowalls — sample sister site</title>
<style>
  body { font-family: -apple-system, system-ui, sans-serif; margin: 0; color: #1a1a1a; }
  header { padding: 32px 48px; border-bottom: 1px solid #eee; }
  header h1 { margin: 0; font-weight: 300; letter-spacing: .5px; }
  .hero { padding: 80px 48px; text-align: center; background: #f5f1eb; }
  .hero h2 { font-size: 48px; font-weight: 200; margin: 0 0 16px; }
  .hero p { font-size: 17px; color: #555; max-width: 640px; margin: 0 auto; }
  .hero img { max-width: 100%; margin-top: 32px; }
  .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 32px; padding: 48px; }
  .grid .item { border: 1px solid #eee; padding: 20px; }
  .grid h3 { margin: 0 0 8px; }
  footer { text-align: center; padding: 32px; border-top: 1px solid #eee; color: #888; }
</style></head><body>
<header>
  <h1 id="brand">retrowalls</h1>
  <p id="tagline">Mid-century wallcovering reissued for the modern home.</p>
</header>
<section class="hero" data-edit-region="true">
  <h2>Discover the archive.</h2>
  <p>Hand-printed wallpaper drawn from 1960s and 1970s pattern books, reissued in trade-spec rolls.</p>
  <p><a href="/catalog">Browse the catalog</a> · <a href="/samples">Order samples</a></p>
  <img src="https://placehold.co/1280x640/333/fff?text=Hero+Image" alt="Hero pattern showcase">
</section>
<section class="grid">
  <div class="item"><h3>Nostalgia</h3><p>Patterns straight from the period archives.</p></div>
  <div class="item"><h3>Trade-spec</h3><p>Yards, repeats, fire ratings — all there.</p></div>
  <div class="item"><h3>Samples</h3><p>Order before you commit.</p></div>
</section>
<footer><a href="mailto:hi@retrowalls.example">hi@retrowalls.example</a></footer>
</body></html>`);
});

app.listen(PORT, '127.0.0.1', () => {
  console.log(`pilot → http://127.0.0.1:${PORT}`);
  console.log(`        admin login: http://127.0.0.1:${PORT}/_devlogin`);
});