also looking at this
fix(ios): hand the page the answers native was already sending
#222
18 files
+597
-164
| @@ -0,0 +1,147 @@ | ||
| 1 | import { readFileSync } from 'node:fs' | |
| 2 | import { join } from 'node:path' | |
| 3 | import { describe, expect, it } from 'bun:test' | |
| 4 | ||
| 5 | // The script CraftApp.swift injects into every page, run here against a fake | |
| 6 | // native side. The E2E suite runs it on a simulator too, but with one config | |
| 7 | // per run, so a path that needs a capability both on and off is checked here. | |
| 8 | ||
| 9 | const template = readFileSync(join(import.meta.dir, '../templates/CraftApp.swift'), 'utf8') | |
| 10 | ||
| 11 | /** The page script, as the string Swift evaluates, with every flag off. */ | |
| 12 | function pageScript(): string { | |
| 13 | const opening = 'let script = """\n window.craft = {' | |
| 14 | const start = template.indexOf(opening) | |
| 15 | if (start === -1) throw new Error('CraftApp.swift no longer injects `window.craft = {` from `let script`') | |
| 16 | const end = template.indexOf('\n """', start) | |
| 17 | return template | |
| 18 | .slice(template.indexOf('\n', start) + 1, end) | |
| 19 | // `\(config.enableHaptics)` and the like. Each is a Bool in the template. | |
| 20 | .replace(/\\\((?:[^()]|\([^()]*\))*\)/g, 'false') | |
| 21 | .replace(/\\\\/g, '\\') | |
| 22 | } | |
| 23 | ||
| 24 | interface Post { action: string, callbackId?: string, [key: string]: unknown } | |
| 25 | ||
| 26 | function loadPage() { | |
| 27 | const posts: Post[] = [] | |
| 28 | const listeners: Record<string, ((event: { type: string, detail: unknown }) => void)[]> = {} | |
| 29 | const page: Record<string, any> = { | |
| 30 | webkit: { messageHandlers: { craft: { postMessage: (message: Post) => posts.push(message) } } }, | |
| 31 | addEventListener: (type: string, listener: (event: { type: string, detail: unknown }) => void) => { | |
| 32 | (listeners[type] ||= []).push(listener) | |
| 33 | }, | |
| 34 | removeEventListener: () => {}, | |
| 35 | dispatchEvent: (event: { type: string, detail: unknown }) => { | |
| 36 | for (const listener of listeners[event.type] || []) listener(event) | |
| 37 | return true | |
| 38 | }, | |
| 39 | location: { href: 'craft://app/' }, | |
| 40 | } | |
| 41 | class CustomEvent { | |
| 42 | type: string | |
| 43 | detail: unknown | |
| 44 | constructor(type: string, options?: { detail?: unknown }) { | |
| 45 | this.type = type | |
| 46 | this.detail = options?.detail | |
| 47 | } | |
| 48 | } | |
| 49 | const quiet = { log() {}, warn() {}, error() {} } | |
| 50 | // eslint-disable-next-line no-new-func | |
| 51 | new Function('window', 'document', 'navigator', 'CustomEvent', 'console', pageScript())( | |
| 52 | page, | |
| 53 | { addEventListener() {}, readyState: 'complete' }, | |
| 54 | {}, | |
| 55 | CustomEvent, | |
| 56 | quiet, | |
| 57 | ) | |
| 58 | ||
| 59 | const craft = page.craft | |
| 60 | /** The last message the page posted for `action`. */ | |
| 61 | const last = (action: string): Post => { | |
| 62 | const post = posts.findLast(message => message.action === action) | |
| 63 | if (!post) throw new Error(`the page never posted ${action}`) | |
| 64 | return post | |
| 65 | } | |
| 66 | return { | |
| 67 | craft, | |
| 68 | last, | |
| 69 | // What Swift's resolveCallback and rejectCallback evaluate. | |
| 70 | answer: (action: string, value: unknown) => craft._resolveCallback(last(action).callbackId, value), | |
| 71 | refuse: (action: string, message: string, code: string) => craft._rejectCallback(last(action).callbackId, message, code), | |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | describe('the injected iOS page script', () => { | |
| 76 | // #207: each of these posted no callbackId, so Swift's reply helpers, which | |
| 77 | // return early on a nil id, dropped the answer and the page got undefined. | |
| 78 | const calls: [string, (craft: any) => unknown][] = [ | |
| 79 | ['haptic', craft => craft.haptic('light')], | |
| 80 | ['vibrate', craft => craft.vibrate([100, 50, 100])], | |
| 81 | ['startListening', craft => craft.startListening()], | |
| 82 | ['stopListening', craft => craft.stopListening()], | |
| 83 | ['watchPosition', craft => craft.geolocation.watchPosition(() => {})], | |
| 84 | ['clearWatch', craft => craft.geolocation.clearWatch()], | |
| 85 | ] | |
| 86 | ||
| 87 | for (const [action, call] of calls) { | |
| 88 | it(`hands ${action} the answer native sends`, async () => { | |
| 89 | const page = loadPage() | |
| 90 | const returned = call(page.craft) | |
| 91 | ||
| 92 | expect(page.last(action).callbackId).toMatch(/^cb_\d+$/) | |
| 93 | expect(returned).toBeInstanceOf(Promise) | |
| 94 | page.answer(action, true) | |
| 95 | expect(await returned).toBe(true) | |
| 96 | }) | |
| 97 | } | |
| 98 | ||
| 99 | it('hands a refusal to the caller of the raw call', async () => { | |
| 100 | const page = loadPage() | |
| 101 | const haptic = page.craft.haptic('light') | |
| 102 | page.refuse('haptic', 'Haptics is disabled', 'CAPABILITY_DISABLED') | |
| 103 | ||
| 104 | await expect(haptic).rejects.toMatchObject({ code: 'CAPABILITY_DISABLED' }) | |
| 105 | }) | |
| 106 | ||
| 107 | it('sends the v1 location clearWatch with an id once the last watch leaves', async () => { | |
| 108 | const page = loadPage() | |
| 109 | const first = page.craft.location.watchPosition(() => {}) | |
| 110 | const second = page.craft.location.watchPosition(() => {}) | |
| 111 | page.answer('watchPosition', true) | |
| 112 | ||
| 113 | page.craft.location.clearWatch(first) | |
| 114 | expect(() => page.last('clearWatch')).toThrow() | |
| 115 | page.craft.location.clearWatch(second) | |
| 116 | expect(page.last('clearWatch').callbackId).toMatch(/^cb_\d+$/) | |
| 117 | page.answer('clearWatch', true) | |
| 118 | }) | |
| 119 | ||
| 120 | // Haptics are feedback. An app that left enableHaptics off must not have | |
| 121 | // `await haptics.selection()` stop the flow it sits in, which is how the | |
| 122 | // SDK documents it and how Android and the web fallback behave. | |
| 123 | const feedback: [string, string, (craft: any) => Promise<unknown>][] = [ | |
| 124 | ['impact', 'haptic', craft => craft.haptics.impact('heavy')], | |
| 125 | ['notification', 'haptic', craft => craft.haptics.notification('error')], | |
| 126 | ['selection', 'haptic', craft => craft.haptics.selection()], | |
| 127 | ['vibrate', 'vibrate', craft => craft.haptics.vibrate([100])], | |
| 128 | ] | |
| 129 | ||
| 130 | for (const [name, action, call] of feedback) { | |
| 131 | it(`settles haptics.${name} with nothing played when haptics are off`, async () => { | |
| 132 | const page = loadPage() | |
| 133 | const settled = call(page.craft) | |
| 134 | page.refuse(action, 'Haptics is disabled', 'CAPABILITY_DISABLED') | |
| 135 | ||
| 136 | expect(await settled).toBeUndefined() | |
| 137 | }) | |
| 138 | ||
| 139 | it(`still rejects haptics.${name} when the native call fails`, async () => { | |
| 140 | const page = loadPage() | |
| 141 | const settled = call(page.craft) | |
| 142 | page.refuse(action, 'Native API call failed', 'NATIVE_CALL_FAILED') | |
| 143 | ||
| 144 | await expect(settled).rejects.toMatchObject({ code: 'NATIVE_CALL_FAILED' }) | |
| 145 | }) | |
| 146 | } | |
| 147 | }) | |