ReviewOS

also looking at this

craft-native/craft

feat(ios): scheduleNotification — the deferral that expired on its own, ratchet 26 → 25

#109
Closed chris wants to merge feat/ios-schedule-notification into main
6 files +922 -37

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

.github/workflows/ci.ymlmodified+29-1
Changes to .github/workflows/ci.yml
@@ -109,6 +109,34 @@ jobs:
109109 working-directory: packages/typescript
110110 run: bun run build
111111
112 # `packages/ios` had a test suite that nothing ran. That is how its
113 # generator kept a defect nobody saw: `init()` read `CRAFT_IOS_RUNTIME`
114 # straight from the environment, so the suite passed 12/12 in a clean shell
115 # and 8/12 in a developer's, and CI never had an opinion either way.
116 ios-builder:
117 runs-on: ubuntu-latest
118
119 steps:
120 - uses: actions/checkout@v6
121
122 - name: Setup Pantry (provides Bun, Zig, etc.)
123 uses: pantry-pm/pantry/packages/action@235036fa0f48bae99b2293df5a3dc35c809b1777 # pinned: last SHA whose bundled typescript resolves on linux-x64
124
125 - name: Install dependencies
126 run: bun install
127
128 # Unset deliberately. The suite must not depend on it, and this is the
129 # job that would notice if it started to again.
130 - name: Test iOS builder
131 working-directory: packages/ios
132 run: bun test
133 env:
134 CRAFT_IOS_RUNTIME: ''
135
136 - name: Build iOS builder
137 working-directory: packages/ios
138 run: bun run build
139
112140 create-craft:
113141 runs-on: ubuntu-latest
114142
@@ -219,7 +247,7 @@ jobs:
219247 run: bun run verify
220248
221249 publish-commit:
222 needs: [zig-core, typescript-sdk, create-craft, ts-maps, lint, integration]
250 needs: [zig-core, typescript-sdk, ios-builder, create-craft, ts-maps, lint, integration]
223251 runs-on: ubuntu-latest
224252
225253 steps:
packages/ios/src/index.test.tsmodified+102-1
Changes to packages/ios/src/index.test.ts
@@ -5,6 +5,9 @@ import { describe, expect, it } from 'bun:test'
55import {
66 build,
77 init,
8 installRuntime,
9 renderRuntimeSettings,
10 resolveRuntimeDir,
811 orderSimulators,
912 renderBackgroundModes,
1013 renderEntitlements,
@@ -83,6 +86,7 @@ describe('Craft iOS builder', () => {
8386 it('generates a production project whose bundled index lives under dist', async () => {
8487 const output = mkdtempSync(join(tmpdir(), 'craft-ios-project-'))
8588 await init({
89 runtimeDir: null,
8690 name: 'WildLoop',
8791 bundleId: 'org.wildloop.app',
8892 output,
@@ -127,7 +131,7 @@ describe('Craft iOS builder', () => {
127131 const output = join(root, 'ios')
128132 Bun.spawnSync(['mkdir', '-p', web])
129133 writeFileSync(join(web, 'index.html'), '<main>Available offline</main>')
130 await init({ name: 'WildLoop', bundleId: 'org.wildloop.app', output })
134 await init({ runtimeDir: null, name: 'WildLoop', bundleId: 'org.wildloop.app', output })
131135 await build({ htmlPath: web, devServer: 'https://wildloop.org', output, generateProject: false })
132136
133137 const swift = readFileSync(join(output, 'Sources', 'WildLoopApp.swift'), 'utf8')
@@ -138,6 +142,7 @@ describe('Craft iOS builder', () => {
138142 it('generates a native Live Activity extension when enabled', async () => {
139143 const output = mkdtempSync(join(tmpdir(), 'craft-ios-live-activity-'))
140144 await init({
145 runtimeDir: null,
141146 name: 'WildLoop',
142147 bundleId: 'org.wildloop.app',
143148 output,
@@ -161,6 +166,7 @@ describe('Craft iOS builder', () => {
161166 it('generates an embedded watchOS companion when enabled', async () => {
162167 const output = mkdtempSync(join(tmpdir(), 'craft-ios-watch-'))
163168 await init({
169 runtimeDir: null,
164170 name: 'WildLoop',
165171 bundleId: 'org.wildloop.app',
166172 output,
@@ -243,3 +249,98 @@ describe('choosing a simulator', () => {
243249 expect(devices[0]?.name).toBe('iPad Air')
244250 })
245251})
252
253describe('Zig runtime installation', () => {
254 // A runtime directory holding *one* simulator slice, so `installRuntime`
255 // takes the `cpSync` branch. The two-slice branch shells out to `lipo`,
256 // which needs genuine Mach-O input and does not exist off macOS — it is
257 // covered by building a real generated app, not from here. What these do
258 // cover is everything around it: resolution, ordering, and the warning.
259 function fakeRuntime(archives = ['libcraft-ios.a', 'libcraft-ios-simulator-arm64.a']): string {
260 const dir = mkdtempSync(join(tmpdir(), 'craft-rt-'))
261 for (const a of archives) writeFileSync(join(dir, a), 'stand-in for an archive')
262 return dir
263 }
264
265 it('resolves an explicit directory, and null means no runtime whatever the environment says', () => {
266 const dir = fakeRuntime()
267 const saved = process.env.CRAFT_IOS_RUNTIME
268 process.env.CRAFT_IOS_RUNTIME = dir
269 try {
270 // The gap this closes: the suite used to inherit the developer's shell,
271 // and went from 12 passing to 8 passing and 4 failing when this variable
272 // happened to be set.
273 expect(resolveRuntimeDir(null)).toBeNull()
274 expect(resolveRuntimeDir(dir)).toBe(dir)
275 expect(resolveRuntimeDir()).toBe(dir)
276 }
277 finally {
278 if (saved === undefined) delete process.env.CRAFT_IOS_RUNTIME
279 else process.env.CRAFT_IOS_RUNTIME = saved
280 }
281 })
282
283 it('names the source in the error, so a bad path says which knob set it', () => {
284 expect(() => resolveRuntimeDir('/no/such/runtime')).toThrow(/runtimeDir is/)
285 })
286
287 it('writes one archive name per SDK, which is what a single -lcraft-ios needs', async () => {
288 const output = mkdtempSync(join(tmpdir(), 'craft-out-'))
289 expect(await installRuntime(output, fakeRuntime())).toBe(true)
290 expect(existsSync(join(output, 'Runtime', 'device', 'libcraft-ios.a'))).toBe(true)
291 expect(existsSync(join(output, 'Runtime', 'simulator', 'libcraft-ios.a'))).toBe(true)
292 })
293
294 it('warns when only one simulator slice is present, rather than shipping it silently', async () => {
295 const warnings: string[] = []
296 const saved = console.warn
297 console.warn = (...args: unknown[]) => void warnings.push(args.join(' '))
298 try {
299 await installRuntime(mkdtempSync(join(tmpdir(), 'craft-out-')), fakeRuntime())
300 }
301 finally {
302 console.warn = saved
303 }
304 // RUNTIME_ARCHIVES' own comment calls this the failure that "only shows up
305 // on someone else's laptop", so it must not be silent.
306 expect(warnings.join('\n')).toContain('libcraft-ios-simulator-x64.a')
307 })
308
309 it('leaves a working install alone when the source directory is incomplete', async () => {
310 // The ordering bug: validation ran after the wipe, so a bad runtime dir
311 // destroyed the archives already in place and left project.yml linking
312 // against a Runtime/ that no longer existed.
313 const output = mkdtempSync(join(tmpdir(), 'craft-out-'))
314 await installRuntime(output, fakeRuntime())
315 const installed = join(output, 'Runtime', 'device', 'libcraft-ios.a')
316 expect(existsSync(installed)).toBe(true)
317
318 const empty = mkdtempSync(join(tmpdir(), 'craft-rt-empty-'))
319 await expect(installRuntime(output, empty)).rejects.toThrow(/has none of/)
320 expect(existsSync(installed)).toBe(true)
321 })
322
323 it('renders both SDK search paths and forces the four entry points', () => {
324 const settings = renderRuntimeSettings()
325 expect(settings).toContain('LIBRARY_SEARCH_PATHS[sdk=iphoneos*]')
326 expect(settings).toContain('LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*]')
327 // Without -u the linker drops the whole archive as unreachable, because
328 // nothing in the Swift references these symbols — both seams use dlsym.
329 for (const sym of ['handle_action', 'set_webview', 'deliver_result', 'deliver_error']) {
330 expect(settings).toContain(`-Wl,-u,_craft_ios_${sym}`)
331 }
332 // Six-space indent: this is spliced into project.yml under `settings:`.
333 for (const line of settings.split('\n')) expect(line.startsWith(' ')).toBe(true)
334 })
335
336 it('generates a project whose settings match whether a runtime was installed', async () => {
337 const output = mkdtempSync(join(tmpdir(), 'craft-app-'))
338 await init({ runtimeDir: fakeRuntime(), name: 'HasRuntime', output })
339 expect(readFileSync(join(output, 'project.yml'), 'utf8')).toContain('-lcraft-ios')
340
341 // And re-running without one leaves no orphaned archives claiming otherwise.
342 await init({ runtimeDir: null, name: 'HasRuntime', output })
343 expect(readFileSync(join(output, 'project.yml'), 'utf8')).not.toContain('-lcraft-ios')
344 expect(existsSync(join(output, 'Runtime'))).toBe(false)
345 })
346})
packages/ios/src/index.tsmodified+60-10
Changes to packages/ios/src/index.ts
@@ -91,6 +91,20 @@ export interface InitOptions {
9191 teamId?: string
9292 output: string
9393 config?: Partial<CraftConfig>
94 /**
95 * Where the Zig runtime archives live, overriding `CRAFT_IOS_RUNTIME`.
96 *
97 * `null` means "no runtime, whatever the environment says" — the shape
98 * `AppConfig.craftPath` has for `CRAFT_BIN`, and the reason it exists here
99 * is the same: a caller that does not opt in should not be steered by an
100 * ambient variable. Without it this package's own tests inherited whatever
101 * the developer's shell exported, and went from 12 passing to 8 passing and
102 * 4 failing when `CRAFT_IOS_RUNTIME` happened to be set.
103 *
104 * Omitted (`undefined`) keeps the environment lookup, which is what the
105 * monorepo dev loop uses.
106 */
107 runtimeDir?: string | null
94108}
95109
96110export interface BuildOptions {
@@ -380,12 +394,18 @@ export function syncWebAssets(source: string, output: string): void {
380394 * dev loop, not a lookup path. Shipping the runtime to real apps means putting
381395 * these archives in the pantry package beside the `craft` binary, which is a
382396 * distribution decision this function does not make.
397 *
398 * `override` is what `InitOptions.runtimeDir` passes: a path to use instead of
399 * the variable, or `null` to declare there is no runtime regardless of what the
400 * environment says. `undefined` falls through to `CRAFT_IOS_RUNTIME`.
383401 */
384export function resolveRuntimeDir(): string | null {
385 const dir = process.env.CRAFT_IOS_RUNTIME
402export function resolveRuntimeDir(override?: string | null): string | null {
403 if (override === null) return null
404 const dir = override ?? process.env.CRAFT_IOS_RUNTIME
386405 if (!dir) return null
387406 if (!existsSync(dir)) {
388 throw new Error(`CRAFT_IOS_RUNTIME points at ${dir}, which does not exist.`)
407 const source = override === undefined ? 'CRAFT_IOS_RUNTIME points at' : 'runtimeDir is'
408 throw new Error(`${source} ${dir}, which does not exist.`)
389409 }
390410 return dir
391411}
@@ -411,23 +431,44 @@ const RUNTIME_ARCHIVES = {
411431 * Returns true when a runtime was installed.
412432 */
413433export async function installRuntime(output: string, runtimeDir: string): Promise<boolean> {
414 const dest = join(output, 'Runtime')
415 rmSync(dest, { recursive: true, force: true })
416
417 for (const [sdk, archives] of Object.entries(RUNTIME_ARCHIVES)) {
434 // Every SDK resolved before anything is written, and long before the
435 // `rmSync` below. The check used to run per-SDK *after* the wipe, so a
436 // runtime directory missing an archive destroyed a working install on its
437 // way to throwing, and left `project.yml` linking `-lcraft-ios` against a
438 // `Runtime/` that no longer existed.
439 const resolved = Object.entries(RUNTIME_ARCHIVES).map(([sdk, archives]) => {
418440 const present = archives.filter(a => existsSync(join(runtimeDir, a)))
419441 if (present.length === 0) {
420442 throw new Error(
421 `CRAFT_IOS_RUNTIME=${runtimeDir} has none of ${archives.join(', ')}. `
443 `${runtimeDir} has none of ${archives.join(', ')}. `
422444 + `Run \`zig build build-ios-all\` in packages/zig and point at its zig-out/lib.`,
423445 )
424446 }
447 return { sdk, archives, present }
448 })
425449
450 const dest = join(output, 'Runtime')
451 rmSync(dest, { recursive: true, force: true })
452
453 for (const { sdk, archives, present } of resolved) {
426454 const sdkDir = join(dest, sdk)
427455 mkdirSync(sdkDir, { recursive: true })
428456 const target = join(sdkDir, 'libcraft-ios.a')
429457
430458 if (present.length === 1) {
459 // Said out loud, because the comment on RUNTIME_ARCHIVES describes this
460 // exact outcome as the thing to avoid: a single-slice simulator archive
461 // links on the machine that built it and fails on the other kind, which
462 // is a break that only ever shows up on someone else's laptop. Copying
463 // is still better than refusing — a one-arch dev loop is legitimate —
464 // but it should not happen silently.
465 const missing = archives.filter(a => !present.includes(a))
466 if (missing.length > 0) {
467 console.warn(
468 ` ⚠ ${sdk}: only ${present[0]} was found; ${missing.join(', ')} is missing. `
469 + `The generated project will not link on the other architecture.`,
470 )
471 }
431472 cpSync(join(runtimeDir, present[0]!), target)
432473 }
433474 else {
@@ -533,9 +574,18 @@ export async function init(options: InitOptions): Promise<void> {
533574
534575 // The Zig runtime, when this build has one. Installed before the project is
535576 // rendered because the settings below name the directories it creates.
536 const runtimeDir = resolveRuntimeDir()
577 const runtimeDir = resolveRuntimeDir(options.runtimeDir)
537578 const hasRuntime = runtimeDir ? await installRuntime(output, runtimeDir) : false
538 if (hasRuntime) console.log(' Linked the Zig runtime from', runtimeDir)
579 if (hasRuntime) {
580 console.log(' Linked the Zig runtime from', runtimeDir)
581 }
582 else {
583 // Re-running init without a runtime used to strip the link settings from
584 // project.yml and leave the archives behind, so the directory said one
585 // thing and the project said another. Whatever this run decides, the tree
586 // agrees with it.
587 rmSync(join(output, 'Runtime'), { recursive: true, force: true })
588 }
539589
540590 const projectYml = projectYmlTemplate
541591 .replace(/\{\{CRAFT_RUNTIME_SETTINGS\}\}/g, hasRuntime ? renderRuntimeSettings() : '')