Compare commits

..

2 Commits

Author SHA1 Message Date
665b0bf935 Stop two keyboard tests hanging, and make the CI mirror mirror CI
Some checks are pending
pre-commit / pre-commit (push) Waiting to run
Tests / test (3.10) (push) Waiting to run
Tests / test (3.11) (push) Waiting to run
Tests / test (3.12) (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
Two failures, both pre-existing and both invisible as failures — each
reported only as "did not complete" after stalling the suite.

app/test/keyboard_operability_test.dart awaited writeLog() straight from
the test body. LogStorageService is backed by a FileDocumentStore there,
so that awaits real dart:io, and the fake-async zone never completes it:
the test hangs rather than fails. Seeding now happens inside runAsync
alongside the pumping, which is the only zone where that write can
resolve. The screen's own initState load needs the same real clock, and
pumpAndSettle() cannot be used to wait for it — inside runAsync it waits
on a frame scheduler this screen never reports as idle — so the helper
pumps a bounded number of times instead. 473 app tests now pass in 14s.

scripts/ci_mirror.sh ran bare pytest while python-tests.yml runs it under
xvfb-run. Without a display the gate's real-Tk tests collapse: the focus
ring has nothing to walk, so three TestRealFocusRing tests fail locally
and pass in CI. The mirror was reporting red on a green tree, which is
worse than not mirroring at all. It now uses the workflow's exact
xvfb-run invocation and fails loudly if xvfb-run is missing.

Neither change touches production code: the delete control and its
id-gate were already correct, and the Python suite already passed under a
display (609 passed, 100% coverage).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014dzgcZxmpS3VtmLci2sbxj
2026-08-04 21:50:15 +02:00
5f34ba6139 Build and publish a signed APK on every push to main
diet-guard had no APK build at all; releases were not produced. Adds a
standalone release-apk.yml plus the release signing config the app was
missing.

Signing follows the pattern proven in home_inventory: gradle reads
android/key.properties when present and falls back to the debug key when
absent, so local `flutter run --release` is unaffected. CI writes that
file from repository secrets and shreds it afterwards, and apksigner
fails the build closed if a debug-signed APK ever appears.

Only the signingConfig line changes here: isMinifyEnabled, isShrinkResources
and proguardFiles are left exactly as they were.

This repo has no Flutter analyze/test job, so the release job is not
gated on tests — a broken commit still publishes, with the build failing
as the only backstop. Add `needs:` if a Flutter CI job is added later.

versionCode is github.run_number, which is per-workflow-file — this file
must never be renamed, or the counter resets and updates stop installing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014dzgcZxmpS3VtmLci2sbxj
2026-08-04 20:55:14 +02:00
4 changed files with 219 additions and 20 deletions

152
.github/workflows/release-apk.yml vendored Normal file
View File

@ -0,0 +1,152 @@
name: release-apk
on:
push:
branches: [main]
# Lets a release be rebuilt from the current main without a dummy commit.
workflow_dispatch:
# gh release create needs write access to the repo's contents.
permissions:
contents: write
# Releases are tag-anchored; two concurrent runs would race on the same tag.
concurrency:
group: release-apk-${{ github.ref }}
cancel-in-progress: false
# NOTE: versionCode is github.run_number, which is per-workflow-FILE. Never
# rename or replace this file — the counter resets to 1, versionCode goes
# backwards, and Android silently refuses every future update.
#
# This job is NOT gated on a test suite: this repo has no Flutter analyze/test
# job to depend on, so a broken commit still publishes. The build failing is
# the only backstop. Add `needs:` here if a Flutter CI job is ever added.
jobs:
release:
name: release (APK)
runs-on: ubuntu-latest
defaults:
run:
working-directory: app
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: "17"
- uses: subosito/flutter-action@v2
with:
flutter-version: 3.44.8
channel: stable
cache: true
- name: Read version from pubspec
id: ver
run: |
set -euo pipefail
# 1.0.0+1 -> 1.0.0; the +build part is replaced by the run number.
raw=$(grep -m1 -E '^version:' pubspec.yaml | sed -E 's/^version:[[:space:]]*//')
semver=${raw%%+*}
if ! echo "$semver" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
echo "::error::pubspec version '$raw' is not X.Y.Z"; exit 1
fi
echo "semver=$semver" >> "$GITHUB_OUTPUT"
# One step, no echoes, printf-into-file rather than echo: the key must
# never reach the log. storeFile is ABSOLUTE because key.properties is
# read from android/ while file() resolves against android/app/.
- name: Materialise the release keystore
env:
KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
STORE_PASS: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
KEY_PASS: ${{ secrets.ANDROID_KEY_PASSWORD }}
run: |
set -euo pipefail
if [ -z "$KEYSTORE_B64" ]; then
echo "::error::ANDROID_KEYSTORE_BASE64 is not set; refusing to publish."
exit 1
fi
umask 077
ks="$GITHUB_WORKSPACE/app/android/app/kuhy-release.jks"
printf '%s' "$KEYSTORE_B64" | base64 -d > "$ks"
{
printf 'storeFile=%s\n' "$ks"
printf 'storePassword=%s\n' "$STORE_PASS"
printf 'keyAlias=%s\n' "$KEY_ALIAS"
printf 'keyPassword=%s\n' "$KEY_PASS"
} > "$GITHUB_WORKSPACE/app/android/key.properties"
ls -l "$ks"
- run: flutter pub get
- name: Build signed APK
run: |
flutter build apk --release \
--build-name="${{ steps.ver.outputs.semver }}" \
--build-number="${{ github.run_number }}"
# Fail closed. `findByName("release") ?: debug` silently falls back to
# the debug key if key.properties is misplaced, producing a green build
# that can never be installed over. This is the check that catches it.
- name: Verify the APK is release-signed
run: |
set -euo pipefail
apk=build/app/outputs/flutter-apk/app-release.apk
signer=$(ls -d "$ANDROID_SDK_ROOT"/build-tools/* | sort -V | tail -1)/apksigner
"$signer" verify --print-certs "$apk" | tee /tmp/certs.txt
if grep -qi 'CN=Android Debug' /tmp/certs.txt; then
echo "::error::APK is debug-signed; not publishing."; exit 1
fi
if ! grep -q 'CN=kuhy' /tmp/certs.txt; then
echo "::error::APK is not signed with the kuhy release key."; exit 1
fi
- name: Shred the signing material
if: always()
run: |
rm -f "$GITHUB_WORKSPACE/app/android/key.properties" \
"$GITHUB_WORKSPACE/app/android/app/kuhy-release.jks"
- name: Name the asset
id: art
run: |
set -euo pipefail
out="diet-guard-${{ steps.ver.outputs.semver }}-build.${{ github.run_number }}.apk"
cp build/app/outputs/flutter-apk/app-release.apk "$out"
echo "path=$out" >> "$GITHUB_OUTPUT"
- name: Publish the per-commit prerelease
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
tag="v${{ steps.ver.outputs.semver }}-build.${{ github.run_number }}"
gh release create "$tag" "${{ steps.art.outputs.path }}" \
--prerelease \
--target "${{ github.sha }}" \
--title "$tag" \
--notes "Automated build of ${{ github.sha }} (versionCode ${{ github.run_number }})."
# Cut a clean vX.Y.Z the first time a pubspec version is seen. A failing
# `gh release view` is the "not published yet" signal, which also covers
# the first-ever run on a repo with no releases.
- name: Publish the clean version release
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
tag="v${{ steps.ver.outputs.semver }}"
if gh release view "$tag" >/dev/null 2>&1; then
echo "$tag already exists; leaving it as-is."; exit 0
fi
if git rev-parse -q --verify "refs/tags/$tag" >/dev/null; then
echo "Tag $tag exists without a release; refusing to clobber."; exit 0
fi
gh release create "$tag" "${{ steps.art.outputs.path }}" \
--target "${{ github.sha }}" \
--title "$tag" \
--notes "Release $tag (versionCode ${{ github.run_number }})."

View File

@ -1,9 +1,21 @@
import java.util.Properties
plugins {
id("com.android.application")
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id("dev.flutter.flutter-gradle-plugin")
}
// Release signing key, kept out of the repo. CI writes key.properties from
// repository secrets; locally the file is absent and the build falls back to
// the debug key so `flutter run --release` still works. Every release APK is
// signed with the one shared key, so updates install over each other instead
// of forcing an uninstall.
val keystoreProperties = Properties().apply {
val f = rootProject.file("key.properties")
if (f.exists()) f.inputStream().use { load(it) }
}
android {
namespace = "com.kuhy.diet_guard_app"
compileSdk = flutter.compileSdkVersion
@ -28,11 +40,23 @@ android {
versionName = flutter.versionName
}
signingConfigs {
if (keystoreProperties.getProperty("storeFile") != null) {
create("release") {
storeFile = file(keystoreProperties.getProperty("storeFile"))
storePassword = keystoreProperties.getProperty("storePassword")
keyAlias = keystoreProperties.getProperty("keyAlias")
keyPassword = keystoreProperties.getProperty("keyPassword")
}
}
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig = signingConfigs.getByName("debug")
// Falls back to the debug key only when key.properties is absent
// (a fresh clone or a local run), so `flutter run --release` works.
signingConfig = signingConfigs.findByName("release")
?: signingConfigs.getByName("debug")
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(

View File

@ -157,7 +157,7 @@ void main() {
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await LogStorageService.instance.writeLog({
await _pumpHistoryScreen(tester, {
'2026-07-30': [
const FoodEntry(
id: 'has-id',
@ -173,14 +173,6 @@ void main() {
],
});
// HistoryScreen loads via a fire-and-forget Future in initState that the
// frame scheduler does not track, so settle inside runAsync.
await tester.runAsync(() async {
await tester.pumpWidget(const MaterialApp(home: HistoryScreen()));
await Future<void>.delayed(const Duration(milliseconds: 50));
await tester.pumpAndSettle();
});
// Delete used to be bound only to onLongPress, which has no keyboard
// equivalent whatsoever.
expect(
@ -198,7 +190,7 @@ void main() {
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await LogStorageService.instance.writeLog({
await _pumpHistoryScreen(tester, {
'2026-07-30': [
const FoodEntry(
time: '2026-07-30T09:00:00+02:00',
@ -213,12 +205,6 @@ void main() {
],
});
await tester.runAsync(() async {
await tester.pumpWidget(const MaterialApp(home: HistoryScreen()));
await Future<void>.delayed(const Duration(milliseconds: 50));
await tester.pumpAndSettle();
});
// Delete stays id-only to avoid ambiguous time+desc matches, so the
// control must be absent rather than present-and-failing.
expect(find.byTooltip('Delete entry'), findsNothing);
@ -228,3 +214,27 @@ void main() {
/// A do-nothing month-navigation callback.
void _noop() {}
/// Seed [log] to disk, then pump [HistoryScreen] until its load has painted.
///
/// Both halves must happen inside `runAsync`, and that is the whole point of
/// this helper. `LogStorageService` here is backed by a `FileDocumentStore`,
/// so `writeLog` awaits real `dart:io`; awaited from the test body it waits
/// on a `Future` the fake-async zone never completes, and the test hangs
/// rather than failing -- reported only as "did not complete" once the suite
/// times out. `HistoryScreen` then loads through a fire-and-forget `Future`
/// in `initState`, which needs the same real clock to resolve.
///
/// The pumping is a bounded manual loop rather than `pumpAndSettle()`, which
/// inside `runAsync` waits on a frame scheduler this screen never reports as
/// idle -- another hang, with the same unhelpful symptom.
Future<void> _pumpHistoryScreen(WidgetTester tester, DayLog log) async {
await tester.runAsync(() async {
await LogStorageService.instance.writeLog(log);
await tester.pumpWidget(const MaterialApp(home: HistoryScreen()));
for (var i = 0; i < 10; i++) {
await Future<void>.delayed(const Duration(milliseconds: 20));
await tester.pump();
}
});
}

View File

@ -85,7 +85,20 @@ run_precommit_all_files() {
run_pytest_clean_venv() {
log "pytest in the clean venv (mirrors the Tests workflow)"
"$VENV_DIR/bin/python" -m pytest "$@" || fail "pytest (clean requirements.txt venv)"
# Under xvfb-run, exactly as python-tests.yml does. The gate's real-Tk
# tests walk a focus ring via tk_focusNext, which consults mapped-ness
# and real focus: on a developer's live display the window never takes
# focus and the ring collapses to one widget, so TestRealFocusRing fails
# here while passing in CI. Mirroring the workflow's display is the whole
# point of this script -- without it the mirror reports red on a green
# tree, which is worse than not mirroring at all.
if ! command -v xvfb-run >/dev/null 2>&1; then
fail "xvfb-run is not on PATH (install xorg-server-xvfb); the real-Tk
gate tests need the same virtual display the Tests workflow gives them"
fi
xvfb-run -a -s "-screen 0 1600x1200x24" \
"$VENV_DIR/bin/python" -m pytest "$@" \
|| fail "pytest (clean requirements.txt venv)"
}
# Run flutter with the calling git hook's environment stripped.