Compare commits
8 Commits
6de2fcc1bd
...
036a5116b8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
036a5116b8 | ||
|
|
4630813644 | ||
|
|
38271d0d88 | ||
|
|
fff9f05bae | ||
|
|
049b4fd280 | ||
|
|
604b983a51 | ||
|
|
4ca4cacda0 | ||
|
|
3b64635339 |
44
README.md
44
README.md
@@ -4,6 +4,8 @@ A private, Tinder-style web app for browsing the motorcycle market: swipe real l
|
||||
|
||||
**Status:** Phase 3 (personalization) built on `dev` (2026-11-11): sections of 25 (wildcards + second looks + new), taste engine with match points, tank gauges, dossier "Why this rank", real Trends tab, agent pass (mock / live / fallback). Phase 2 remains shipped as-is; deployment (podman stack) is Phase 4 — not started.
|
||||
|
||||
**B1 (photo experience v2)** built on `dev`: full photo galleries mirrored locally, deck flips (tap left/right thirds, clamped), segment bars, a fullscreen viewer (swipe nav, pinch/double-tap zoom, pull-down close), and repeat rotation (a re-shown card leads at its next photo). See UI-SPEC §11 + the "B1 develop notes" section below.
|
||||
|
||||
## How it works
|
||||
|
||||
```
|
||||
@@ -87,6 +89,48 @@ Stage 2 + the task brief.
|
||||
|
||||
`data/` and `.venv/` are gitignored — the DB, image mirror and venv are local state.
|
||||
|
||||
## B1 develop notes (photo experience v2)
|
||||
|
||||
- **Columns** (guarded in `db.init_db`): `listings.imgs TEXT` (JSON array of
|
||||
gallery URLs, page order; entry 0 = the primary = `img_url`) ·
|
||||
`swipes.last_photo_idx INTEGER` (photo on screen at swipe time; drives §11.5
|
||||
rotation `(last+1) % n`; NULL = photo 0).
|
||||
- **Importer** copies the hunt state's `imgs[]` into `listings.imgs`; `img_url`
|
||||
stays the state `img` and falls back to `imgs[0]`. Re-imports are
|
||||
byte-stable (selfcheck proves it on the 640-row copy).
|
||||
- **Route:** `GET /img/{id}?i=n` — mirrored file → CDN 302 → 404. `i=0`
|
||||
(default) keeps the legacy chain (`{id}.webp`, then `{id}_0.webp`, then
|
||||
`img_url`); `i>0` serves `{id}_{n}.webp` with the CDN fallback from
|
||||
`imgs[n]`. Out-of-range/unknown → 404.
|
||||
- **Mirror:** `app/imgfetch.py` downloads every `imgs[n]`. The primary (n=0)
|
||||
lands at the legacy `data/img/{id}.webp` (one canonical file — no duplicate
|
||||
fetch); extras land at `data/img/{id}_{n}.webp`. Same politeness as P2
|
||||
(~1 req/s, skip-existing, `.part`-atomic, per-file CLI progress).
|
||||
- **Payload:** deck/section/list/listing items add `n_photos` (int) and
|
||||
`last_photo_idx` (int|null). `POST /api/swipe` accepts optional `photo_idx`
|
||||
(non-negative int; persisted; absent key preserves, explicit null clears).
|
||||
- **Frontend:** tap LEFT/RIGHT third = photo flip (clamped, no wrap, commits
|
||||
nothing), CENTER third = fullscreen viewer (ⓘ still wins); segment bars per
|
||||
photo (Tinder-style; band chips drop to `top:22px` when segments exist);
|
||||
viewer = swipe nav + pinch 1×–4× + double-tap 1×↔2.5× + ✕/pull-down close
|
||||
(returns to the same card/photo); re-shown cards lead at `(last+1) % n`;
|
||||
preloads the top card's next photo + next card's lead; `sw.js` cache is
|
||||
`bike-tinder-v3`.
|
||||
- **FE walk (evidence):** `app/fe_walk_b1.py` — deterministic, headless:
|
||||
|
||||
```sh
|
||||
.venv/bin/python -m app.fe_walk_b1 setup # fixtures: copy DB + 701-706 synthetics + local photos
|
||||
env -u OPENROUTER_API_KEY .venv/bin/python -m app.serve \
|
||||
--db /var/home/admin/dev/bike-tinder-b1/test-walk.sqlite3 \
|
||||
--img-dir /var/home/admin/dev/bike-tinder-b1/test-img \
|
||||
--host 127.0.0.1 --port 9454 # scratch server (mock forge)
|
||||
.venv/bin/python -m app.fe_walk_b1 # 390 DOM checks
|
||||
```
|
||||
- **Test data:** `b1/test-state/` = hunt-state sample with `imgs` (8/3-photo
|
||||
galleries; refresh `fetched_at` before an import gate test) ·
|
||||
`b1/test-img/` = staged `{id}_{n}.webp` photos · `b1/test-copy.sqlite3` =
|
||||
pristine live-DB snapshot (selfcheck copies it; never write to it).
|
||||
|
||||
## Deployment (planned)
|
||||
|
||||
A small podman compose stack on the Rotarran under `/var/mnt/DATA/podman-stacks/bike-tinder/`, its own systemd user unit (`Delegate=yes`), SQLite database. The stack ships its own Tailscale node (sidecar container — no host-tailnet coupling) and serves the app privately at `https://biketinder.tailf35a34.ts.net`. Single user; reachable only from Glenn's own devices.
|
||||
|
||||
94
app/api.py
94
app/api.py
@@ -12,6 +12,9 @@ on; frozen in the task brief):
|
||||
url, img ("/img/{id}" if img_url or cached file exists else null), desc,
|
||||
family, score, year, seller_type, extras (array of strings),
|
||||
decision (null|'like'|'maybe'|'nope'), first_seen}
|
||||
B1 additions (UI-SPEC §11 / DATA-MODEL "B1 additions"):
|
||||
n_photos (int), last_photo_idx (int|null); GET /img/{id}?i=n serves
|
||||
gallery photo n.
|
||||
"""
|
||||
import datetime
|
||||
import json
|
||||
@@ -42,7 +45,7 @@ DECK_WHERE = (
|
||||
)
|
||||
|
||||
DECK_SELECT = (
|
||||
"SELECT l.*, s.action AS decision"
|
||||
"SELECT l.*, s.action AS decision, s.last_photo_idx AS last_photo_idx"
|
||||
" FROM listings l LEFT JOIN swipes s ON s.listing_id = l.id"
|
||||
" WHERE " + DECK_WHERE
|
||||
)
|
||||
@@ -128,10 +131,21 @@ def count_deck_after(app, score, first_seen, listing_id):
|
||||
|
||||
def build_payload(img_dir):
|
||||
def _payload(conn, row):
|
||||
img = None
|
||||
if row['img_url'] or os.path.exists(
|
||||
os.path.join(img_dir, '%d.webp' % row['id'])):
|
||||
img = '/img/%d' % row['id']
|
||||
leg = os.path.join(img_dir, '%d.webp' % row['id'])
|
||||
b1 = os.path.join(img_dir, '%d_0.webp' % row['id'])
|
||||
has_photo = (bool(row['img_url']) or os.path.exists(leg)
|
||||
or os.path.exists(b1))
|
||||
img = '/img/%d' % row['id'] if has_photo else None
|
||||
# B1: gallery size + swipe-time photo index (UI-SPEC §11 / DATA-MODEL B1)
|
||||
imgs = []
|
||||
if isinstance(row['imgs'], str) and row['imgs']:
|
||||
try:
|
||||
imgs = json.loads(row['imgs'])
|
||||
except ValueError:
|
||||
imgs = []
|
||||
if not isinstance(imgs, list):
|
||||
imgs = []
|
||||
n_photos = len(imgs) if imgs else (1 if has_photo else 0)
|
||||
extras = row['extras']
|
||||
if isinstance(extras, str):
|
||||
try:
|
||||
@@ -148,6 +162,9 @@ def build_payload(img_dir):
|
||||
'region': row['region'],
|
||||
'url': row['url'],
|
||||
'img': img,
|
||||
'n_photos': n_photos,
|
||||
'last_photo_idx': (row['last_photo_idx']
|
||||
if 'last_photo_idx' in row.keys() else None),
|
||||
'desc': row['desc'],
|
||||
'family': row['family'],
|
||||
'score': row['score'],
|
||||
@@ -290,6 +307,15 @@ def create_app(db_path, img_dir, state_dir=None):
|
||||
section_id = body.get('section_id')
|
||||
if not isinstance(section_id, int):
|
||||
section_id = None
|
||||
# B1: optional photo index on screen at swipe time (§11.5 rotation).
|
||||
has_photo_idx = 'photo_idx' in body
|
||||
photo_idx = body.get('photo_idx')
|
||||
if has_photo_idx and photo_idx is not None:
|
||||
if (isinstance(photo_idx, bool) or not isinstance(photo_idx, int)
|
||||
or photo_idx < 0):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail='photo_idx must be a non-negative int (or null)')
|
||||
conn = get_conn(app)
|
||||
try:
|
||||
row = conn.execute('SELECT id FROM listings WHERE id=?',
|
||||
@@ -297,6 +323,18 @@ def create_app(db_path, img_dir, state_dir=None):
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail='unknown listing')
|
||||
now = imp.now_iso()
|
||||
if has_photo_idx:
|
||||
# explicit photo_idx (int or null) overwrites; an absent key
|
||||
# preserves the stored index (list edits etc.)
|
||||
conn.execute(
|
||||
'''INSERT INTO swipes (listing_id, action, created_at,
|
||||
updated_at, last_photo_idx)
|
||||
VALUES (?,?,?,?,?)
|
||||
ON CONFLICT(listing_id) DO UPDATE SET
|
||||
action=excluded.action, updated_at=excluded.updated_at,
|
||||
last_photo_idx=excluded.last_photo_idx''',
|
||||
(listing_id, action, now, now, photo_idx))
|
||||
else:
|
||||
conn.execute(
|
||||
'''INSERT INTO swipes (listing_id, action, created_at, updated_at)
|
||||
VALUES (?,?,?,?)
|
||||
@@ -308,8 +346,9 @@ def create_app(db_path, img_dir, state_dir=None):
|
||||
now)
|
||||
conn.commit()
|
||||
stored = conn.execute(
|
||||
'SELECT listing_id, action, created_at, updated_at'
|
||||
' FROM swipes WHERE listing_id=?', (listing_id,)).fetchone()
|
||||
'SELECT listing_id, action, created_at, updated_at,'
|
||||
' last_photo_idx FROM swipes WHERE listing_id=?',
|
||||
(listing_id,)).fetchone()
|
||||
result = dict(stored)
|
||||
result['round'] = round_
|
||||
result['section_id'] = section_id
|
||||
@@ -353,7 +392,8 @@ def create_app(db_path, img_dir, state_dir=None):
|
||||
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
||||
' WHERE ' + where, params).fetchone()['n']
|
||||
rows = conn.execute(
|
||||
'SELECT l.*, s.action AS decision FROM listings l'
|
||||
'SELECT l.*, s.action AS decision,'
|
||||
' s.last_photo_idx AS last_photo_idx FROM listings l'
|
||||
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
||||
' WHERE ' + where + ' ORDER BY ' + LIST_SORTS[sort] +
|
||||
' LIMIT ? OFFSET ?', params + [limit, offset]).fetchall()
|
||||
@@ -368,7 +408,8 @@ def create_app(db_path, img_dir, state_dir=None):
|
||||
try:
|
||||
engine = secmod.build_engine(conn, now=utcnow())
|
||||
row = conn.execute(
|
||||
'SELECT l.*, s.action AS decision FROM listings l'
|
||||
'SELECT l.*, s.action AS decision,'
|
||||
' s.last_photo_idx AS last_photo_idx FROM listings l'
|
||||
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
||||
' WHERE l.id=?', (listing_id,)).fetchone()
|
||||
if not row:
|
||||
@@ -625,18 +666,43 @@ def create_app(db_path, img_dir, state_dir=None):
|
||||
# -- images + static -------------------------------------------------
|
||||
|
||||
@app.get('/img/{listing_id}')
|
||||
def img(listing_id: int):
|
||||
fpath = os.path.join(app.state.img_dir, '%d.webp' % listing_id)
|
||||
def img(listing_id: int, i: int = Query(0, ge=0)):
|
||||
"""Photo n of a gallery (B1 §11.1): mirrored file -> CDN 302 -> 404.
|
||||
|
||||
i=0 keeps the legacy chain ({id}.webp, with {id}_0.webp accepted as
|
||||
the gallery-mirror name for the primary); i>0 serves {id}_{n}.webp
|
||||
with the CDN fallback taken from imgs[n].
|
||||
"""
|
||||
fpath = os.path.join(
|
||||
app.state.img_dir,
|
||||
'%d.webp' % listing_id if i == 0 else
|
||||
'%d_%d.webp' % (listing_id, i))
|
||||
if i == 0 and not os.path.exists(fpath):
|
||||
alt = os.path.join(app.state.img_dir, '%d_0.webp' % listing_id)
|
||||
if os.path.exists(alt):
|
||||
fpath = alt
|
||||
if os.path.exists(fpath):
|
||||
return FileResponse(fpath, media_type='image/webp')
|
||||
conn = get_conn(app)
|
||||
try:
|
||||
row = conn.execute('SELECT img_url FROM listings WHERE id=?',
|
||||
row = conn.execute(
|
||||
'SELECT img_url, imgs FROM listings WHERE id=?',
|
||||
(listing_id,)).fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
if row and row['img_url']:
|
||||
return RedirectResponse(url=row['img_url'], status_code=302)
|
||||
url = None
|
||||
if row:
|
||||
if i == 0 and row['img_url']:
|
||||
url = row['img_url']
|
||||
elif row['imgs']:
|
||||
try:
|
||||
arr = json.loads(row['imgs'])
|
||||
except ValueError:
|
||||
arr = []
|
||||
if isinstance(arr, list) and len(arr) > i and arr[i]:
|
||||
url = arr[i]
|
||||
if url:
|
||||
return RedirectResponse(url=url, status_code=302)
|
||||
raise HTTPException(status_code=404, detail='no image')
|
||||
|
||||
@app.get('/')
|
||||
|
||||
@@ -95,6 +95,13 @@ def init_db(conn):
|
||||
conn.execute('ALTER TABLE listings ADD COLUMN seller_type TEXT')
|
||||
if 'extras' not in cols:
|
||||
conn.execute('ALTER TABLE listings ADD COLUMN extras TEXT')
|
||||
# B1 (photo experience v2): gallery URLs + swipe-time photo index.
|
||||
if 'imgs' not in cols:
|
||||
conn.execute('ALTER TABLE listings ADD COLUMN imgs TEXT')
|
||||
sw_cols = {r['name'] for r in conn.execute(
|
||||
"PRAGMA table_info('swipes')")}
|
||||
if 'last_photo_idx' not in sw_cols:
|
||||
conn.execute('ALTER TABLE swipes ADD COLUMN last_photo_idx INTEGER')
|
||||
# P3 tables (idempotent on every call)
|
||||
conn.executescript(P3_SCHEMA)
|
||||
# P3 event seeding: one round-1 event per existing swipe, copied
|
||||
|
||||
519
app/fe_walk_b1.py
Normal file
519
app/fe_walk_b1.py
Normal file
@@ -0,0 +1,519 @@
|
||||
"""B1 FE walk — headless-chromium proof of UI-SPEC §11 on the real page.
|
||||
|
||||
Not part of `app.selfcheck` (needs playwright + a live server); run it after
|
||||
starting the scratch server (the brief's isolation recipe):
|
||||
|
||||
.venv/bin/python -m app.fe_walk_b1 setup # optional — auto at start
|
||||
|
||||
env -u OPENROUTER_API_KEY .venv/bin/python -m app.serve \
|
||||
--db /var/home/admin/dev/bike-tinder-b1/test-walk.sqlite3 \
|
||||
--img-dir /var/home/admin/dev/bike-tinder-b1/test-img \
|
||||
--host 127.0.0.1 --port 9454 # in another shell (mock forge)
|
||||
|
||||
.venv/bin/python -m app.fe_walk_b1 # walk (this file)
|
||||
|
||||
Setup is deterministic and idempotent: a fresh copy of the pristine B1 copy
|
||||
DB (test-copy.sqlite3) each run, synthetic multi-photo listings (ids 701+)
|
||||
with local image files staged into test-img from media-sample/, and one
|
||||
swiped return listing (705) whose stored last_photo_idx drives §11.5
|
||||
rotation. Everything walks the REAL page: real pointers, real DOM, no
|
||||
stubbed APIs.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sqlite3
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
ARENA = '/var/home/admin/dev/bike-tinder-b1'
|
||||
PRISTINE = os.path.join(ARENA, 'test-copy.sqlite3')
|
||||
WALK_DB = os.path.join(ARENA, 'test-walk.sqlite3')
|
||||
IMG_DIR = os.path.join(ARENA, 'test-img')
|
||||
MEDIA = os.path.join(ARENA, 'media-sample')
|
||||
BASE_URL = 'http://127.0.0.1:9454'
|
||||
|
||||
CDN = ('https://media.kijiji.ca/api/v1/ca-prod-dealer-ads/images/x/%s'
|
||||
'?rule=kijijica-640-webp')
|
||||
|
||||
# synthetic fixtures: listing_id -> (n_photos, label)
|
||||
FIX = {
|
||||
701: (8, 'gallery-8'),
|
||||
702: (3, 'gallery-3'),
|
||||
703: (2, 'gallery-2'),
|
||||
704: (1, 'single-photo'),
|
||||
705: (8, 'return-rotation'), # pre-swiped: last_photo_idx=0 -> lead 1
|
||||
706: (0, 'zero-photo'),
|
||||
}
|
||||
SCORE_BASE = 2000
|
||||
|
||||
|
||||
def stage_img_files():
|
||||
"""{id}_0..{id}_{n-1}.webp (and {id}.webp for the legacy single) from
|
||||
media-sample's real webp files — no network needed."""
|
||||
os.makedirs(IMG_DIR, exist_ok=True)
|
||||
names = sorted(m for m in os.listdir(MEDIA) if m.endswith('.webp'))
|
||||
assert len(names) == 12, names
|
||||
k = 0 # cycle through the 12 real files
|
||||
for lid in sorted(FIX):
|
||||
n = FIX[lid][0]
|
||||
if n == 0:
|
||||
continue
|
||||
if n == 1:
|
||||
shutil.copy2(os.path.join(MEDIA, names[k % len(names)]),
|
||||
os.path.join(IMG_DIR, '%d.webp' % lid))
|
||||
k += 1
|
||||
continue
|
||||
for i in range(n):
|
||||
shutil.copy2(os.path.join(MEDIA, names[k % len(names)]),
|
||||
os.path.join(IMG_DIR, '%d_%d.webp' % (lid, i)))
|
||||
k += 1
|
||||
|
||||
|
||||
def setup_db():
|
||||
shutil.copy2(PRISTINE, WALK_DB)
|
||||
from app import db as dbmod
|
||||
conn = dbmod.connect(WALK_DB)
|
||||
dbmod.init_db(conn) # B1 migrations
|
||||
for lid in sorted(FIX):
|
||||
n = FIX[lid][0]
|
||||
imgs = [CDN % ('x%d-%d' % (lid, i)) for i in range(n)]
|
||||
img = imgs[0] if imgs else None
|
||||
score = SCORE_BASE + (750 - lid) # 701 highest -> top of the deck
|
||||
conn.execute(
|
||||
"DELETE FROM listings WHERE id=?", (lid,))
|
||||
conn.execute(
|
||||
"INSERT INTO listings (id, region, src_id, title, url, status,"
|
||||
" img_url, imgs, score, first_seen, desc) VALUES"
|
||||
" (?, 'ns', ?, ?, ?, 'active', ?, ?, ?,"
|
||||
" '2026-11-11T00:00:00Z', ?)",
|
||||
(lid, 'b1fix%d' % lid, 'B1 test %s' % FIX[lid][1],
|
||||
'https://www.kijiji.ca/v-motorcycles/b1/%d' % lid,
|
||||
img, json.dumps(imgs) if imgs else None, score, 'B1 fixture'))
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO swipes (listing_id, action, created_at,"
|
||||
" last_photo_idx) VALUES (705, 'like', '2026-11-08T00:00:00Z', 0)")
|
||||
# Close live-copied sections: the first /api/section read then composes
|
||||
# a fresh 25 on the walk DB (deterministic fixture ordering).
|
||||
conn.execute(
|
||||
"UPDATE sections SET status='closed' WHERE status != 'closed'")
|
||||
# A round-1 event per fixture keeps synthetics out of the wildcard
|
||||
# pool (wildcards must be unseen); new-pool eligibility is unaffected.
|
||||
# 705's event matches its stored verdict (round 1, like).
|
||||
for lid in FIX:
|
||||
conn.execute(
|
||||
"INSERT INTO swipe_events (listing_id, action, round,"
|
||||
" section_id) VALUES (?, ?, 1, NULL)",
|
||||
(lid, 'like' if lid == 705 else 'maybe'))
|
||||
conn.commit()
|
||||
n = conn.execute('SELECT COUNT(*) AS n FROM listings').fetchone()['n']
|
||||
conn.close()
|
||||
print('setup: walk DB at %s (%s listings; 701-706 synthetic, 705 = liked'
|
||||
' return w/ last_photo_idx=0)' % (WALK_DB, n))
|
||||
|
||||
|
||||
def need_server():
|
||||
try:
|
||||
with urllib.request.urlopen(BASE_URL + '/api/health',
|
||||
timeout=3) as r:
|
||||
return r.status == 200
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
# --- walk helpers (module-level `page` assigned inside walk) ----------------
|
||||
|
||||
def wait(selector, timeout=8000):
|
||||
page.wait_for_selector(selector, timeout=timeout)
|
||||
|
||||
|
||||
def card_box():
|
||||
b = page.evaluate("""() => { const el=document.querySelector('.topcard');
|
||||
if(!el) return null; const r=el.getBoundingClientRect();
|
||||
return {x:r.x,y:r.y,w:r.width,h:r.height}; }""")
|
||||
assert b, 'no .topcard in DOM'
|
||||
return b
|
||||
|
||||
|
||||
def top_state():
|
||||
return page.evaluate("""() => {
|
||||
const t=document.querySelector('.topcard');
|
||||
const r={topcards:document.querySelectorAll('.topcard').length,
|
||||
hasSegs:!!t.classList.contains('hasSegs'), segs:0, segOn:-1,
|
||||
photo:null, nophoto:false, band:null,
|
||||
stampLike:0, stampNope:0, stampMaybe:0};
|
||||
const s=t.querySelector('.segments');
|
||||
if(s){ r.segs=s.children.length;
|
||||
[...s.children].forEach((x,k)=>{ if(x.classList.contains('on'))
|
||||
r.segOn=k; }); }
|
||||
const p=t.querySelector('.phwrap img.photo');
|
||||
if(p) r.photo=p.getAttribute('src');
|
||||
else if(t.querySelector('.nophoto')) r.nophoto=true;
|
||||
const b=t.querySelector('.band'); if(b) r.band=b.textContent;
|
||||
const sl=t.querySelector('.stamp.like');
|
||||
if(sl) r.stampLike=sl.style.opacity;
|
||||
const sn=t.querySelector('.stamp.nope');
|
||||
if(sn) r.stampNope=sn.style.opacity;
|
||||
const sm=t.querySelector('.stamp.maybe');
|
||||
if(sm) r.stampMaybe=sm.style.opacity;
|
||||
return r; }""")
|
||||
|
||||
|
||||
def tap_third(which):
|
||||
b = card_box()
|
||||
y = b['y'] + b['h'] * 0.72
|
||||
x = b['x'] + b['w'] * (0.16 if which == 'left'
|
||||
else (0.84 if which == 'right' else 0.5))
|
||||
page.mouse.click(x, y)
|
||||
time.sleep(0.08)
|
||||
|
||||
|
||||
def assert_item(cond, msg):
|
||||
if not cond:
|
||||
print('FAIL ' + msg)
|
||||
sys.exit(1)
|
||||
print('ok ' + msg)
|
||||
|
||||
|
||||
def lead_of(item):
|
||||
n = item.get('n_photos') or 0
|
||||
li = item.get('last_photo_idx')
|
||||
if not n or n < 2 or li is None:
|
||||
return 0
|
||||
return (li + 1) % n
|
||||
|
||||
|
||||
def photo_url(item, i):
|
||||
if not item.get('img'):
|
||||
return None
|
||||
return item['img'] if i == 0 else '/img/%s?i=%s' % (item['id'], i)
|
||||
|
||||
|
||||
def viewer_on():
|
||||
return page.evaluate(
|
||||
"() => document.getElementById('viewer').classList.contains('on')")
|
||||
|
||||
|
||||
def vimg_src():
|
||||
return page.evaluate(
|
||||
"() => document.getElementById('vimg').getAttribute('src')")
|
||||
|
||||
|
||||
def vimg_transform():
|
||||
return page.evaluate(
|
||||
"() => document.getElementById('vimg').style.transform")
|
||||
|
||||
|
||||
# --- the walk ---------------------------------------------------------------
|
||||
|
||||
def walk():
|
||||
from playwright.sync_api import sync_playwright
|
||||
with sync_playwright() as pw:
|
||||
exe = os.path.expanduser(
|
||||
'~/.cache/ms-playwright/chromium-1243/chrome-linux64/chrome')
|
||||
if not os.path.exists(exe):
|
||||
exe = None
|
||||
browser = pw.chromium.launch(headless=True, executable_path=exe,
|
||||
args=['--no-sandbox'])
|
||||
ctx = browser.new_context(viewport={'width': 390, 'height': 844},
|
||||
has_touch=True, is_mobile=True)
|
||||
global page
|
||||
page = ctx.new_page()
|
||||
page.goto(BASE_URL + '/', wait_until='networkidle')
|
||||
wait('.topcard')
|
||||
time.sleep(0.6)
|
||||
|
||||
section = None
|
||||
for _ in range(40):
|
||||
body = page.evaluate(
|
||||
"() => fetch('/api/section').then(r=>r.json())")
|
||||
if body and body.get('items'):
|
||||
section = body
|
||||
break
|
||||
time.sleep(0.5)
|
||||
assert section, 'no section payload'
|
||||
items = list(section['items'])
|
||||
print('section: %d items — recipe %s' % (len(items), json.dumps(
|
||||
section['section']['recipe'], sort_keys=True)))
|
||||
by_id = {i['id']: i for i in items}
|
||||
for lid in (701, 702, 703, 704, 706):
|
||||
assert lid in by_id, 'fixture %s missing from section' % lid
|
||||
assert by_id[lid]['slot'] == 'new', (lid, by_id[lid]['slot'])
|
||||
assert by_id[705]['slot'] == 'return', by_id[705]['slot']
|
||||
|
||||
seen = []
|
||||
round_no = 0
|
||||
while True:
|
||||
remaining = [i['id'] for i in items if i['id'] not in seen]
|
||||
if not remaining:
|
||||
break
|
||||
t = top_state()
|
||||
top_id = None
|
||||
m = re.search(r'/img/(\d+)(?:\?i=\d+)?$', t['photo'] or '')
|
||||
if m:
|
||||
top_id = int(m.group(1))
|
||||
else:
|
||||
for i in items: # zero-photo card: no src to match
|
||||
if i['id'] not in seen and not i.get('img'):
|
||||
top_id = i['id']
|
||||
break
|
||||
assert top_id in by_id, 'cannot identify top card'
|
||||
item = by_id[top_id]
|
||||
seen.append(top_id)
|
||||
round_no += 1
|
||||
n = item.get('n_photos') or 0
|
||||
exp_lead = lead_of(item)
|
||||
exp_src = photo_url(item, exp_lead)
|
||||
print('-- top id=%s [%s] n=%d lead=%d slot=%s --'
|
||||
% (top_id, item['title'][:26], n, exp_lead, item['slot']))
|
||||
assert_item(t['topcards'] == 1,
|
||||
'exactly one .topcard before gestures')
|
||||
if n >= 2:
|
||||
assert_item(t['hasSegs'] and t['segs'] == n,
|
||||
'segments count == n (%d)' % n)
|
||||
assert_item(t['segOn'] == exp_lead,
|
||||
'segment idx == rotation lead (%d)' % exp_lead)
|
||||
assert_item(t['photo'] == exp_src,
|
||||
'photo src == expected lead url')
|
||||
else:
|
||||
assert_item(not t['hasSegs'] and t['segs'] == 0,
|
||||
'no segments for n=%d' % n)
|
||||
if n == 1:
|
||||
assert_item(t['photo'] == photo_url(item, 0),
|
||||
'single photo shows the primary')
|
||||
else:
|
||||
assert_item(t['nophoto'],
|
||||
'zero-photo card shows the placeholder')
|
||||
if item['slot'] == 'return' and item['id'] == 705:
|
||||
assert_item(exp_lead == 1, 'rotation lead: (0+1)%%8 = 1')
|
||||
assert_item(t['band'] and 'SECOND LOOK' in t['band'],
|
||||
'return card carries the SECOND LOOK band '
|
||||
'(with segments)')
|
||||
|
||||
if n >= 2:
|
||||
# --- flip: advance + clamp both ends (no wrap) ----------
|
||||
for i in range(n + 2):
|
||||
tap_third('right')
|
||||
st = top_state()
|
||||
assert_item(st['topcards'] == 1,
|
||||
'tap kept the one-.topcard invariant')
|
||||
want = min(exp_lead + i + 1, n - 1)
|
||||
assert_item(st['segOn'] == want,
|
||||
'right-tap %d -> idx %d (clamp)' % (i + 1,
|
||||
want))
|
||||
for i in range(n + 2):
|
||||
tap_third('left')
|
||||
st = top_state()
|
||||
assert_item(st['topcards'] == 1,
|
||||
'tap kept the one-.topcard invariant')
|
||||
want = max(n - 1 - (i + 1), 0)
|
||||
assert_item(st['segOn'] == want,
|
||||
'left-tap %d -> idx %d (clamp)' % (i + 1,
|
||||
want))
|
||||
st = top_state()
|
||||
assert_item(st['topcards'] == 1
|
||||
and st['stampLike'] in ('0', '')
|
||||
and st['stampNope'] in ('0', '')
|
||||
and st['stampMaybe'] in ('0', ''),
|
||||
'taps never commit: no stamps, no flight')
|
||||
# --- viewer: open on current photo, nav + clamp, zoom,
|
||||
# pull-close restores the same card+photo ------------
|
||||
want_idx = 1
|
||||
while st['segOn'] > want_idx:
|
||||
tap_third('left')
|
||||
st = top_state()
|
||||
if st['segOn'] < want_idx:
|
||||
tap_third('right')
|
||||
st = top_state()
|
||||
before_src = st['photo']
|
||||
tap_third('center')
|
||||
assert_item(viewer_on(), 'center tap opens the viewer')
|
||||
vsegs = page.evaluate(
|
||||
"() => document.getElementById('vsegs')"
|
||||
".querySelectorAll('i').length")
|
||||
assert_item(vsegs == n, 'viewer segments == n (%d)' % n)
|
||||
assert_item(vimg_src() == before_src,
|
||||
'viewer opens on the current photo')
|
||||
vp = page.locator('#vphoto')
|
||||
vb = vp.bounding_box()
|
||||
al, ar = (vb['x'] + vb['width'] * 0.75,
|
||||
vb['x'] + vb['width'] * 0.15)
|
||||
ay = vb['y'] + vb['height'] * 0.5
|
||||
page.mouse.move(al, ay)
|
||||
page.mouse.down()
|
||||
page.mouse.move(ar, ay, steps=6)
|
||||
page.mouse.up()
|
||||
time.sleep(0.1)
|
||||
land = min(want_idx + 1, n - 1)
|
||||
assert_item(vimg_src() == photo_url(item, land),
|
||||
'viewer swipe-left advances + clamps')
|
||||
page.mouse.move(ar, ay)
|
||||
page.mouse.down()
|
||||
page.mouse.move(al, ay, steps=6)
|
||||
page.mouse.up()
|
||||
time.sleep(0.1)
|
||||
back = max(land - 1, 0)
|
||||
assert_item(vimg_src() == photo_url(item, back),
|
||||
'viewer swipe-right steps back (clamped)')
|
||||
page.mouse.dblclick(vb['x'] + vb['width'] * 0.5,
|
||||
vb['y'] + vb['height'] * 0.5)
|
||||
time.sleep(0.1)
|
||||
assert_item('scale(2.5)' in vimg_transform(),
|
||||
'double-tap zooms to 2.5x')
|
||||
page.mouse.dblclick(vb['x'] + vb['width'] * 0.5,
|
||||
vb['y'] + vb['height'] * 0.5)
|
||||
time.sleep(0.1)
|
||||
assert_item('scale(2.5)' not in vimg_transform(),
|
||||
'double-tap again returns to 1x')
|
||||
# pinch via synthetic pointer events (real handler path)
|
||||
page.evaluate("""() => {
|
||||
const v=document.getElementById('vphoto');
|
||||
const r=v.getBoundingClientRect();
|
||||
const mk=(t,id,x,y)=>{ v.dispatchEvent(new PointerEvent(t,
|
||||
{pointerId:id, clientX:x, clientY:y, bubbles:true})); };
|
||||
const x=r.width/2, y=r.height/2;
|
||||
mk('pointerdown',10,x-25,y); mk('pointerdown',11,x+25,y);
|
||||
for(let s=0;s<6;s++){ mk('pointermove',10,x-25-s*22,y);
|
||||
mk('pointermove',11,x+25+s*22,y); }
|
||||
mk('pointerup',10,x-25-6*22,y);
|
||||
mk('pointerup',11,x+25+6*22,y);
|
||||
}""")
|
||||
assert_item('scale(4)' in vimg_transform(),
|
||||
'pinch-zoom clamps at 4x')
|
||||
page.mouse.dblclick(vb['x'] + vb['width'] * 0.5,
|
||||
vb['y'] + vb['height'] * 0.5)
|
||||
time.sleep(0.1) # back to 1x for the pull gesture
|
||||
page.mouse.move(vb['x'] + vb['width'] * 0.5,
|
||||
vb['y'] + vb['height'] * 0.35)
|
||||
page.mouse.down()
|
||||
page.mouse.move(vb['x'] + vb['width'] * 0.5,
|
||||
vb['y'] + vb['height'] * 0.35 + 170, steps=8)
|
||||
page.mouse.up()
|
||||
time.sleep(0.2)
|
||||
assert_item(not viewer_on(),
|
||||
'pull-down (>110px) closes the viewer')
|
||||
st = top_state()
|
||||
assert_item(st['segOn'] == want_idx
|
||||
and st['photo'] == before_src,
|
||||
'close returns the same card + photo position')
|
||||
else:
|
||||
if n == 1:
|
||||
tap_third('center')
|
||||
assert_item(viewer_on(),
|
||||
'center tap opens the viewer (single photo)')
|
||||
page.locator('#vclose').click()
|
||||
time.sleep(0.15)
|
||||
if n == 0:
|
||||
tap_third('center')
|
||||
assert_item(not viewer_on(), 'zero-photo: no viewer')
|
||||
|
||||
# --- ⓘ beats the zones --------------------------------------
|
||||
page.locator('.topcard .ibtn').click()
|
||||
time.sleep(0.15)
|
||||
assert_item(page.evaluate(
|
||||
"() => document.getElementById('detail')"
|
||||
".classList.contains('on')"),
|
||||
'ⓘ opens the dossier (zones do not fire)')
|
||||
pre = top_state()
|
||||
page.locator('#dclose').click()
|
||||
time.sleep(0.2)
|
||||
post = top_state()
|
||||
assert_item(pre['photo'] == post['photo']
|
||||
and pre['segOn'] == post['segOn'],
|
||||
'dossier close leaves the photo position intact')
|
||||
if n >= 1:
|
||||
# dossier hero -> viewer at photo 0 (dossier stays open)
|
||||
page.locator('.topcard .ibtn').click()
|
||||
time.sleep(0.15)
|
||||
hb = page.locator('.dhero').bounding_box()
|
||||
page.mouse.click(hb['x'] + hb['width'] / 2,
|
||||
hb['y'] + hb['height'] / 2)
|
||||
time.sleep(0.15)
|
||||
assert_item(viewer_on() and page.evaluate(
|
||||
"() => document.getElementById('detail')"
|
||||
".classList.contains('on')"),
|
||||
'dossier hero tap opens the viewer over the dossier')
|
||||
assert_item(vimg_src() == photo_url(item, 0),
|
||||
'dossier viewer starts at photo 0')
|
||||
page.locator('#vclose').click()
|
||||
time.sleep(0.15)
|
||||
assert_item(not viewer_on() and page.evaluate(
|
||||
"() => document.getElementById('detail')"
|
||||
".classList.contains('on')"),
|
||||
'viewer ✕ returns to the still-open dossier')
|
||||
page.locator('#dclose').click()
|
||||
time.sleep(0.15)
|
||||
|
||||
# single commit per round (fast chain runs after the report)
|
||||
page.locator('#btnNope').click()
|
||||
time.sleep(0.07)
|
||||
|
||||
# --- P3 regression: report -> undo x3 -> fast chain -> forge -----
|
||||
wait('#report.on', 15000)
|
||||
print('report visible: %s' % page.locator('#repCounts').inner_text())
|
||||
assert_item('Section complete' in page.locator('#report').inner_text(),
|
||||
'report screen after the full section')
|
||||
for _ in range(4):
|
||||
page.locator('#btnUndo').click()
|
||||
time.sleep(0.08)
|
||||
assert_item(page.evaluate(
|
||||
"() => document.querySelectorAll('.topcard').length") == 1,
|
||||
'undo x4 from the report re-opens the deck (one .topcard)')
|
||||
for k in range(3):
|
||||
b = card_box()
|
||||
y = b['y'] + b['h'] * 0.6
|
||||
page.mouse.move(b['x'] + b['w'] * 0.5, y)
|
||||
page.mouse.down()
|
||||
page.mouse.move(b['x'] + b['w'] * 0.5 + 320, y, steps=5)
|
||||
page.mouse.up()
|
||||
if k < 2:
|
||||
time.sleep(0.07)
|
||||
tc = page.evaluate(
|
||||
"() => document.querySelectorAll('.topcard').length")
|
||||
assert tc == 1, 'fast chain: topcard=%s (must be 1)' % tc
|
||||
assert_item(True, '3 like-commits @70ms: one .topcard, no lockout')
|
||||
page.locator('#btnNope').click() # finish the 4th restored card
|
||||
time.sleep(0.1)
|
||||
wait('#report.on', 15000)
|
||||
page.locator('#btnForge').click()
|
||||
wait('.topcard', 15000)
|
||||
time.sleep(0.4)
|
||||
sect = page.evaluate(
|
||||
"() => document.getElementById('sect').textContent.trim()")
|
||||
assert_item(re.match(r'^\d+/25$', sect) is not None,
|
||||
'forge hydrates a fresh section (progress=%s)' % sect)
|
||||
# --- List + Trends tabs render with P3 elements intact ----------
|
||||
page.locator('.tab[data-tab="list"]').click()
|
||||
wait('.row')
|
||||
tanks = page.locator('.row .tank').count()
|
||||
rows = page.locator('.row').count()
|
||||
assert_item(tanks > 0 and rows > 0,
|
||||
'list: %d rows, %d tank gauges (P3 intact)'
|
||||
% (rows, tanks))
|
||||
page.locator('.tab[data-tab="trends"]').click()
|
||||
wait('#tLead')
|
||||
assert_item((page.locator('#tLead').text_content() or '').strip()
|
||||
!= '', 'trends tab renders')
|
||||
print('WALK COMPLETE — all B1 FE checks green')
|
||||
browser.close()
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
if 'setup' in sys.argv or not os.path.exists(WALK_DB):
|
||||
if not os.path.exists(PRISTINE):
|
||||
print('pristine copy DB missing: %s' % PRISTINE)
|
||||
return 1
|
||||
setup_db()
|
||||
stage_img_files()
|
||||
print('fixtures staged in %s' % IMG_DIR)
|
||||
if not need_server():
|
||||
print('NO SERVER on %s — start it (see module docstring).' % BASE_URL)
|
||||
return 1
|
||||
walk()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -1,18 +1,24 @@
|
||||
"""Bike-Tinder local image mirror.
|
||||
|
||||
Downloads each active listing's card image once into data/img/{id}.webp.
|
||||
Downloads each active listing's photos once:
|
||||
- gallery listings (B1 §11.1): imgs[n] -> data/img/{id}_{n}.webp for the
|
||||
extra photos; the primary (n=0) lands at the legacy data/img/{id}.webp
|
||||
so the v1 single-image chain is untouched;
|
||||
- legacy listings: img_url -> data/img/{id}.webp (as before).
|
||||
|
||||
.venv/bin/python -m app.imgfetch --db data/bike-tinder.sqlite3
|
||||
.venv/bin/python -m app.imgfetch --limit 15
|
||||
|
||||
Polite by design: ~1 req/s (sleep between fetches), skip-existing, crash-safe
|
||||
(partial bytes land in .part and are atomically renamed), per-item failures
|
||||
never kill the run. Honest end summary: `fetched N · skipped M · failed K`.
|
||||
never kill the run. Per-file CLI progress; honest end summary:
|
||||
`fetched N · skipped M · failed K`.
|
||||
|
||||
UA + habits match the hunt scanner (reference/motorcycle-hunt-scan.py) so the
|
||||
CDN sees the same client as the crawl. Stdlib only — no requests dependency.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
@@ -44,8 +50,10 @@ def fetch_one(img_url, dst):
|
||||
})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=TIMEOUT) as resp:
|
||||
if getattr(resp, 'status', 200) != 200:
|
||||
return 'HTTP %s' % resp.status
|
||||
status = (getattr(resp, 'status', None)
|
||||
or getattr(resp, 'code', None) or 200)
|
||||
if status != 200:
|
||||
return 'HTTP %s' % status
|
||||
buf = resp.read()
|
||||
if not is_webp(buf):
|
||||
return 'not webp (%d bytes, %r)' % (len(buf), buf[:8])
|
||||
@@ -65,33 +73,76 @@ def fetch_one(img_url, dst):
|
||||
pass
|
||||
|
||||
|
||||
def parse_imgs(raw):
|
||||
"""listings.imgs TEXT -> list of non-empty URL strings ([] when unparsable)."""
|
||||
if not raw:
|
||||
return []
|
||||
try:
|
||||
v = json.loads(raw)
|
||||
except ValueError:
|
||||
return []
|
||||
if not isinstance(v, list):
|
||||
return []
|
||||
return [u.strip() for u in v if isinstance(u, str) and u.strip()]
|
||||
|
||||
|
||||
def build_work(rows, img_dir):
|
||||
"""Flatten listings rows into (listing_id, n_or_None, url, dst) downloads.
|
||||
|
||||
Gallery rows fetch every imgs[n]: n=0 -> the legacy {id}.webp (the primary
|
||||
— one canonical file, no duplicate fetch), n>=1 -> {id}_{n}.webp. Legacy
|
||||
rows (no gallery) keep the img_url -> {id}.webp chain. Work is deduped by
|
||||
destination path.
|
||||
"""
|
||||
work = []
|
||||
for r in rows:
|
||||
g = parse_imgs(r['imgs'])
|
||||
if g:
|
||||
for n, url in enumerate(g):
|
||||
dst = os.path.join(img_dir, ('%d.webp' % r['id'] if n == 0
|
||||
else '%d_%d.webp' % (r['id'], n)))
|
||||
work.append((r['id'], n, url, dst))
|
||||
elif r['img_url']:
|
||||
work.append((r['id'], None, r['img_url'],
|
||||
os.path.join(img_dir, '%d.webp' % r['id'])))
|
||||
seen, deduped = set(), []
|
||||
for item in work:
|
||||
if item[3] not in seen:
|
||||
seen.add(item[3])
|
||||
deduped.append(item)
|
||||
return deduped
|
||||
|
||||
|
||||
def run_fetch(db_path, img_dir, limit=None, sleep=SLEEP, quiet=False):
|
||||
conn = db.connect(db_path)
|
||||
db.init_db(conn)
|
||||
rows = conn.execute(
|
||||
"SELECT id, img_url FROM listings"
|
||||
" WHERE status='active' AND img_url IS NOT NULL"
|
||||
" ORDER BY id").fetchall()
|
||||
"SELECT id, img_url, imgs FROM listings"
|
||||
" WHERE status='active' ORDER BY id").fetchall()
|
||||
conn.close()
|
||||
|
||||
os.makedirs(img_dir, exist_ok=True)
|
||||
work = build_work(rows, img_dir)
|
||||
total = len(work)
|
||||
fetched = skipped = failed = 0
|
||||
for row in rows:
|
||||
for idx, (lid, n, url, dst) in enumerate(work, 1):
|
||||
if limit is not None and fetched >= limit:
|
||||
break
|
||||
dst = os.path.join(img_dir, '%d.webp' % row['id'])
|
||||
if os.path.exists(dst):
|
||||
skipped += 1
|
||||
continue
|
||||
result = fetch_one(row['img_url'], dst)
|
||||
result = fetch_one(url, dst)
|
||||
if result == 'fetched':
|
||||
fetched += 1
|
||||
if not quiet:
|
||||
print(' [%d/%d] id=%d n=%s ok' % (idx, total, lid, n))
|
||||
else:
|
||||
failed += 1
|
||||
if not quiet:
|
||||
print(' fail id=%d %s' % (row['id'], result))
|
||||
print(' fail id=%d n=%s %s' % (lid, n, result))
|
||||
time.sleep(sleep)
|
||||
return {'fetched': fetched, 'skipped': skipped, 'failed': failed}
|
||||
return {'fetched': fetched, 'skipped': skipped, 'failed': failed,
|
||||
'total': total}
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
@@ -105,8 +156,8 @@ def main(argv=None):
|
||||
args = ap.parse_args(argv)
|
||||
|
||||
res = run_fetch(args.db, args.img_dir, limit=args.limit, sleep=args.sleep)
|
||||
print('fetched %d · skipped %d · failed %d' % (
|
||||
res['fetched'], res['skipped'], res['failed']))
|
||||
print('fetched %d · skipped %d · failed %d · total %d' % (
|
||||
res['fetched'], res['skipped'], res['failed'], res['total']))
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
@@ -88,6 +88,24 @@ def iter_items(st):
|
||||
yield src_id, it
|
||||
|
||||
|
||||
def imgs_from_item(it):
|
||||
"""Gallery images from a state item: deduplicated, non-empty URL strings
|
||||
in page order, or None. The hunt guarantees imgs[0] equals `img` (the
|
||||
established primary)."""
|
||||
raw = it.get('imgs')
|
||||
if not isinstance(raw, (list, tuple)):
|
||||
return None
|
||||
out, seen = [], set()
|
||||
for u in raw:
|
||||
if not isinstance(u, str):
|
||||
continue
|
||||
u = u.strip()
|
||||
if u and u not in seen:
|
||||
seen.add(u)
|
||||
out.append(u)
|
||||
return out or None
|
||||
|
||||
|
||||
def normalize(region, src_id, it, now):
|
||||
"""Normalize one state item into a row dict (strings->ints robustly,
|
||||
missing fields -> NULL)."""
|
||||
@@ -102,6 +120,10 @@ def normalize(region, src_id, it, now):
|
||||
year = rubric.year_from_title(title)
|
||||
score, family = rubric.score_item(title, it.get('desc') or '', price_num, km, year)
|
||||
extras = rubric.extras_from_text(title, it.get('desc') or '')
|
||||
# B1: gallery (page order, entry 0 = the primary). img_url keeps the
|
||||
# established single `img` signal and falls back to imgs[0].
|
||||
imgs = imgs_from_item(it)
|
||||
img = it.get('img') or (imgs[0] if imgs else None)
|
||||
# seller_type only from a REAL signal in the state item (the hunt image
|
||||
# backfill / enrichment passes) — never inferred from titles.
|
||||
seller = it.get('seller_type') or it.get('seller')
|
||||
@@ -116,7 +138,8 @@ def normalize(region, src_id, it, now):
|
||||
'km': km,
|
||||
'location': (it.get('loc') or it.get('city') or None),
|
||||
'url': it.get('url') or '',
|
||||
'img_url': it.get('img') or None,
|
||||
'img_url': img,
|
||||
'imgs': json.dumps(imgs) if imgs else None,
|
||||
'desc': it.get('desc') or None,
|
||||
'family': family,
|
||||
'score': score,
|
||||
@@ -138,25 +161,25 @@ def upsert(conn, row, now):
|
||||
if existing:
|
||||
conn.execute(
|
||||
'''UPDATE listings SET title=?, price_display=?, price_num=?, km=?,
|
||||
location=?, url=?, img_url=?, desc=?, family=?, score=?,
|
||||
location=?, url=?, img_url=?, imgs=?, desc=?, family=?, score=?,
|
||||
year=?, seller_type=?, extras=?,
|
||||
status='active', dup_of=NULL, last_seen=?, updated_at=?
|
||||
WHERE id=?''',
|
||||
(row['title'], row['price_display'], row['price_num'], row['km'],
|
||||
row['location'], row['url'], row['img_url'], row['desc'],
|
||||
row['family'], row['score'], row['year'], row['seller_type'],
|
||||
row['extras'], now, now, existing['id']))
|
||||
row['location'], row['url'], row['img_url'], row['imgs'],
|
||||
row['desc'], row['family'], row['score'], row['year'],
|
||||
row['seller_type'], row['extras'], now, now, existing['id']))
|
||||
return existing['id'], False
|
||||
cur = conn.execute(
|
||||
'''INSERT INTO listings (region, src_id, title, price_display, price_num,
|
||||
km, location, url, img_url, desc, family, score, year, seller_type,
|
||||
extras, status, dup_of,
|
||||
km, location, url, img_url, imgs, desc, family, score, year,
|
||||
seller_type, extras, status, dup_of,
|
||||
first_seen, last_seen, created_at, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
|
||||
(row['region'], row['src_id'], row['title'], row['price_display'],
|
||||
row['price_num'], row['km'], row['location'], row['url'],
|
||||
row['img_url'], row['desc'], row['family'], row['score'], row['year'],
|
||||
row['seller_type'], row['extras'], 'active',
|
||||
row['img_url'], row['imgs'], row['desc'], row['family'], row['score'],
|
||||
row['year'], row['seller_type'], row['extras'], 'active',
|
||||
None, row['first_seen'], row['last_seen'], now, now))
|
||||
return cur.lastrowid, True
|
||||
|
||||
|
||||
@@ -416,7 +416,8 @@ def close_section(conn, section, now, status='closed',
|
||||
|
||||
def section_items(conn, section):
|
||||
return conn.execute(
|
||||
"""SELECT l.*, s.action AS decision, si.slot, si.position
|
||||
"""SELECT l.*, s.action AS decision, s.last_photo_idx AS last_photo_idx,
|
||||
si.slot, si.position
|
||||
FROM section_items si
|
||||
JOIN listings l ON l.id = si.listing_id
|
||||
LEFT JOIN swipes s ON s.listing_id = l.id
|
||||
|
||||
390
app/selfcheck.py
390
app/selfcheck.py
@@ -1,9 +1,12 @@
|
||||
"""Dependency-free self-check for the Phase 1 data spine.
|
||||
"""Dependency-free self-check for the Bike-Tinder data spine.
|
||||
|
||||
python3 -m app.selfcheck
|
||||
|
||||
Covers: gate rejections, dedupe (incl. false-positive sanity), scoring
|
||||
spot-checks against the reference rubric, and gone-after-2-misses.
|
||||
spot-checks against the reference rubric, gone-after-2-misses, the P2/P3
|
||||
API surface (on temp copies of the DB only — the live file is never opened
|
||||
for writing), and the B1 photo-experience checks (gallery import,
|
||||
/img?i= chain, photo_idx persistence, rotation math, 640-row stability).
|
||||
"""
|
||||
import datetime
|
||||
import json
|
||||
@@ -308,12 +311,23 @@ TESTS = [
|
||||
|
||||
PAYLOAD_KEYS = {'id', 'title', 'price_display', 'price_num', 'km', 'location',
|
||||
'region', 'url', 'img', 'desc', 'family', 'score', 'year',
|
||||
'seller_type', 'extras', 'decision', 'first_seen'}
|
||||
'seller_type', 'extras', 'decision', 'first_seen',
|
||||
'n_photos', 'last_photo_idx'} # B1 payload additions
|
||||
# P3 additive payload fields (UI-SPEC §10.7)
|
||||
P3_PAYLOAD_KEYS = {'points', 'points_breakdown', 'tank', 'slot', 'round_next'}
|
||||
|
||||
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
LIVE_DB = os.path.join(REPO_ROOT, 'data', 'bike-tinder.sqlite3')
|
||||
LIVE_DB = os.environ.get('BT_TEST_DB') or os.path.join(
|
||||
REPO_ROOT, 'data', 'bike-tinder.sqlite3')
|
||||
if not os.path.exists(LIVE_DB):
|
||||
# B1 test-arena copy (the brief's "copy DB"): pristine snapshot of the
|
||||
# live DB, never written by selfcheck (tests copy it further).
|
||||
_b1_copy = '/var/home/admin/dev/bike-tinder-b1/test-copy.sqlite3'
|
||||
if os.path.exists(_b1_copy):
|
||||
LIVE_DB = _b1_copy
|
||||
B1_STATE_CANDIDATES = ('/var/home/admin/dev/bike-tinder-b1',
|
||||
os.path.expanduser(
|
||||
'~/.hermes/scripts/motorcycle-hunt.d'))
|
||||
|
||||
# --- P2 API checks (need the venv: fastapi + httpx). Run against a TEMP COPY
|
||||
# --- of the live DB — the live file is never opened by these tests. ---------
|
||||
@@ -552,11 +566,19 @@ def test_p3_event_seeding_idempotent():
|
||||
try:
|
||||
conn = _sq_conn(db_path)
|
||||
n_swipes = conn.execute('SELECT COUNT(*) AS n FROM swipes').fetchone()['n']
|
||||
tables = {r[0] for r in conn.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table'")}
|
||||
n_events_before = 0
|
||||
if 'swipe_events' in tables:
|
||||
n_events_before = conn.execute(
|
||||
'SELECT COUNT(*) AS n FROM swipe_events').fetchone()['n']
|
||||
conn.close()
|
||||
conn = db.connect(db_path)
|
||||
db.init_db(conn)
|
||||
n1 = conn.execute(
|
||||
'SELECT COUNT(*) AS n FROM swipe_events').fetchone()['n']
|
||||
if n_events_before == 0:
|
||||
# fresh pre-P3 DB: seeded exactly once from swipes rows
|
||||
assert n1 == n_swipes, (n1, n_swipes)
|
||||
seeded = conn.execute(
|
||||
'SELECT listing_id, round, created_at FROM swipe_events LIMIT 1'
|
||||
@@ -566,6 +588,10 @@ def test_p3_event_seeding_idempotent():
|
||||
'SELECT created_at FROM swipes WHERE listing_id=?',
|
||||
(seeded['listing_id'],)).fetchone()
|
||||
assert seeded['created_at'] == match['created_at']
|
||||
else:
|
||||
# migrated DB that already carries events (cleared swipes keep
|
||||
# their history) — seeding must not fire, events stay as found
|
||||
assert n1 == n_events_before, (n1, n_events_before)
|
||||
# second init does not reseed/duplicate
|
||||
db.init_db(conn)
|
||||
assert conn.execute(
|
||||
@@ -803,7 +829,10 @@ def test_p3_agentpass_mock_and_fallback():
|
||||
p2 = ag.synthesize_payload(db.connect(db_path), now=now)
|
||||
assert p1 == p2, 'mock payload must be deterministic'
|
||||
ag.validate_payload(p1)
|
||||
# fallback: live attempt fails -> deterministic fallback profile
|
||||
# fallback: live attempt fails -> deterministic fallback profile.
|
||||
# (The copy may carry profiles from earlier live runs — scope the
|
||||
# assertion to the versions created by THIS test.)
|
||||
new_min = res['profile_version']
|
||||
os.environ['OPENROUTER_API_KEY'] = 'test-key-never-used'
|
||||
orig = ag._live_call
|
||||
ag._live_call = lambda model, summary: (None, None, 'boom', None)
|
||||
@@ -817,7 +846,8 @@ def test_p3_agentpass_mock_and_fallback():
|
||||
ag.validate_payload(res3['payload'])
|
||||
conn = _sq_conn(db_path)
|
||||
sources = [r['source'] for r in conn.execute(
|
||||
'SELECT source FROM taste_profile ORDER BY version')]
|
||||
'SELECT source FROM taste_profile WHERE version >= ?'
|
||||
' ORDER BY version', (new_min,))]
|
||||
assert sources == ['mock', 'fallback'], sources
|
||||
conn.close()
|
||||
finally:
|
||||
@@ -917,6 +947,338 @@ def test_p3_payload_additions():
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
# --- B1 checks (photo experience v2, UI-SPEC §11 / DATA-MODEL B1) -----------
|
||||
|
||||
def make_b1_item(title, imgs=None, img=None, price='$5,000', km='8000'):
|
||||
it = make_item(title, price=price, km=km)
|
||||
if img is not None:
|
||||
it['img'] = img
|
||||
if imgs is not None:
|
||||
it['imgs'] = imgs
|
||||
return it
|
||||
|
||||
|
||||
def test_b1_migration_guarded():
|
||||
d, db_path = fresh_db()
|
||||
try:
|
||||
conn = db.connect(db_path)
|
||||
db.init_db(conn)
|
||||
lc = {r['name'] for r in conn.execute(
|
||||
"PRAGMA table_info('listings')")}
|
||||
sc = {r['name'] for r in conn.execute("PRAGMA table_info('swipes')")}
|
||||
assert 'imgs' in lc and 'last_photo_idx' in sc, (lc, sc)
|
||||
db.init_db(conn) # second pass: idempotent
|
||||
lc2 = {r['name'] for r in conn.execute(
|
||||
"PRAGMA table_info('listings')")}
|
||||
assert lc2 == lc
|
||||
conn.close()
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
def test_b1_importer_gallery_carry():
|
||||
"""imgs -> listings.imgs (JSON, page order); img_url keeps `img` and falls
|
||||
back to imgs[0]; empty/junk galleries -> NULL; re-import idempotent."""
|
||||
d, db_path = fresh_db()
|
||||
try:
|
||||
state_dir = os.path.join(d, 'state')
|
||||
CDN = 'https://media.kijiji.ca/api/v1/x/%s?rule=kijijica-640-webp'
|
||||
n8 = [CDN % ('a%d' % i) for i in range(8)]
|
||||
n3 = [CDN % ('b%d' % i) for i in range(3)]
|
||||
ns = {
|
||||
'r8': make_b1_item('2021 Indian Scout Bobber 8shot', imgs=n8),
|
||||
'r3': make_b1_item('2021 Indian Scout Bobber 3shot', imgs=n3,
|
||||
img=n3[0]),
|
||||
'empty': make_b1_item('Kawasaki Vulcan 900 Custom', imgs=[]),
|
||||
'plain': make_b1_item('Honda Shadow'),
|
||||
'junk': make_b1_item('Suzuki C50', imgs='not-a-list'),
|
||||
}
|
||||
nb = {'other': make_item('Some Other Bike', price_num=9999)}
|
||||
write_states(state_dir, make_state(ns), make_state(nb))
|
||||
FIXED = '2026-11-11T12:00:00Z'
|
||||
orig = imp.now_iso
|
||||
imp.now_iso = lambda: FIXED
|
||||
try:
|
||||
r1 = imp.run_import(state_dir, db_path)
|
||||
r2 = imp.run_import(state_dir, db_path)
|
||||
finally:
|
||||
imp.now_iso = orig
|
||||
assert r1['ok'] and r2['ok'] and r2['total'] == 6 and r2['new'] == 0, \
|
||||
(r1, r2)
|
||||
conn = db.connect(db_path)
|
||||
db.init_db(conn)
|
||||
row = lambda s: conn.execute(
|
||||
'SELECT * FROM listings WHERE src_id=?', (s,)).fetchone()
|
||||
r8 = row('r8')
|
||||
assert json.loads(r8['imgs']) == n8 and r8['img_url'] == n8[0]
|
||||
r3 = row('r3')
|
||||
assert json.loads(r3['imgs']) == n3 and r3['img_url'] == n3[0]
|
||||
assert row('empty')['imgs'] is None and row('empty')['img_url'] is None
|
||||
plain = row('plain')
|
||||
assert plain['imgs'] is None and plain['img_url'] is None
|
||||
assert row('junk')['imgs'] is None
|
||||
before = [tuple(x) for x in conn.execute(
|
||||
'SELECT * FROM listings ORDER BY id')]
|
||||
s_n = conn.execute(
|
||||
'SELECT COUNT(*) AS n FROM swipes').fetchone()['n']
|
||||
conn.close()
|
||||
imp.now_iso = lambda: FIXED
|
||||
try:
|
||||
r3 = imp.run_import(state_dir, db_path)
|
||||
finally:
|
||||
imp.now_iso = orig
|
||||
conn = db.connect(db_path)
|
||||
db.init_db(conn)
|
||||
after = [tuple(x) for x in conn.execute(
|
||||
'SELECT * FROM listings ORDER BY id')]
|
||||
assert s_n == conn.execute(
|
||||
'SELECT COUNT(*) AS n FROM swipes').fetchone()['n']
|
||||
conn.close()
|
||||
assert before == after, 're-import must be byte-stable (fixed clock)'
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
def test_b1_640row_reimport_idempotence():
|
||||
"""Deliverable A: the 640-row copy DB re-imported from the hunt state
|
||||
snapshot stays row-for-row stable across two runs (fixed clock)."""
|
||||
sourced = None
|
||||
for cand in B1_STATE_CANDIDATES:
|
||||
if (os.path.exists(os.path.join(cand, 'kijiji-ns.json'))
|
||||
and os.path.exists(os.path.join(cand, 'kijiji-nb.json'))):
|
||||
sourced = cand
|
||||
break
|
||||
assert sourced, 'hunt state snapshot not found (%s)' % (
|
||||
B1_STATE_CANDIDATES,)
|
||||
with open(os.path.join(sourced, 'kijiji-ns.json')) as fh:
|
||||
ns = json.load(fh)
|
||||
with open(os.path.join(sourced, 'kijiji-nb.json')) as fh:
|
||||
nb = json.load(fh)
|
||||
d = tempfile.mkdtemp(prefix='bt-b1-640-')
|
||||
try:
|
||||
state_dir = os.path.join(d, 'state')
|
||||
db_path = os.path.join(d, 'copy.sqlite3')
|
||||
shutil.copy2(LIVE_DB, db_path)
|
||||
ns['fetched_at'] = nb['fetched_at'] = recent_iso() # evergreen gates
|
||||
write_states(state_dir, ns, nb)
|
||||
FIXED = '2026-11-11T12:00:00Z'
|
||||
orig = imp.now_iso
|
||||
imp.now_iso = lambda: FIXED
|
||||
try:
|
||||
r1 = imp.run_import(state_dir, db_path)
|
||||
finally:
|
||||
imp.now_iso = orig
|
||||
assert r1['ok'], r1
|
||||
conn = db.connect(db_path)
|
||||
db.init_db(conn)
|
||||
total = conn.execute('SELECT COUNT(*) AS n FROM listings').fetchone()['n']
|
||||
active = conn.execute(
|
||||
"SELECT COUNT(*) AS n FROM listings WHERE status='active'"
|
||||
).fetchone()['n']
|
||||
dup = conn.execute(
|
||||
"SELECT COUNT(*) AS n FROM listings WHERE status='dup'"
|
||||
).fetchone()['n']
|
||||
before = [tuple(x) for x in conn.execute(
|
||||
'SELECT * FROM listings ORDER BY id')]
|
||||
s_n = conn.execute('SELECT COUNT(*) AS n FROM swipes').fetchone()['n']
|
||||
conn.close()
|
||||
assert total == 640 and active == 563 and dup == 77, \
|
||||
(total, active, dup) # the B1 fixture is stable at 640
|
||||
imp.now_iso = lambda: FIXED
|
||||
try:
|
||||
r2 = imp.run_import(state_dir, db_path)
|
||||
finally:
|
||||
imp.now_iso = orig
|
||||
assert r2['ok'] and r2['new'] == 0 and r2['total'] == 640, r2
|
||||
conn = db.connect(db_path)
|
||||
db.init_db(conn)
|
||||
after = [tuple(x) for x in conn.execute(
|
||||
'SELECT * FROM listings ORDER BY id')]
|
||||
s_n2 = conn.execute('SELECT COUNT(*) AS n FROM swipes').fetchone()['n']
|
||||
conn.close()
|
||||
assert before == after, '640 rows must be byte-stable across import'
|
||||
assert s_n == s_n2, 'swipes must be untouched by re-import'
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
def test_b1_payload_fields_and_rotation_math():
|
||||
"""n_photos == len(imgs) (segments count = n) on deck/section/list rows;
|
||||
§11.5 rotation lead = (last_photo_idx + 1) % n for null / n=1 / n=3 / n=8."""
|
||||
d, c, db_path = p3_env()
|
||||
try:
|
||||
_conn = db.connect(db_path)
|
||||
db.init_db(_conn) # B1 columns on the pristine copy
|
||||
_conn.close()
|
||||
conn = _sq_conn(db_path)
|
||||
CDN = 'https://media.kijiji.ca/api/v1/x/%s?rule=kijijica-640-webp'
|
||||
n8 = [CDN % ('g8-%d' % i) for i in range(8)]
|
||||
n3 = [CDN % ('g3-%d' % i) for i in range(3)]
|
||||
conn.execute(
|
||||
"INSERT INTO listings (region, src_id, title, url, status,"
|
||||
" img_url, imgs, score, first_seen) VALUES"
|
||||
" ('ns','b1g8','B1 eight-photo','http://x/e','active',?,?,200,"
|
||||
" '2026-11-11T00:00:00Z')",
|
||||
(n8[0], json.dumps(n8)))
|
||||
g8 = conn.execute("SELECT id FROM listings WHERE src_id='b1g8'"
|
||||
).fetchone()['id']
|
||||
conn.execute(
|
||||
"INSERT INTO listings (region, src_id, title, url, status,"
|
||||
" img_url, imgs, score, first_seen) VALUES"
|
||||
" ('ns','b1g3','B1 three-photo','http://x/t','active',?,?,190,"
|
||||
" '2026-11-11T00:00:01Z')",
|
||||
(n3[0], json.dumps(n3)))
|
||||
g3 = conn.execute("SELECT id FROM listings WHERE src_id='b1g3'"
|
||||
).fetchone()['id']
|
||||
# a swiped fixture row with a stored photo idx (rotation source)
|
||||
conn.execute(
|
||||
"INSERT INTO listings (region, src_id, title, url, status,"
|
||||
" img_url, imgs, score, first_seen) VALUES"
|
||||
" ('ns','b1rot','B1 rotation','http://x/r','active',?,?,180,"
|
||||
" '2026-11-11T00:00:02Z')",
|
||||
(n8[0], json.dumps(n8)))
|
||||
rot = conn.execute("SELECT id FROM listings WHERE src_id='b1rot'"
|
||||
).fetchone()['id']
|
||||
conn.execute(
|
||||
"INSERT INTO swipes (listing_id, action, last_photo_idx)"
|
||||
" VALUES (?, 'like', 2)", (rot,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
# payload contract: n_photos mirrors the gallery length
|
||||
for payload in (c.get('/api/list?limit=500').json()['items'],
|
||||
c.get('/api/deck?limit=100').json()['items']):
|
||||
m = {i['id']: i for i in payload}
|
||||
for gid, want in ((g8, 8), (g3, 3), (rot, 8)):
|
||||
if gid in m:
|
||||
assert m[gid]['n_photos'] == want, (gid, m[gid])
|
||||
one = c.get('/api/listing/%d' % rot).json()
|
||||
assert one['n_photos'] == 8 and one['last_photo_idx'] == 2
|
||||
# rotation math on the real stored values (FE: (last+1) % n)
|
||||
def lead(li, n):
|
||||
if not n or n < 2 or li is None or li < 0:
|
||||
return 0
|
||||
return (li + 1) % n
|
||||
assert lead(None, 8) == 0 # no stored index -> photo 0
|
||||
assert lead(0, 1) == 0 # single photo -> always 0
|
||||
assert lead(0, 3) == 1 and lead(2, 3) == 0 # 3-photo wrap
|
||||
assert lead(1, 8) == 2 and lead(7, 8) == 0 # 8-photo wrap
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
def test_b1_img_route_chain():
|
||||
"""GET /img/{id}?i=n: mirrored file (incl. {id}_0.webp as the gallery
|
||||
primary name) -> CDN 302 -> 404; i=0 keeps the legacy chain; out-of-range
|
||||
and unknown rows 404."""
|
||||
d, c, db_path = p3_env()
|
||||
try:
|
||||
import sqlite3 as _sq
|
||||
_conn = db.connect(db_path)
|
||||
db.init_db(_conn) # B1 columns on the pristine copy
|
||||
_conn.close()
|
||||
conn = _sq.connect(db_path)
|
||||
conn.row_factory = _sq.Row
|
||||
CDN = 'https://media.kijiji.ca/api/v1/x/%s?rule=kijijica-640-webp'
|
||||
n3 = [CDN % ('c%d' % i) for i in range(3)]
|
||||
conn.execute(
|
||||
"INSERT INTO listings (region, src_id, title, url, status,"
|
||||
" img_url, imgs) VALUES ('ns','b1img','B1 images','http://x/i',"
|
||||
" 'active',?,?)", (n3[0], json.dumps(n3)))
|
||||
gid = conn.execute("SELECT id FROM listings WHERE src_id='b1img'"
|
||||
).fetchone()['id']
|
||||
conn.execute(
|
||||
"INSERT INTO listings (region, src_id, title, url, status,"
|
||||
" img_url) VALUES ('ns','b1leg','B1 legacy','http://x/l',"
|
||||
" 'active',?)", (CDN % 'L',))
|
||||
lg = conn.execute("SELECT id FROM listings WHERE src_id='b1leg'"
|
||||
).fetchone()['id']
|
||||
conn.commit()
|
||||
conn.close()
|
||||
img_dir = os.path.join(d, 'img')
|
||||
for n in range(3):
|
||||
p = os.path.join(img_dir, '%d_%d.webp' % (gid, n))
|
||||
with open(p, 'wb') as fh:
|
||||
fh.write(b'RIFF\x00\x00\x00\x00WEBP g%d n%d' % (gid, n))
|
||||
# legacy chain: {id}.webp file wins for i=0
|
||||
with open(os.path.join(img_dir, '%d.webp' % lg), 'wb') as fh:
|
||||
fh.write(b'RIFF\x00\x00\x00\x00WEBP legacy')
|
||||
r = c.get('/img/%d' % lg)
|
||||
assert r.status_code == 200 and r.headers.get(
|
||||
'content-type', '').startswith('image/webp')
|
||||
# i=0 with no {id}.webp -> accepts the gallery primary {id}_0.webp
|
||||
r = c.get('/img/%d' % gid)
|
||||
assert r.status_code == 200, r.status_code
|
||||
# gallery extras answered for every n
|
||||
for n in (1, 2):
|
||||
r = c.get('/img/%d?i=%d' % (gid, n))
|
||||
assert r.status_code == 200, (n, r.status_code)
|
||||
assert r.headers.get('content-type', '').startswith('image/webp')
|
||||
# out-of-range -> 404; negative -> validation error; unknown -> 404
|
||||
assert c.get('/img/%d?i=3' % gid).status_code == 404
|
||||
assert c.get('/img/%d?i=-1' % gid).status_code in (404, 422)
|
||||
assert c.get('/img/%d?i=1' % lg).status_code == 404 # legacy 1-photo
|
||||
assert c.get('/img/999999').status_code == 404
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
def test_b1_swipe_photo_idx():
|
||||
"""POST /api/swipe optionally persists photo_idx; an absent key preserves
|
||||
the stored index; delete clears it (payload last_photo_idx -> null)."""
|
||||
d, c, db_path = p3_env()
|
||||
try:
|
||||
import sqlite3 as _sq
|
||||
_conn = db.connect(db_path)
|
||||
db.init_db(_conn) # B1 columns on the pristine copy
|
||||
_conn.close()
|
||||
conn = _sq.connect(db_path)
|
||||
conn.row_factory = _sq.Row
|
||||
CDN = 'https://media.kijiji.ca/api/v1/x/%s.jpg'
|
||||
n3 = [CDN % ('p%d' % i) for i in range(3)]
|
||||
conn.execute(
|
||||
"INSERT INTO listings (region, src_id, title, url, status,"
|
||||
" img_url, imgs, score, first_seen) VALUES ('ns','b1sw',"
|
||||
" 'B1 swipe idx','http://x/s','active',?,?,9999,"
|
||||
" '2026-11-11T00:00:00Z')", (n3[0], json.dumps(n3)))
|
||||
gid = conn.execute("SELECT id FROM listings WHERE src_id='b1sw'"
|
||||
).fetchone()['id']
|
||||
conn.commit()
|
||||
conn.close()
|
||||
r = c.post('/api/swipe', json={'listing_id': gid, 'action': 'maybe',
|
||||
'photo_idx': 2})
|
||||
assert r.status_code == 200 and r.json()['last_photo_idx'] == 2, r.text
|
||||
# absent photo_idx preserves the stored index
|
||||
r = c.post('/api/swipe', json={'listing_id': gid, 'action': 'nope'})
|
||||
assert r.status_code == 200 and r.json()['last_photo_idx'] == 2, r.text
|
||||
# explicit overwrite (0 is a valid photo index)
|
||||
r = c.post('/api/swipe', json={'listing_id': gid, 'action': 'like',
|
||||
'photo_idx': 0})
|
||||
assert r.status_code == 200 and r.json()['last_photo_idx'] == 0, r.text
|
||||
# invalid values rejected loudly
|
||||
assert c.post('/api/swipe', json={'listing_id': gid, 'action': 'nope',
|
||||
'photo_idx': -1}).status_code == 400
|
||||
assert c.post('/api/swipe', json={'listing_id': gid, 'action': 'nope',
|
||||
'photo_idx': 'x'}).status_code == 400
|
||||
assert c.post('/api/swipe', json={'listing_id': gid, 'action': 'nope',
|
||||
'photo_idx': True}).status_code == 400
|
||||
# explicit null clears the index
|
||||
r = c.post('/api/swipe', json={'listing_id': gid, 'action': 'maybe',
|
||||
'photo_idx': None})
|
||||
assert r.status_code == 200 and r.json()['last_photo_idx'] is None
|
||||
# payload reflects the persisted index; delete removes the row
|
||||
c.post('/api/swipe', json={'listing_id': gid, 'action': 'like',
|
||||
'photo_idx': 1})
|
||||
lst = c.get('/api/list?limit=500').json()['items']
|
||||
row = [i for i in lst if i['id'] == gid][0]
|
||||
assert row['n_photos'] == 3 and row['last_photo_idx'] == 1
|
||||
c.delete('/api/swipe/%d' % gid)
|
||||
lst = c.get('/api/list?limit=500').json()['items']
|
||||
row = [i for i in lst if i['id'] == gid][0]
|
||||
assert row['last_photo_idx'] is None and row['n_photos'] == 3
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
P3_API_TESTS = [
|
||||
('p3: events seeding idempotent (from swipes)', test_p3_event_seeding_idempotent),
|
||||
('p3: swipe round math + clear-then-re-decide', test_p3_round_math_and_clear),
|
||||
@@ -933,6 +1295,16 @@ P3_API_TESTS = [
|
||||
if API_DEPS:
|
||||
TESTS += P3_API_TESTS
|
||||
|
||||
B1_TESTS = [
|
||||
('b1: guarded migrations (imgs + last_photo_idx, idempotent)',
|
||||
test_b1_migration_guarded),
|
||||
('b1: importer carries imgs gallery (JSON, order, img fallback.)',
|
||||
test_b1_importer_gallery_carry),
|
||||
('b1: 640-row copy re-import byte-stable (A)',
|
||||
test_b1_640row_reimport_idempotence),
|
||||
]
|
||||
TESTS += B1_TESTS
|
||||
|
||||
API_TESTS = [
|
||||
('api: / serves the app + static + manifest/sw', test_api_root),
|
||||
('api: stats live == active count, health counts', test_api_stats_health),
|
||||
@@ -940,6 +1312,12 @@ API_TESTS = [
|
||||
('api: swipe upsert/edit/delete lifecycle', test_api_swipe_lifecycle),
|
||||
('api: /img/{id} 200 cached | 302 hotlink | 404 none', test_api_img_routes),
|
||||
('api: badge seen roundtrip', test_api_badge_roundtrip),
|
||||
('b1: /img/{id}?i=n gallery chain (200/302/404, {id}_0 primary)',
|
||||
test_b1_img_route_chain),
|
||||
('b1: swipe photo_idx persists / preserves / clears',
|
||||
test_b1_swipe_photo_idx),
|
||||
('b1: payload n_photos + last_photo_idx, rotation math',
|
||||
test_b1_payload_fields_and_rotation_math),
|
||||
]
|
||||
|
||||
if API_DEPS:
|
||||
|
||||
@@ -181,6 +181,21 @@ nav{height:calc(68px + env(safe-area-inset-bottom,0px));display:flex;align-items
|
||||
.trow span{cursor:pointer}
|
||||
.trow i{color:#4a4a55;font-style:normal;cursor:default}
|
||||
.tmuted{font-size:12px;color:#6f6f7a;margin:10px 18px 20px}
|
||||
/* B1 §11 — photo galleries: segments, tap zones, fullscreen viewer */
|
||||
.segments{position:absolute;top:8px;left:50%;transform:translateX(-50%);display:flex;gap:4px;z-index:4;pointer-events:none}
|
||||
.segments i{width:22px;height:3px;border-radius:2px;background:rgba(255,255,255,.35);transition:background .15s}
|
||||
.segments i.on{background:#fff}
|
||||
.card.hasSegs .band{top:22px} /* §11.2 amendment: bands drop below the bars */
|
||||
.vback{position:absolute;inset:0;z-index:71;background:rgba(0,0,0,.45);opacity:0;pointer-events:none;transition:opacity .28s}
|
||||
.vback.on{opacity:1;pointer-events:auto}
|
||||
.viewer{position:absolute;top:26px;left:0;right:0;bottom:0;z-index:72;background:#0b0b0f;display:flex;flex-direction:column;border-radius:18px 18px 0 0;box-shadow:0 -14px 40px rgba(0,0,0,.5);transform:translateY(103%);transition:transform .3s cubic-bezier(.2,.8,.3,1);overflow:hidden}
|
||||
.viewer.on{transform:translateY(0)}
|
||||
.vgrab{flex:none;height:26px;display:flex;align-items:center;justify-content:center;cursor:grab;touch-action:none}
|
||||
.vgrab i{width:42px;height:5px;border-radius:3px;background:rgba(255,255,255,.3);display:block}
|
||||
.vclose{position:absolute;top:38px;left:14px;width:36px;height:36px;border-radius:50%;background:rgba(0,0,0,.55);color:#fff;border:0;font-size:16px;z-index:6;cursor:pointer;display:flex;align-items:center;justify-content:center}
|
||||
.vphoto{position:relative;flex:1;min-height:0;overflow:hidden;touch-action:none}
|
||||
.vimg{position:absolute;inset:0;width:100%;height:100%;object-fit:contain;transform-origin:center center}
|
||||
.viewer .segments{top:34px}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -253,6 +268,14 @@ nav{height:calc(68px + env(safe-area-inset-bottom,0px));display:flex;align-items
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="vback" id="vback"></div>
|
||||
<div class="viewer" id="viewer">
|
||||
<div class="vgrab" id="vgrab"><i></i></div>
|
||||
<button class="vclose" id="vclose">✕</button>
|
||||
<div class="segments" id="vsegs"></div>
|
||||
<div class="vphoto" id="vphoto"><img class="vimg" id="vimg" alt="" draggable="false"></div>
|
||||
</div>
|
||||
|
||||
<div class="list" id="listScreen">
|
||||
<div class="lhead"><h1>All bikes</h1><div class="sub" id="lsub">—</div></div>
|
||||
<input class="search" id="lsearch" type="search" placeholder="Search titles" autocomplete="off">
|
||||
@@ -332,12 +355,18 @@ async function getJSON(url){
|
||||
/* swipe persistence: optimistic, never waits; failures retry 2s/5s/10s then
|
||||
every 15s while the tab lives. Last write per listing wins; the queue runs
|
||||
serially so reorderings can't land out of sequence. */
|
||||
const pend=new Map(); // listing_id -> {act, secId, fails, warned}
|
||||
const pend=new Map(); // listing_id -> {act, secId, photoIdx, fails, warned}
|
||||
let qRunning=false, qTimer=null, qWarned=false;
|
||||
function enqueue(listingId, act, secId=null){ // act: 'like'|'nope'|'maybe'|null (delete)
|
||||
pend.set(listingId,{act,secId,fails:0});
|
||||
function enqueue(listingId, act, secId=null, photoIdx=null){ // act: 'like'|'nope'|'maybe'|null (delete)
|
||||
pend.set(listingId,{act,secId,photoIdx,fails:0});
|
||||
scheduleQueue();
|
||||
}
|
||||
function swipeBody(id, act, secId, photoIdx){
|
||||
const b={listing_id:id,action:act};
|
||||
if(secId!=null) b.section_id=secId;
|
||||
if(photoIdx!=null) b.photo_idx=photoIdx; // B1 §11.5: photo on screen at strike
|
||||
return b;
|
||||
}
|
||||
function queDelay(op){ return op.fails===1?2000:op.fails===2?5000:op.fails===3?10000:15000; }
|
||||
function scheduleQueue(){
|
||||
if(qRunning||!pend.size||qTimer) return;
|
||||
@@ -355,7 +384,7 @@ async function runQueue(){
|
||||
try{
|
||||
const res= p.act==null
|
||||
? await fetch('/api/swipe/'+id,{method:'DELETE'})
|
||||
: await fetch('/api/swipe',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(p.secId?{listing_id:id,action:p.act,section_id:p.secId}:{listing_id:id,action:p.act})});
|
||||
: await fetch('/api/swipe',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(swipeBody(id,p.act,p.secId,p.photoIdx))});
|
||||
if(res.ok){ pend.delete(id); }
|
||||
else { p.fails++; if(!qWarned){ qWarned=true; toast("Couldn't save — retrying"); } minDelay=Math.max(minDelay,queDelay(p)); }
|
||||
}catch(e){
|
||||
@@ -371,10 +400,10 @@ async function runQueue(){
|
||||
progress stays correct while the persistence queue is mid-flight. */
|
||||
const localDecisions=new Map(); // listing_id -> action|null
|
||||
let lastResyncAt=0;
|
||||
function setDecision(id, act, secId=null){
|
||||
function setDecision(id, act, secId=null, photoIdx=null){
|
||||
localDecisions.set(id, act);
|
||||
updateRowDecision(id, act); // optimistic paint
|
||||
enqueue(id, act, secId);
|
||||
enqueue(id, act, secId, photoIdx);
|
||||
}
|
||||
|
||||
/* ---------- payload -> display helpers ---------- */
|
||||
@@ -458,12 +487,78 @@ function bandHTML(item){ // P3 system bands — the ONLY card-fa
|
||||
}
|
||||
return '';
|
||||
}
|
||||
/* ---------- B1 §11: photo galleries ---------- */
|
||||
function nPhotos(it){ // payload n_photos; tolerant of old APIs
|
||||
const n= it&&typeof it.n_photos==='number'?it.n_photos:(it&&it.img?1:0);
|
||||
return Math.max(0, Math.trunc(n));
|
||||
}
|
||||
function leadFor(it){ // §11.5 repeat rotation: (last+1) % n
|
||||
const n=nPhotos(it);
|
||||
if(n<2) return 0;
|
||||
const li=it.last_photo_idx;
|
||||
if(!Number.isInteger(li)||li<0) return 0;
|
||||
return (li+1)%n;
|
||||
}
|
||||
function photoSrc(it,i){ if(!it||!it.img) return null; return i>0?('/img/'+it.id+'?i='+i):it.img; }
|
||||
function segBars(n,cur){ // the <i> bars only (no wrapper)
|
||||
let h='';
|
||||
for(let k=0;k<n;k++) h+=`<i${k===cur?' class="on"':''}></i>`;
|
||||
return h;
|
||||
}
|
||||
function segHTML(n,cur){
|
||||
if(!n||n<2) return '';
|
||||
return `<div class="segments">${segBars(n,cur)}</div>`;
|
||||
}
|
||||
function phwrapHTML(src){
|
||||
return `<div class="phwrap">${src?`<img class="photo" src="${src}" alt="" draggable="false">`:`<div class="nophoto">Photo pending — thumbnail backfill queued</div>`}</div>`;
|
||||
}
|
||||
function attachPhError(entry){
|
||||
const wrap=entry.el.querySelector('.phwrap');
|
||||
const im=wrap.querySelector('img');
|
||||
if(im) im.addEventListener('error',()=>{ wrap.innerHTML='<div class="nophoto">Photo pending — thumbnail backfill queued</div>'; });
|
||||
}
|
||||
function paintSegs(entry){
|
||||
const segs=entry.el.querySelector('.segments');
|
||||
if(segs) [...segs.children].forEach((x,k)=>x.classList.toggle('on',k===(entry.cur||0)));
|
||||
}
|
||||
/* §11.2 flip — instant, clamped at the ends (no wrap), commits nothing */
|
||||
function flipCard(entry,dir){
|
||||
const n=nPhotos(entry.d);
|
||||
if(n<2) return;
|
||||
const cur=(entry.cur||0)+dir;
|
||||
if(cur<0||cur>=n) return;
|
||||
entry.cur=cur;
|
||||
const wrap=entry.el.querySelector('.phwrap');
|
||||
wrap.innerHTML= phwrapHTML(photoSrc(entry.d,cur));
|
||||
attachPhError(entry);
|
||||
paintSegs(entry);
|
||||
preloadPhotos();
|
||||
}
|
||||
/* §11.6 preload: top card's next photo + next card's lead photo, off-path */
|
||||
function preloadPhotos(){
|
||||
pile.slice(0,2).forEach((entry,pos)=>{
|
||||
const d=entry.d;
|
||||
if(!d) return;
|
||||
const targets= pos===0
|
||||
? ((()=>{ const n=nPhotos(d); const nx=(entry.cur||0)+1;
|
||||
return (n>1&&nx<n)?[photoSrc(d,nx)]:[]; })())
|
||||
: [photoSrc(d,leadFor(d))];
|
||||
for(const s of targets){
|
||||
if(!s) continue;
|
||||
const im=new Image(); im.decoding='async'; im.src=s;
|
||||
}
|
||||
});
|
||||
}
|
||||
function cardNode(item){
|
||||
const el=document.createElement('div');
|
||||
el.className='card';
|
||||
el.innerHTML=`${item.img?`<img class="photo" src="${item.img}" alt="" draggable="false">`:`<div class="nophoto">Photo pending — thumbnail backfill queued</div>`}
|
||||
const n=nPhotos(item);
|
||||
const lead=leadFor(item);
|
||||
if(n>=2) el.classList.add('hasSegs');
|
||||
el.innerHTML=`${phwrapHTML(photoSrc(item,lead))}
|
||||
<div class="grad"></div>
|
||||
${bandHTML(item)}
|
||||
${segHTML(n,lead)}
|
||||
<div class="stamp like">LIKE</div>
|
||||
<div class="stamp nope">NOPE</div>
|
||||
<div class="stamp maybe">MAYBE</div>
|
||||
@@ -474,11 +569,8 @@ function cardNode(item){
|
||||
info.querySelector('.title').textContent=item.title||'';
|
||||
info.querySelector('.price').textContent=priceFor(item);
|
||||
info.querySelector('.sub').textContent=subFor(item);
|
||||
const ph=el.querySelector('.photo');
|
||||
if(ph) ph.addEventListener('error',()=>{
|
||||
ph.remove();
|
||||
el.insertAdjacentHTML('afterbegin','<div class="nophoto">Photo pending — thumbnail backfill queued</div>');
|
||||
});
|
||||
const entry={d:item,el,cur:lead};
|
||||
attachPhError(entry);
|
||||
el.addEventListener('pointerdown',down);
|
||||
el.addEventListener('pointermove',move);
|
||||
el.addEventListener('pointerup',upH);
|
||||
@@ -486,7 +578,7 @@ function cardNode(item){
|
||||
const ib=el.querySelector('.ibtn');
|
||||
ib.addEventListener('pointerdown',e=>e.stopPropagation());
|
||||
ib.addEventListener('click',e=>{ e.stopPropagation(); if(isTop(el)) openDetail(item,{type:'deck'}); });
|
||||
return {d:item,el};
|
||||
return entry;
|
||||
}
|
||||
function layer(el,d,anim){
|
||||
el.classList.toggle('topcard',d===0);
|
||||
@@ -511,6 +603,7 @@ function fillPile(){
|
||||
layer(entry.el,d,true);
|
||||
}
|
||||
relayer();
|
||||
preloadPhotos();
|
||||
}
|
||||
/* deck feed: P3 sections (pinned order) with a legacy /api/deck fallback */
|
||||
async function loadDeck(){ // legacy cursor feed (pre-P3 API)
|
||||
@@ -586,7 +679,8 @@ function commit(el,dir){
|
||||
const t=pile.shift();
|
||||
hist.push(t.d);
|
||||
const act= dir==='maybe'?'maybe':dir;
|
||||
setDecision(t.d.id, act, (!legacyMode&§ion&&!section.exhausted)?section.section.id:null);
|
||||
setDecision(t.d.id, act, (!legacyMode&§ion&&!section.exhausted)?section.section.id:null,
|
||||
nPhotos(t.d)>1?(t.cur||0):null); // B1: photo on screen at strike
|
||||
sectDecided++;
|
||||
paintSect();
|
||||
flyOut(el,dir);
|
||||
@@ -620,6 +714,7 @@ function undo(){
|
||||
if(pile.length>DEPTH){ const drop=pile.pop(); drop.el.remove(); buffer.unshift(drop.d); }
|
||||
void entry.el.offsetWidth;
|
||||
relayer();
|
||||
preloadPhotos();
|
||||
$('empty').classList.remove('on');
|
||||
$('report').classList.remove('on');
|
||||
}
|
||||
@@ -654,13 +749,25 @@ function upH(e){
|
||||
if(dx>w*0.35) commit(el,'like');
|
||||
else if(dx<-w*0.35) commit(el,'nope');
|
||||
else if(dy<-140) commit(el,'maybe');
|
||||
else { el.style.transform=''; setStamps(el,0,0,0); if(!moved && e.type==='pointerup' && isTop(el)) openDetail(pile[0].d,{type:'deck'}); }
|
||||
else { el.style.transform=''; setStamps(el,0,0,0); if(!moved && e.type==='pointerup' && isTop(el)) tapZone(pile[0],e); }
|
||||
}
|
||||
/* §11.2/§11.3/§11.4 tap zones: sides flip (clamped), center = fullscreen.
|
||||
The old tap-anywhere -> dossier gesture is retired (ⓘ stays the door). */
|
||||
function tapZone(entry,e){
|
||||
const n=nPhotos(entry.d);
|
||||
if(n<1 || !entry.d.img) return;
|
||||
const r=stage.getBoundingClientRect();
|
||||
const rel=e.clientX-r.left, w=r.width||360;
|
||||
if(rel>=w/3 && rel<=w*2/3) openViewer(entry.d, entry.cur||0, {type:'card',entry});
|
||||
else flipCard(entry, rel<w/3?-1:1);
|
||||
}
|
||||
|
||||
/* ---------- docs/mockup core: fullscreen dossier ---------- */
|
||||
let dCtx=null;
|
||||
let dData=null; // B1: dossier payload (hero -> viewer)
|
||||
function openDetail(data,ctx){
|
||||
dCtx=ctx||null;
|
||||
dData=data;
|
||||
const ph=$('dphoto');
|
||||
if(data.img){ ph.src=data.img; ph.classList.remove('ph'); }
|
||||
else { ph.removeAttribute('src'); ph.classList.add('ph'); }
|
||||
@@ -721,7 +828,12 @@ function dStart(e){ dDragY=e.clientY; $('detail').style.transition='none'; try{e
|
||||
function dMoveH(e){ if(dDragY===null) return; const dy=Math.max(0,e.clientY-dDragY); $('detail').style.transform=`translateY(${dy}px)`; }
|
||||
function dEndH(e){ if(dDragY===null) return; const dy=Math.max(0,e.clientY-dDragY); dDragY=null;
|
||||
if(dy>110){ closeDetail(); }
|
||||
else { const d=$('detail'); d.style.transition='transform .25s cubic-bezier(.2,.8,.3,1)'; d.style.transform=''; } }
|
||||
else { const d=$('detail'); d.style.transition='transform .25s cubic-bezier(.2,.8,.3,1)'; d.style.transform='';
|
||||
/* B1 §11.3: a tap on the dossier HERO opens the fullscreen viewer at photo 0 */
|
||||
if(e.type==='pointerup' && dy<=6 && e.currentTarget&&e.currentTarget.classList
|
||||
&& e.currentTarget.classList.contains('dhero') && dData&&dData.img){
|
||||
openViewer(dData,0,{type:'dossier'});
|
||||
} } }
|
||||
[document.querySelector('.dgrab'),document.querySelector('.dhero')].forEach(el=>{ if(!el) return; el.addEventListener('pointerdown',dStart); el.addEventListener('pointermove',dMoveH); el.addEventListener('pointerup',dEndH); el.addEventListener('pointercancel',dEndH); });
|
||||
function dAction(a){
|
||||
const ctx=dCtx; closeDetail();
|
||||
@@ -736,6 +848,113 @@ function dAction(a){
|
||||
}
|
||||
$('dNo').onclick=()=>dAction('nope'); $('dMb').onclick=()=>dAction('maybe'); $('dLk').onclick=()=>dAction('like');
|
||||
|
||||
/* ---------- B1 §11.3: fullscreen viewer (swipe nav, pinch/double-tap zoom,
|
||||
✕ + pull-down close) — returns to the same card & photo position ---------- */
|
||||
let vCtx=null; // {item, idx, back} — deck cards untouched
|
||||
let vScale=1, vTx=0, vTy=0;
|
||||
let vDrag=null; // single-pointer gesture state
|
||||
const vPtrs=new Map(); // pointerId -> {x,y}
|
||||
let vPinch=null; // {d0, scale0}
|
||||
let vLastTap=0;
|
||||
function vCount(){ return vCtx&&vCtx.item?nPhotos(vCtx.item):0; }
|
||||
function vApply(){ if(!vCtx) return; if(vScale<=1){ vTx=0; vTy=0; } $('vimg').style.transform=`translate(${vTx}px,${vTy}px) scale(${vScale})`; }
|
||||
function vPaint(){
|
||||
const item=vCtx.item, i=vCtx.idx, n=vCount();
|
||||
$('vsegs').innerHTML= n>=2?segBars(n,i):'';
|
||||
const im=$('vimg'), s=photoSrc(item,i);
|
||||
if(s){ im.src=s; im.style.opacity=1; }
|
||||
else { im.removeAttribute('src'); im.style.opacity=.25; }
|
||||
im.onerror=()=>{ im.style.opacity=.25; };
|
||||
vApply();
|
||||
}
|
||||
function openViewer(item, idx, back){
|
||||
const n=nPhotos(item);
|
||||
if(!n||!item||!item.img) return;
|
||||
vCtx={item, idx:Math.max(0,Math.min(idx||0,n-1)), back:back||null};
|
||||
vScale=1; vTx=0; vTy=0;
|
||||
vPaint();
|
||||
$('viewer').classList.add('on'); $('vback').classList.add('on');
|
||||
}
|
||||
function closeViewer(){
|
||||
const v=$('viewer');
|
||||
v.classList.remove('on'); $('vback').classList.remove('on');
|
||||
v.style.transition=''; v.style.transform='';
|
||||
vCtx=null; vPtrs.clear(); vPinch=null; vDrag=null;
|
||||
}
|
||||
$('vback').addEventListener('click',closeViewer);
|
||||
$('vclose').onclick=closeViewer;
|
||||
$('vclose').addEventListener('pointerdown',e=>e.stopPropagation());
|
||||
const vph=$('vphoto');
|
||||
function vDist(){
|
||||
const pts=[...vPtrs.values()];
|
||||
return pts.length>=2?Math.hypot(pts[0].x-pts[1].x,pts[0].y-pts[1].y):null;
|
||||
}
|
||||
vph.addEventListener('pointerdown',e=>{
|
||||
if(!vCtx) return;
|
||||
vPtrs.set(e.pointerId,{x:e.clientX,y:e.clientY});
|
||||
try{ vph.setPointerCapture(e.pointerId); }catch(_){}
|
||||
if(vPtrs.size===1){ vDrag={sx:e.clientX,sy:e.clientY,px:vTx,py:vTy,scale0:vScale,mode:null}; }
|
||||
else if(vPtrs.size===2){ vDrag=null; vPinch={d0:vDist()||1,scale0:vScale}; }
|
||||
});
|
||||
vph.addEventListener('pointermove',e=>{
|
||||
if(!vCtx) return;
|
||||
const pt=vPtrs.get(e.pointerId); if(pt){ pt.x=e.clientX; pt.y=e.clientY; }
|
||||
if(vPtrs.size>=2&&vPinch){
|
||||
const dd=vDist(); if(!dd) return;
|
||||
vScale=Math.max(1,Math.min(4,vPinch.scale0*dd/vPinch.d0)); // 1×–4×
|
||||
vApply(); return;
|
||||
}
|
||||
if(!vDrag) return;
|
||||
const dx=e.clientX-vDrag.sx, dy=e.clientY-vDrag.sy;
|
||||
if(!vDrag.mode){
|
||||
if(Math.abs(dx)<=6&&Math.abs(dy)<=6) return;
|
||||
vDrag.mode= vScale>1?'pan':(Math.abs(dx)>=Math.abs(dy)?'nav':'pull');
|
||||
}
|
||||
if(vDrag.mode==='pull'){
|
||||
const v=$('viewer'); v.style.transition='none';
|
||||
v.style.transform=`translateY(${Math.max(0,dy)}px)`;
|
||||
} else if(vDrag.mode==='nav'){
|
||||
vTx=Math.max(-70,Math.min(70,dx)); vApply(); // live edge-feedback
|
||||
} else { vTx=vDrag.px+dx; vTy=vDrag.py+dy; vApply(); } // pan while zoomed
|
||||
});
|
||||
function vUp(e){
|
||||
const wasDrag=vDrag, dx=wasDrag?e.clientX-wasDrag.sx:0, dy=wasDrag?e.clientY-wasDrag.sy:0;
|
||||
if(wasDrag&&vPtrs.size<=1){
|
||||
const mode=wasDrag.mode;
|
||||
if(mode==='pull'){
|
||||
if(dy>110){ closeViewer(); }
|
||||
else { const v=$('viewer'); v.style.transition='transform .25s cubic-bezier(.2,.8,.3,1)'; v.style.transform=''; }
|
||||
} else if(mode==='nav'){
|
||||
if(Math.abs(dx)>=50&&vCtx){
|
||||
const nx=vCtx.idx+(dx>0?-1:1); // clamp at the ends
|
||||
if(nx>=0&&nx<vCount()){ vCtx.idx=nx; vPaint(); }
|
||||
}
|
||||
vTx=0; vApply();
|
||||
} else if(mode==='pan'){
|
||||
// keep pan position
|
||||
} else if(e.type==='pointerup'){ // tap -> double-tap zoom toggle
|
||||
const now=Date.now();
|
||||
if(now-vLastTap<300){ vLastTap=0; vScale= vScale>1?1:2.5; vApply(); }
|
||||
else vLastTap=now;
|
||||
}
|
||||
vDrag=null;
|
||||
}
|
||||
vPtrs.delete(e.pointerId); vPinch=null;
|
||||
}
|
||||
vph.addEventListener('pointerup',vUp);
|
||||
vph.addEventListener('pointercancel',vUp);
|
||||
/* pull-down close from the grabber (the §3 idiom) */
|
||||
let vgY=null;
|
||||
function vgStart(e){ vgY=e.clientY; $('viewer').style.transition='none'; try{e.currentTarget.setPointerCapture(e.pointerId)}catch(_){} }
|
||||
function vgMove(e){ if(vgY===null) return; $('viewer').style.transform=`translateY(${Math.max(0,e.clientY-vgY)}px)`; }
|
||||
function vgEnd(e){ if(vgY===null) return; const dy=Math.max(0,e.clientY-vgY); vgY=null;
|
||||
if(dy>110){ closeViewer(); }
|
||||
else { const v=$('viewer'); v.style.transition='transform .25s cubic-bezier(.2,.8,.3,1)'; v.style.transform=''; } }
|
||||
$('vgrab').addEventListener('pointerdown',vgStart);
|
||||
$('vgrab').addEventListener('pointermove',vgMove);
|
||||
$('vgrab').addEventListener('pointerup',vgEnd);
|
||||
$('vgrab').addEventListener('pointercancel',vgEnd);
|
||||
|
||||
/* ---------- list screen (real filters + search) ---------- */
|
||||
let listItems=[];
|
||||
const listCtl={decision:'all',sort:'score',q:'',priceDir:1};
|
||||
@@ -1043,7 +1262,7 @@ document.querySelectorAll('.tab').forEach(t=>t.onclick=()=>{
|
||||
|
||||
/* ---------- desktop keyboard arrows (DESIGN.md: v1 frontend) ---------- */
|
||||
document.addEventListener('keydown',e=>{
|
||||
if(e.target.tagName==='INPUT'||$('detail').classList.contains('on')) return;
|
||||
if(e.target.tagName==='INPUT'||$('detail').classList.contains('on')||$('viewer').classList.contains('on')) return;
|
||||
if(e.key==='ArrowRight'){ if(pile[0]) commit(pile[0].el,'like'); }
|
||||
else if(e.key==='ArrowLeft'){ if(pile[0]) commit(pile[0].el,'nope'); }
|
||||
else if(e.key==='ArrowUp'){ if(pile[0]) commit(pile[0].el,'maybe'); }
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
offline once seen).
|
||||
- Everything else (shell/navigation): network-first, fall back to cache.
|
||||
*/
|
||||
const VERSION = 'bike-tinder-v2';
|
||||
const VERSION = 'bike-tinder-v3';
|
||||
const SHELL = [
|
||||
'/',
|
||||
'/manifest.json',
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
httpx
|
||||
playwright==1.54.0 # B1 dev/test only: headless FE walk (app/fe_walk_b1.py)
|
||||
|
||||
Reference in New Issue
Block a user