Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/deploy-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,38 @@ jobs:
echo "VITE_BASE_PATH=$base" >> "$GITHUB_ENV"
echo "PAGES_BASE_PATH=$base" >> "$GITHUB_ENV"
echo "base=$base" >> "$GITHUB_OUTPUT"
- name: Resolve and inject commit SHA
id: commit-sha
shell: bash
run: |
set -euo pipefail
sha=$(git rev-parse --verify HEAD^{commit})
if [[ ! "$sha" =~ ^[0-9a-f]{40}$ ]]; then
echo "ERROR: invalid commit SHA: $sha" >&2
exit 1
fi
printf 'VITE_LOOP_COMMIT_SHA=%s\n' "$sha" >> "$GITHUB_ENV"
printf 'sha=%s\n' "$sha" >> "$GITHUB_OUTPUT"
- run: pnpm build
env:
VITE_LOOP_COMMIT_SHA: ${{ steps.commit-sha.outputs.sha }}
- name: Verify main build asset base path
shell: bash
run: |
set -euo pipefail
base="${{ steps.vite-base.outputs.base }}"
grep -F "${base}assets/" dist/index.html
if grep -qE 'src="/assets/|href="/assets/' dist/index.html; then exit 1; fi
- name: Verify commit SHA in dist
shell: bash
run: |
set -euo pipefail
expected_sha="${{ steps.commit-sha.outputs.sha }}"
if ! grep -rq "$expected_sha" dist/; then
echo "ERROR: commit SHA $expected_sha not found in dist/" >&2
exit 1
fi
echo "OK: commit SHA $expected_sha found in dist/"
- name: Deploy to gh-pages (replace root, preserve pr-*)
run: |
git config user.name "github-actions[bot]"
Expand Down Expand Up @@ -101,6 +125,7 @@ jobs:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
with:
persist-credentials: false
ref: ${{ github.event.pull_request.head.sha }}
- uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6
with:
version: 11
Expand All @@ -126,14 +151,38 @@ jobs:
echo "VITE_BASE_PATH=$base" >> "$GITHUB_ENV"
echo "PAGES_BASE_PATH=$base" >> "$GITHUB_ENV"
echo "base=$base" >> "$GITHUB_OUTPUT"
- name: Resolve and inject commit SHA
id: commit-sha
shell: bash
run: |
set -euo pipefail
sha=$(git rev-parse --verify HEAD^{commit})
if [[ ! "$sha" =~ ^[0-9a-f]{40}$ ]]; then
echo "ERROR: invalid commit SHA: $sha" >&2
exit 1
fi
printf 'VITE_LOOP_COMMIT_SHA=%s\n' "$sha" >> "$GITHUB_ENV"
printf 'sha=%s\n' "$sha" >> "$GITHUB_OUTPUT"
- run: pnpm build
env:
VITE_LOOP_COMMIT_SHA: ${{ steps.commit-sha.outputs.sha }}
- name: Verify PR preview build asset base path
shell: bash
run: |
set -euo pipefail
base="${{ steps.vite-base.outputs.base }}"
grep -F "${base}assets/" dist/index.html
if grep -qE 'src="/assets/|href="/assets/' dist/index.html; then exit 1; fi
- name: Verify commit SHA in dist
shell: bash
run: |
set -euo pipefail
expected_sha="${{ steps.commit-sha.outputs.sha }}"
if ! grep -rq "$expected_sha" dist/; then
echo "ERROR: commit SHA $expected_sha not found in dist/" >&2
exit 1
fi
echo "OK: commit SHA $expected_sha found in dist/"
- uses: peaceiris/actions-gh-pages@84c30a85c19949d7eee79c4ff27748b70285e453 # v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
52 changes: 38 additions & 14 deletions docs/playtest/manual-playtest-runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ Evidence Panel は `?playtest_evidence=1` クエリパラメータを付ける

- **Download YAML** ボタンをクリックすると `loop-protocol-playtest-evidence-<ISO8601>.yaml` ファイルがダウンロードされる。

### Commit SHA 注入の確認(AC8)

Evidence Panel の YAML を開き、`app_under_test.commit` フィールドを確認する:

```yaml
app_under_test:
name: loop-protocol
commit: "abc1234def5678901234567890123456789012345678" # 40-char hex SHA(注入成功)
```

- **40文字の hex SHA が入っていれば注入成功**。この値を `tested_commit` として PR コメントに記録する。
- `commit: "unknown"` になっている場合は `commit_unknown_reason` を確認し、`tested_commit: "unknown"` + `tested_commit_unknown_reason` を明記する。

### Evidence Panel 出力スキーマと ADR 提出スキーマの対応

Evidence Panel が生成する YAML と、GitHub への最終提出 YAML はスキーマキーが異なる。以下の対応に従ってマッピングする。
Expand All @@ -140,17 +153,25 @@ Evidence Panel が生成する YAML と、GitHub への最終提出 YAML はス

```yaml
playtest_evidence_schema_version: v1
app_under_test:
name: loop-protocol
commit: "abc1234def5678901234567890123456789012345678" # 40-char hex SHA(注入成功時)
# commit: "unknown" # 注入失敗時
# commit_unknown_reason: "..." # 注入失敗時のみ出力(成功時は key absent)
browser:
name: "Chrome"
version: "125.0.0.0"
version_source: "automatic"
unknown_reason: null
version_source: "userAgent"
platform: "Win32"
user_agent: "Mozilla/5.0 ..."
environment:
viewport: "1920x1080"
device_pixel_ratio: 1
os: "Windows 11 + WSL2/Ubuntu"
commit: "abc1234..."
commit_unknown_reason: null
viewport:
inner_width: 1920
inner_height: 1080
device_pixel_ratio:
value: 1
note: "..."
timezone: "Asia/Tokyo"
language: "ja"
```

**GitHub 最終提出(`schema_version: playtest_evidence_v1`):**
Expand All @@ -176,8 +197,8 @@ environment:
| `environment.viewport` | `environment.viewport` |
| `environment.device_pixel_ratio` | `environment.device_pixel_ratio` |
| `environment.os` | `environment.os` |
| `environment.commit` | `tested_commit`(トップレベルフィールド) |
| `environment.commit_unknown_reason` | `tested_commit_unknown_reason`(トップレベル) |
| `app_under_test.commit` | `tested_commit`(トップレベルフィールド) |
| `app_under_test.commit_unknown_reason` | `tested_commit_unknown_reason`(トップレベル、SHA注入成功時は absent) |

### Chrome でブラウザバージョンが `unknown` になる場合

Expand Down Expand Up @@ -374,14 +395,17 @@ scenarios:
tested_commit: "abc1234def5678..."
tested_commit_unknown_reason: null

# GitHub Pages / PR Preview の場合: commit SHA を取得できないときは以下を明示
tested_commit: "unknown"
tested_commit_unknown_reason: "GitHub Pages では実行時に commit SHA を取得できないため"
# GitHub Pages / PR Preview の場合(通常系): Evidence Panel の app_under_test.commit に 40文字 SHA が入るため、それを転記する
tested_commit: "3d643396c8144438148a8fe55727cc30c8aed417" # app_under_test.commit の値をそのまま使う

# GitHub Pages / PR Preview の場合(fallback): commit が "unknown" のときのみ以下を明示
# tested_commit: "unknown"
# tested_commit_unknown_reason: "VITE_LOOP_COMMIT_SHA が未注入のため(deploy workflow を確認すること)"
```

`tested_commit` を空欄のままにすることは禁止。commit SHA が不明な場合は必ず `"unknown"` + `tested_commit_unknown_reason` を記録する。

Evidence Panel の `environment.commit` フィールドが `unknown` になった場合も同様に `tested_commit_unknown_reason` を明記する。
Evidence Panel の `app_under_test.commit` フィールドが 40文字 SHA の場合はそれを `tested_commit` に転記する。`"unknown"` の場合のみ `tested_commit_unknown_reason` を明記する。

### 必須フィールド一覧

Expand Down
60 changes: 52 additions & 8 deletions src/ui/playtestEvidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export interface BrowserInfo {

export interface AppUnderTest {
name: string
commit: 'unknown'
commit_unknown_reason: string
commit: string
commit_unknown_reason?: string
}

export interface EnvironmentInfo {
Expand All @@ -88,6 +88,55 @@ export interface PlaytestEvidenceData {
hashes: Record<string, string>
}


// ---------------------------------------------------------------------------
// App Under Test commit resolution (AC3, AC4, AC5)
// ---------------------------------------------------------------------------

const SHA_REGEX = /^[0-9a-f]{40}$/

/**
* Resolve the commit SHA for the app under test.
*
* AC3: Returns 40-char hex SHA when VITE_LOOP_COMMIT_SHA is injected at build time.
* AC4: Falls back to commit: "unknown" with commit_unknown_reason when unset or invalid.
* AC5: commit_unknown_reason key is absent (object shape branched) on success.
*/
/**
* @param overrideEnvValue - Injectable for unit testing only. Pass undefined in production.
* In production the value comes from import.meta.env.VITE_LOOP_COMMIT_SHA (Vite build-time replace).
*/
export function resolveAppUnderTestCommit(overrideEnvValue?: string | null): AppUnderTest {
// import.meta.env.VITE_LOOP_COMMIT_SHA is replaced at build time by Vite.
// overrideEnvValue is used only in unit tests to inject a controlled value.
const raw: string | undefined =
overrideEnvValue !== undefined && overrideEnvValue !== null
? overrideEnvValue
: typeof import.meta !== 'undefined' && import.meta.env
? import.meta.env.VITE_LOOP_COMMIT_SHA
: undefined

if (raw !== undefined && SHA_REGEX.test(raw)) {
// AC5: object shape has no commit_unknown_reason key on success
return {
name: 'loop-protocol',
commit: raw,
}
}

// AC4: fallback branch — include commit_unknown_reason
const reason =
raw === undefined
? 'VITE_LOOP_COMMIT_SHA was not set at build time'
: `VITE_LOOP_COMMIT_SHA value "${raw}" is not a valid 40-character hex SHA`

return {
name: 'loop-protocol',
commit: 'unknown',
commit_unknown_reason: reason,
}
}

// ---------------------------------------------------------------------------
// Browser info collection (AC3, AC4)
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -268,12 +317,7 @@ export function buildEvidenceData(): PlaytestEvidenceData {

const source_url = typeof location !== 'undefined' ? location.href : 'unknown'

const app_under_test: AppUnderTest = {
name: 'loop-protocol',
commit: 'unknown',
commit_unknown_reason:
'GitHub Pages / PR Preview 上のブラウザからは build-time commit SHA を確定できません。commit SHA の注入には別途 workflow 対応(別 Issue)が必要です。',
}
const app_under_test = resolveAppUnderTestCommit()

const browser = collectBrowserInfo()

Expand Down
14 changes: 14 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
/**
* Build-time commit SHA injected by the deploy workflow via `git rev-parse HEAD`.
* When set, must be a 40-character lowercase hex string.
* Absent (undefined) in local dev builds unless explicitly set.
*/
readonly VITE_LOOP_COMMIT_SHA?: string
}

interface ImportMeta {
readonly env: ImportMetaEnv
}
81 changes: 76 additions & 5 deletions tests/playtest-evidence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
toYaml,
shouldShowPanel,
initPlaytestEvidencePanel,
resolveAppUnderTestCommit,
type PlaytestEvidenceData,
} from '../src/ui/playtestEvidence'

Expand All @@ -43,12 +44,19 @@ describe('buildEvidenceData', () => {
expect(isNaN(d.getTime())).toBe(false)
})

// AC8: app_under_test.commit is unknown with reason
it('GIVEN Pages context WHEN buildEvidenceData THEN app_under_test.commit is "unknown" with reason', () => {
// AC4: app_under_test.commit fallback when VITE_LOOP_COMMIT_SHA is not set
it('GIVEN VITE_LOOP_COMMIT_SHA is not set at build time WHEN buildEvidenceData THEN app_under_test.commit is "unknown" with reason', () => {
const data = buildEvidenceData()
expect(data.app_under_test.commit).toBe('unknown')
expect(typeof data.app_under_test.commit_unknown_reason).toBe('string')
expect(data.app_under_test.commit_unknown_reason.length).toBeGreaterThan(0)
// In the test environment import.meta.env.VITE_LOOP_COMMIT_SHA is not set
// so we expect the fallback behavior
if (data.app_under_test.commit === 'unknown') {
expect(typeof data.app_under_test.commit_unknown_reason).toBe('string')
expect((data.app_under_test.commit_unknown_reason ?? '').length).toBeGreaterThan(0)
} else {
// SHA was injected — it must be a 40-char hex string
expect(data.app_under_test.commit).toMatch(/^[0-9a-f]{40}$/)
expect('commit_unknown_reason' in data.app_under_test).toBe(false)
}
})

// AC3: OS/platform
Expand Down Expand Up @@ -136,6 +144,69 @@ describe('buildEvidenceData', () => {
})
})


// --- AC3, AC4, AC5: resolveAppUnderTestCommit ---
// Note: import.meta.env is statically replaced by Vite at build time and cannot be mutated in
// tests. Instead, resolveAppUnderTestCommit accepts an optional overrideEnvValue parameter for
// unit test injection. Production code calls resolveAppUnderTestCommit() with no argument.
describe('resolveAppUnderTestCommit', () => {
// AC3: valid SHA injection
it('GIVEN VITE_LOOP_COMMIT_SHA is a valid 40-char hex SHA WHEN resolveAppUnderTestCommit THEN commit is the SHA and commit_unknown_reason is absent', () => {
const validSha = 'a'.repeat(40)
const result = resolveAppUnderTestCommit(validSha)
expect(result.commit).toBe(validSha)
expect('commit_unknown_reason' in result).toBe(false)
})

// AC4: fallback when not set (null = explicit "not set" sentinel)
it('GIVEN VITE_LOOP_COMMIT_SHA is undefined WHEN resolveAppUnderTestCommit THEN commit is "unknown" and commit_unknown_reason is present', () => {
const result = resolveAppUnderTestCommit(null)
expect(result.commit).toBe('unknown')
expect(typeof result.commit_unknown_reason).toBe('string')
expect((result.commit_unknown_reason ?? '').length).toBeGreaterThan(0)
})

// AC4: fallback when value is not 40-char hex
it('GIVEN VITE_LOOP_COMMIT_SHA is an invalid value WHEN resolveAppUnderTestCommit THEN commit is "unknown" and commit_unknown_reason mentions the invalid value', () => {
const result = resolveAppUnderTestCommit('not-a-sha')
expect(result.commit).toBe('unknown')
expect(typeof result.commit_unknown_reason).toBe('string')
expect(result.commit_unknown_reason).toContain('not-a-sha')
})

// AC5: commit_unknown_reason key is absent on success (object shape)
it('GIVEN valid SHA WHEN resolveAppUnderTestCommit THEN object does NOT have commit_unknown_reason key at all', () => {
const validSha = '0123456789abcdef'.repeat(2) + '01234567'
const result = resolveAppUnderTestCommit(validSha)
expect(Object.prototype.hasOwnProperty.call(result, 'commit_unknown_reason')).toBe(false)
})

// AC3: 40-char hex validation — exactly 40 chars of 0-9a-f
it('GIVEN a 39-char hex string WHEN resolveAppUnderTestCommit THEN falls back to unknown', () => {
const result = resolveAppUnderTestCommit('a'.repeat(39))
expect(result.commit).toBe('unknown')
})

// AC3: uppercase hex should not match (must be lowercase)
it('GIVEN a 40-char uppercase hex string WHEN resolveAppUnderTestCommit THEN falls back to unknown', () => {
const result = resolveAppUnderTestCommit('A'.repeat(40))
expect(result.commit).toBe('unknown')
})

// AC3: valid boundary — exactly 40 lowercase hex chars
it('GIVEN exactly 40 lowercase hex chars WHEN resolveAppUnderTestCommit THEN commit matches the SHA', () => {
const validSha = '0123456789abcdef0123456789abcdef01234567'
const result = resolveAppUnderTestCommit(validSha)
expect(result.commit).toBe(validSha)
})

// AC3: 41-char hex string should not match
it('GIVEN a 41-char hex string WHEN resolveAppUnderTestCommit THEN falls back to unknown', () => {
const result = resolveAppUnderTestCommit('a'.repeat(41))
expect(result.commit).toBe('unknown')
})
})

// --- AC8: YAML output ---
describe('toYaml', () => {
it('GIVEN a PlaytestEvidenceData WHEN toYaml is called THEN output starts with schema version line', () => {
Expand Down
Loading