Skip to content

Latest commit

 

History

History
57 lines (44 loc) · 12.6 KB

AGENTS.md

File metadata and controls

57 lines (44 loc) · 12.6 KB

AGENTS.md

learnX is a React Native app (iOS, iPadOS, Mac Catalyst, Android, and an Electron build for Windows and Linux) for Tsinghua University's Web Learning, built on @robertying/thu-learn-lib. This file holds what applies everywhere. Folders with more to know have their own AGENTS.md (git ls-files '*AGENTS.md'), and the workflows are skills in .claude/skills. CONTRIBUTING.md is the setup guide for a fresh clone, and patches/README.md says why each dependency is patched.

Commands

  • package.json's scripts are the entry points: Metro, the platform builds, the checks, the E2E runs, the Electron app and the release helpers.
  • cd ios && bundle exec pod install installs the pods with the CocoaPods version locked in Gemfile.lock (run bundle install once first); run it after ios/Podfile, a native package or a patch changes. pnpm pod:update also runs pod update, which moves pods to newer versions: use it only when upgrading (the upgrade-dependencies skill). Use the Ruby in .ruby-version with a gem folder you can write to, such as Homebrew's.
  • The Xcode targets' folders are synchronized folders: a file added there joins its target with no project change. Files a target must not copy as resources, and files one target compiles from elsewhere, are in the project's exception sets.
  • Run pnpm format (Oxfmt) after changing files. pnpm check must pass before committing; it is what CI's JS / Checks runs. Lint is type-aware: mark an intentionally unawaited promise with void.
  • User-visible text goes in src/assets/translations (wording rules in its AGENTS.md), read with t('key', { param }) from helpers/i18n. Dates and times are translations too, written out with the formatters in the same file. The learnx/no-han-outside-translations lint rule rejects Chinese text elsewhere in src; disable it per line only for text that must not be translated, with the reason.
  • Debug builds load JS from Metro; only native changes need a rebuild. scripts/checks/test-ios.sh and scripts/checks/test-android.sh run the native tests (modules/AGENTS.md).
  • .env (git-ignored, see .env.example) is inlined at build time by babel/inline-env.js, which passes only the keys it lists: add a key there before reading it in the app. Keys it doesn't list (the live test login) never reach a bundle. android/local.properties (git-ignored, see its .example) holds the Android release signing values; debug builds don't need it. The app can be used without a Tsinghua account through the mock login (src/data/AGENTS.md).

File names

  • scripts/ is grouped by purpose (e2e/, checks/, ci/, release/, xcode/, dev/ for a developer's own machine, and lib/ for what scripts of several groups source), with their Jest tests in scripts/__tests__. Scripts cd to the repo root, so paths inside them start there.
  • In src and jest, a file is named after what it exports: PascalCase for a component, screen or navigator and for a default-exported constant object; camelCase for hooks, helpers, data, services and files with several exports. Folders are lowercase. Tests are <module>.test.ts(x) in the folder's __tests__ (<module>.<case>.test.ts for a case that needs a fresh module); one table of cases over sibling screens is named for the group.
  • Platform variants use React Native's platform extensions: Name.tsx for iOS and Mac, Name.android.tsx for Android, Name.web.tsx for the Electron app (a React Native Web build), and Name.shared.ts for what every platform uses. Never put the platform in the name itself (NameAndroid.tsx).
  • Code lives with its feature, and the platform shows in the file extension: the Electron app's version of a component, hook or service is Name.web.ts(x) next to the phones' Name.ts(x), which is a no-op where the phones have none, so Metro never puts it in a phone's bundle. An Electron-only module that only .web files import is named for the Electron app. A one-off call into Electron-only code from shared code may instead require it inside an isElectron() branch, which release builds fold away (babel/inline-platform.js; scripts/checks/check-bundle-platform.mjs checks). src/web holds only what has no feature to live with, and src/web/packages only the stand-ins for npm packages that src/web/aliases.json names. The Electron app's names say electron, never desktop; user-visible text may say desktop.
  • In code, check the platform with isApple() (iOS, iPadOS and Mac Catalyst), isAndroid() and isElectron() from helpers/platform, not Platform.OS. Ask for the platform a branch is for (isApple() for an Apple API or design), not for "not Android": a platform added later then takes neither by accident.
  • Scripts, Maestro flows, mock files and patches are kebab-case. Swift and Kotlin files are PascalCase.
  • Names set by something else stay as they are: Android resources and the icons in src/assets/icons are snake_case, ios/ExpoWidgetsTarget/index.swift matches what the expo-widgets config plugin generates, and docs/PRIVACY_POLICY_*.md are linked from the store listings.

React Compiler

  • babel-plugin-react-compiler memoizes every component and hook under src/, so don't add useMemo, useCallback or React.memo for performance.
  • Don't make an effect depend on a function. A function that an effect only calls goes in useEffectEvent, and the effect lists the data it reacts to. The compiler's memoization is an optimization, not a guarantee, so an effect shouldn't rerun just because a function fell out of compilation. react-hooks/exhaustive-deps flags the direct cases.
  • useCallback / useMemo are still right where a value has to keep its identity and can't be an effect event: handlers handed to navigation.setOptions from an effect, values in a context provider, and hook outputs that callers use as effect deps.
  • The compiler silently skips a function it can't handle and leaves it unmemoized, for example try blocks with finally, throw, or conditional, logical or optional-chaining expressions: compute values before the try and keep only the awaited calls inside it. Constants named use… are taken for hooks, so don't use that prefix for non-hooks.

Over-the-air updates

  • The Electron app takes no over-the-air updates: its web build ships inside the installer (electron/AGENTS.md). The rest of this section is about iOS, iPadOS, Mac and Android.
  • expo-updates replaces the JS bundle without a store release, from an update server that speaks the Expo Updates protocol. Release builds bundle with expo export:embed and Metro uses Expo's config, so a store build's bundle and an update are made the same way.
  • app.json holds the update URL, the request headers and the runtime version, and certs/ota-certificate.pem the code-signing certificate (the private key is never in the repo). Builds write them into each platform's native config, and scripts/__tests__/otaConfig.test.ts keeps the two platforms in step. The release build lanes refuse the placeholder URL.
  • A build takes only updates published for its exact runtime version, whatever its build number. The runtime version is the app version of the first release with the current native code: a release that changes only JS keeps it, so one update reaches every version since. Set it to the new app version in any release that changes native code, which is more than Swift and Kotlin: native packages and patches, build settings, Info.plist, String Catalogs, icons, the widgets, and a persisted store's version (src/data/storeVersions.json). JS that calls a native API an older binary lacks also needs a store release.
  • The app version on main is the coming release's, on every platform (package.json, the Xcode project's MARKETING_VERSION, versionName in android/app/build.gradle; the Electron app reads package.json), and the runtime version moves with it: the pull request that sets a new runtime version also moves the app version to it (pnpm bump minor, or patch or major). A version moves only once it has shipped to production (its v<version> tag, which publishing the GitHub release makes): until then every build of the coming release, TestFlight and Play betas included, keeps it, and so does the runtime version. Build numbers are never committed: the release lanes set them per build (the release skill).
  • pnpm ota:check enforces this against the native fingerprints recorded in ota-fingerprint.json (what counts as native is in fingerprint.config.js); its header says what it checks, and on a failure it prints the values to record. Record CI's values, from a clean checkout, since a local one can differ. So the first native change after a release sets the runtime version and the app version to the coming release's version, and later ones in that cycle only update the record.
  • The native side checks on every cold launch and downloads in the background; the update starts at the next launch. Coming back after a long time in the background checks again, or starts an update already downloaded (updateOnReturn in services/ota), run first by hooks/useReLogin so a reload never cuts off a login or a fetch. Each window opens again at the tab and item it showed: services/windows saves every window's link just before the reload (keepWindowLinksForReload), and hooks/useLinkTaps opens it once in place of the launch link (takeReloadLink). Debug builds load from Metro and never use updates. Crash reports carry the running update's id, which says which source map decodes them.
  • E2E builds never ask the real server: they get their own runtime version, a local update server and a test certificate. scripts/e2e/e2e-ota.sh runs the update flow (.maestro/AGENTS.md).

Unit tests (Jest)

  • src/helpers holds logic with no side effects beyond thin platform reads, tested without mocks of the server or the system. Code that talks to the system or to a server outside data/source lives in src/services.
  • @testing-library/react-native's render, renderHook, rerender and act are async: await them.
  • Mock native modules and the data layer per test file; use the real Zustand stores (setSetting, resetUserData, …) instead of mocking them. Global mocks and native view stand-ins are set up in jest/setup.ts for the phones and jest/web/setup.ts for the Electron app, both on jest/common.ts, and in jest/mocks/: read them before testing something that renders a native view. Jest clears mocks and restores spies after each test, and jest/setupAfterEnv.ts resets timers, the language and the clock format, so switch platforms with jest.replaceProperty(Platform, 'OS', 'android'), timers with jest.useFakeTimers() and the language with mockLocale.current (test-utils/locale), and don't restore by hand.
  • A test fails on any console.error or console.warn it doesn't expect (jest/setupAfterEnv.ts), Testing Library's own warnings included: change a store that a rendered component reads inside await act(...), in afterEach too, and lay views out with fireLayout from test-utils/layout. A test that expects a log spies on it.
  • Test hooks that hold real logic (state, ordering, error paths), not thin wrappers around one call.
  • Components and screens are tested with render and screen, one file per component or screen named after it; shared test data goes in jest/fixtures/. Tests import from jest/ as test-utils/…, never by a relative path. Query what the user sees (text, role, testID); press through userEvent, since fireEvent.press can bubble to the test element's own onPress prop. Screens get their props from screenProps (test-utils/screenProps) and mock their data hooks.
  • jest.config.js has a project per platform, each loading that platform's files, so an Android or Electron variant is tested in an __tests__/android/ or __tests__/web/ folder; the tooling project runs scripts/__tests__ and the Electron main process's tests.
  • CI fails when coverage drops below coverageThreshold in jest.config.js (kept just under the current numbers). Cover new code with tests, and raise the thresholds when coverage grows.

Pull requests

  • One change per pull request, squash-merged once the required checks pass. The title and description say what the change does compared with main and why, for a reader who hasn't seen how it came about: no history of earlier attempts or review rounds. The squash commit message follows the same rule.
  • Docs and comments say why, and what isn't plain from the code. They don't restate what the code shows, or keep lists (of files, scripts, flows, versions, counts) that a change elsewhere would leave stale: point to where the list lives instead.