-
Notifications
You must be signed in to change notification settings - Fork 14
feat(native): one door to app-local plugins, with the fallback made mandatory #2978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bde2960
feat(native): one door to app-local plugins, with the fallback made m…
innolope-dev b2f286a
fix(native): take a method name, not a callback holding the plugin
innolope-dev b4474d8
test(native): make the proxy-invariant assertion compile-only
innolope-dev 0ec9781
fix(native): close the lint bypass, require a live bridge, fix the me…
innolope-dev f4c259d
fix(lint): narrow the import-rule overrides instead of switching them…
innolope-dev 1d184ca
fix(lint): ban the registerPlugin CALL, not just the named import
innolope-dev 8e5462b
fix(lint): stop the DS 10 override lifting the tailwind-merge ban too
innolope-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // nativeCapability is the single door to app-local Capacitor plugins, so its | ||
| // job is the failure paths: a plugin the running binary predates, a platform | ||
| // with no native half, and the registerPlugin proxy's thenable trap. | ||
| import { nativeCapability } from '../native-capability' | ||
| import { isAndroidNative, isIOSNative } from '../capacitor' | ||
| import { createPluginProxy, expectToSettle } from '../__mocks__/capacitor-plugin-proxy' | ||
|
|
||
| const pluginImplementation: Record<string, unknown> = {} | ||
|
|
||
| jest.mock('@capacitor/core', () => ({ | ||
| // The real registerPlugin returns a Proxy, not a plain object. Mocking it | ||
| // as a plain object is what hides the thenable trap, so the house mock is | ||
| // used here too. | ||
| registerPlugin: jest.fn(() => | ||
| jest.requireActual('../__mocks__/capacitor-plugin-proxy').createPluginProxy(pluginImplementation, 'TestPlugin') | ||
| ), | ||
| })) | ||
|
|
||
| jest.mock('../capacitor', () => ({ | ||
| isIOSNative: jest.fn(() => false), | ||
| isAndroidNative: jest.fn(() => false), | ||
| })) | ||
|
|
||
| const mockIsIOSNative = isIOSNative as jest.MockedFunction<typeof isIOSNative> | ||
| const mockIsAndroidNative = isAndroidNative as jest.MockedFunction<typeof isAndroidNative> | ||
|
|
||
| interface TestPlugin { | ||
| doThing(options?: undefined): Promise<{ value: string }> | ||
| } | ||
|
|
||
| describe('nativeCapability', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| for (const key of Object.keys(pluginImplementation)) delete pluginImplementation[key] | ||
| mockIsIOSNative.mockReturnValue(false) | ||
| mockIsAndroidNative.mockReturnValue(false) | ||
| }) | ||
|
|
||
| it('returns the native answer on a supported platform', async () => { | ||
| mockIsIOSNative.mockReturnValue(true) | ||
| pluginImplementation.doThing = jest.fn(async () => ({ value: 'native' })) | ||
|
|
||
| const capability = nativeCapability<TestPlugin>('TestPlugin', { platforms: ['ios'] }) | ||
|
|
||
| await expect(capability.call('doThing', undefined, () => ({ value: 'fallback' }))).resolves.toEqual({ | ||
| value: 'native', | ||
| }) | ||
| }) | ||
|
|
||
| it('falls back without touching the plugin on an unsupported platform', async () => { | ||
| mockIsAndroidNative.mockReturnValue(true) | ||
| const doThing = jest.fn() | ||
| pluginImplementation.doThing = doThing | ||
|
|
||
| const capability = nativeCapability<TestPlugin>('TestPlugin', { platforms: ['ios'] }) | ||
|
|
||
| await expect(capability.call('doThing', undefined, () => ({ value: 'fallback' }))).resolves.toEqual({ | ||
| value: 'fallback', | ||
| }) | ||
| // Not merely "answered fallback": on web the proxy exists and invoking | ||
| // it rejects, so the gate has to stop the call, not catch it. | ||
| expect(doThing).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('falls back when the running binary predates the plugin', async () => { | ||
| mockIsIOSNative.mockReturnValue(true) | ||
| // The method is simply absent — exactly an older binary running OTA'd | ||
| // JS. The proxy answers with an Unimplemented rejection, not undefined. | ||
|
|
||
| const capability = nativeCapability<TestPlugin>('TestPlugin', { platforms: ['ios'] }) | ||
|
|
||
| await expect(capability.call('doThing', undefined, () => ({ value: 'fallback' }))).resolves.toEqual({ | ||
| value: 'fallback', | ||
| }) | ||
| }) | ||
|
|
||
| it('hands the rejection to the fallback, for callers that report it', async () => { | ||
| mockIsIOSNative.mockReturnValue(true) | ||
| pluginImplementation.doThing = jest.fn(async () => { | ||
| throw new Error('user cancelled') | ||
| }) | ||
|
|
||
| const capability = nativeCapability<TestPlugin>('TestPlugin', { platforms: ['ios'] }) | ||
| const result = await capability.call('doThing', undefined, (error) => ({ | ||
| value: error instanceof Error ? error.message : 'unknown', | ||
| })) | ||
|
|
||
| expect(result).toEqual({ value: 'user cancelled' }) | ||
| }) | ||
|
|
||
| it('settles rather than hanging, even though the plugin is a proxy', async () => { | ||
| mockIsIOSNative.mockReturnValue(true) | ||
| pluginImplementation.doThing = jest.fn(async () => ({ value: 'native' })) | ||
|
|
||
| const capability = nativeCapability<TestPlugin>('TestPlugin', { platforms: ['ios'] }) | ||
|
|
||
| // The trap this closes: resolving a promise WITH the plugin makes the | ||
| // runtime probe proxy.then, which dispatches a native call that never | ||
| // invokes either callback — the promise stays pending forever. | ||
| await expect( | ||
| expectToSettle(capability.call('doThing', undefined, () => ({ value: 'fallback' }))) | ||
| ).resolves.toEqual({ value: 'native' }) | ||
| }) | ||
|
|
||
| it('gives callers no way to get the proxy back out', () => { | ||
| const capability = nativeCapability<TestPlugin>('TestPlugin', { platforms: ['ios'] }) | ||
|
|
||
| // The reason `call` takes a method NAME. With a callback signature, | ||
| // `call(async (plugin) => plugin, fallback)` type-checks and then hangs | ||
| // forever: the async function assimilates the proxy's .then while | ||
| // resolving, so it never reaches the catch or the fallback. There is no | ||
| // runtime guard for it — the hang happens before any code of ours runs | ||
| // again — so the invariant has to hold at the type level, and typecheck | ||
| // is a CI job. If this stops erroring, the hole is back. | ||
| // @ts-expect-error a callback is not a method name | ||
|
Check failure on line 115 in src/utils/__tests__/native-capability.test.ts
|
||
| expect(() => | ||
| capability.call( | ||
|
Check failure on line 117 in src/utils/__tests__/native-capability.test.ts
|
||
| async (plugin: TestPlugin) => plugin, | ||
| () => undefined | ||
| ) | ||
| ).toBeDefined() | ||
|
innolope-dev marked this conversation as resolved.
Outdated
|
||
| // @ts-expect-error 'notAMethod' is not on TestPlugin | ||
| expect(() => capability.call('notAMethod', undefined, () => undefined)).toBeDefined() | ||
| }) | ||
|
|
||
| it('reports platform support from the declared platforms', () => { | ||
| const iosOnly = nativeCapability<TestPlugin>('TestPlugin', { platforms: ['ios'] }) | ||
| const both = nativeCapability<TestPlugin>('TestPlugin', { platforms: ['ios', 'android'] }) | ||
|
|
||
| expect(iosOnly.isSupportedPlatform()).toBe(false) | ||
|
|
||
| mockIsAndroidNative.mockReturnValue(true) | ||
| expect(iosOnly.isSupportedPlatform()).toBe(false) | ||
| expect(both.isSupportedPlatform()).toBe(true) | ||
|
|
||
| mockIsAndroidNative.mockReturnValue(false) | ||
| mockIsIOSNative.mockReturnValue(true) | ||
| expect(iosOnly.isSupportedPlatform()).toBe(true) | ||
| }) | ||
|
|
||
| it('keeps the house proxy semantics the mock exists to enforce', async () => { | ||
| // Guards the guard: if createPluginProxy ever stopped rejecting for | ||
| // absent methods, every "older binary" test above would pass vacuously. | ||
| const proxy = createPluginProxy<Record<string, unknown>>({}, 'TestPlugin') | ||
|
|
||
| await expect((proxy as unknown as TestPlugin).doThing()).rejects.toThrow('is not implemented') | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.