// Local course fixture only. Does not start a workout or contact a wearable. const assert = require('node:assert/strict'); const { chromium } = require(process.env.PLAYWRIGHT_MODULE || 'playwright'); const base = process.env.MAP_TEST_BASE || 'http://localhost:3113'; (async () => { const browser = await chromium.launch({ headless: true, channel: 'chrome' }); try { for (const angle of [-90, -35, 0, 45, 160, null]) { const context = await browser.newContext({ viewport: { width: 393, height: 852 }, serviceWorkers: 'block' }); const page = await context.newPage(); const errors = []; page.on('pageerror', e => errors.push(e.message)); await context.addInitScript((angle) => { // Observe real Leaflet instances without adding production test hooks. let leaflet; window.__testMaps = []; Object.defineProperty(window, 'L', { configurable: true, get: () => leaflet, set(value) { leaflet = value; value.Map.addInitHook(function () { window.__testMaps.push(this); }); }, }); const shape = [[-1,-1],[.1,-.7],[.8,-1],[.7,.5],[.2,1.3],[-.8,.4],[-1,-1]]; const rad = (angle ?? 0) * Math.PI / 180; const route = shape.map(([x,y]) => [37.5 - (x*Math.sin(rad)+y*Math.cos(rad)) / 1113.2, 127 + (x*Math.cos(rad)-y*Math.sin(rad)) / (1113.2*Math.cos(37.5*Math.PI/180))]); const course = { id:'orientation-fixture', title:'방향 검증 코스', place:'테스트', distanceKm:1, center:route[0], route, label:'cup', source:'test', backHref:'/route-search', ...(angle === null ? {} : { displayRotationDeg:-angle }) }; sessionStorage.setItem('tranimal:course-preview:orientation-test', JSON.stringify(course)); const token = `${btoa(JSON.stringify({alg:'none'}))}.${btoa(JSON.stringify({sub:'1',exp:Math.floor(Date.now()/1000)+3600}))}.fixture`; localStorage.setItem('access_token', token); document.cookie = `access_token=${token}; path=/`; }, angle); await page.route('**/api/**', route => route.fulfill({ json: { nickname:'테스트', username:'테스트', routes:[] } })); await page.goto(`${base}/route-results/orientation-fixture?courseKey=tranimal%3Acourse-preview%3Aorientation-test`); await page.locator('.leaflet-overlay-pane path').waitFor(); await page.waitForFunction(() => window.__testMaps?.some(m => m._loaded)); const state = () => page.evaluate(() => { const map = window.__testMaps.find(m => m._loaded); const point = map.getCenter(); const pixel = window.L.point(map.getSize().x * .43, map.getSize().y * .37); const roundtrip = map.latLngToContainerPoint(map.containerPointToLatLng(pixel)); const course = JSON.parse(sessionStorage.getItem('tranimal:course-preview:orientation-test')); const a = map.latLngToContainerPoint(course.route[0]); const b = map.latLngToContainerPoint(course.route[1]); return { bearing:map.getBearing(), center:[point.lat,point.lng], zoom:map.getZoom(), firstSegmentAngle:Math.atan2(b.y-a.y,b.x-a.x)*180/Math.PI, roundtripError:roundtrip.distanceTo(pixel), tileRotates:!!document.querySelector('.leaflet-tile-pane')?.closest('.leaflet-rotate-pane'), routeRotates:!!document.querySelector('.leaflet-overlay-pane')?.closest('.leaflet-rotate-pane') }; }); const button = page.getByRole('button', { name:'도안 방향에 맞춰 보기', exact:true }); if (angle === null) { assert.equal(await button.count(), 0); assert.equal((await state()).bearing, 0); } else { await page.waitForFunction(expected => { const map=window.__testMaps.find(m=>m._loaded); return Math.abs(map.getBearing()-expected)<.001; }, ((-angle % 360)+360)%360); assert.equal(await button.getAttribute('aria-pressed'), 'true'); const aligned = await state(); assert(aligned.tileRotates && aligned.routeRotates, 'basemap and polyline must rotate together'); const expectedSegment = Math.atan2(.3,1.1)*180/Math.PI; assert(Math.abs((aligned.firstSegmentAngle-expectedSegment+540)%360-180)<2, `restore source shape direction: ${aligned.firstSegmentAngle}`); // Leaflet rounds projected layer points to integer pixels. assert(aligned.roundtripError < 1, `rotated screen/GPS mapping: ${aligned.roundtripError}px`); await button.click(); assert.equal((await state()).bearing, 0); assert.equal(await button.getAttribute('aria-pressed'), 'false'); await button.click(); assert(Math.abs((await state()).bearing - aligned.bearing) < .001); const old = await state(); await page.getByRole('button', { name:'Zoom in', exact:true }).click(); await page.waitForTimeout(150); assert((await state()).zoom > old.zoom, 'zoom after rotation'); const unchanged = await page.evaluate(() => { const course=JSON.parse(sessionStorage.getItem('tranimal:course-preview:orientation-test')); return course.displayRotationDeg; }); assert.equal(unchanged, angle === 0 ? 0 : -angle, 'do not mutate stored metadata (JSON normalizes -0)'); } assert.deepEqual(errors, [], 'no browser runtime errors'); console.log(`PASS orientation=${angle ?? 'legacy/no-angle'}`); if (process.env.MAP_TEST_SCREENSHOT && angle === 45) { await page.screenshot({ path:process.env.MAP_TEST_SCREENSHOT }); } await context.close(); } } finally { await browser.close(); } })().catch(error => { console.error(error); process.exitCode=1; });