Skip to content

Commit 77a27fe

Browse files
committed
test: runtime.sendNativeMessage
1 parent 36a3f1e commit 77a27fe

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

packages/electron-chrome-extensions/script/native-messaging-host/build.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ async function main() {
7474
}
7575

7676
const extensionIds = extensionIdsArg.split(',')
77-
console.log(extensionIds)
7877
await createSEA()
7978
await installConfig(extensionIds)
8079
}
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { expect } from 'chai'
2+
import { randomUUID } from 'node:crypto'
13
import { promisify } from 'node:util'
24
import * as cp from 'node:child_process'
35
import * as path from 'node:path'
@@ -6,8 +8,7 @@ const exec = promisify(cp.exec)
68
import { useExtensionBrowser, useServer } from './hooks'
79
import { getExtensionId } from './crx-helpers'
810

9-
// TODO:
10-
describe.skip('nativeMessaging', () => {
11+
describe('nativeMessaging', () => {
1112
const server = useServer()
1213
const browser = useExtensionBrowser({
1314
url: server.getUrl,
@@ -17,14 +18,21 @@ describe.skip('nativeMessaging', () => {
1718

1819
before(async () => {
1920
const extensionId = await getExtensionId('rpc')
20-
const scriptPath = path.join(__dirname, '..', 'script', 'native-messaging-host', 'build.js')
21-
await exec(`${scriptPath} ${extensionId}`)
21+
const nativeMessagingPath = path.join(__dirname, '..', 'script', 'native-messaging-host')
22+
await exec(`${path.join(nativeMessagingPath, 'build.js')} ${extensionId}`)
2223
})
2324

24-
describe('connectNative()', () => {
25-
it('returns tab details', async () => {
26-
const result = await browser.crx.exec('runtime.connectNative', hostApplication)
27-
console.log({ result })
25+
describe('sendNativeMessage()', () => {
26+
it('sends and receives primitive value', async () => {
27+
const value = randomUUID()
28+
const result = await browser.crx.exec('runtime.sendNativeMessage', hostApplication, value)
29+
expect(result).to.equal(value)
30+
})
31+
32+
it('sends and receives object', async () => {
33+
const value = { json: randomUUID(), wow: 'nice' }
34+
const result = await browser.crx.exec('runtime.sendNativeMessage', hostApplication, value)
35+
expect(result).to.deep.equal(value)
2836
})
2937
})
3038
})

packages/electron-chrome-extensions/spec/fixtures/rpc/manifest.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
"persistent": true
1717
},
1818
"manifest_version": 2,
19-
"permissions": ["contextMenus", "webRequest", "webRequestBlocking", "<all_urls>"]
19+
"permissions": [
20+
"contextMenus",
21+
"nativeMessaging",
22+
"webRequest",
23+
"webRequestBlocking",
24+
"<all_urls>"
25+
]
2026
}

packages/electron-chrome-extensions/src/browser/api/runtime.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { randomUUID } from 'node:crypto'
12
import { EventEmitter } from 'node:events'
23
import { ExtensionContext } from '../context'
34
import { ExtensionEvent } from '../router'
@@ -37,7 +38,7 @@ export class RuntimeAPI extends EventEmitter {
3738
}
3839

3940
private sendNativeMessage = async (event: ExtensionEvent, application: string, message: any) => {
40-
const connectionId = crypto.randomUUID()
41+
const connectionId = randomUUID()
4142
const host = new NativeMessagingHost(
4243
event.extension.id,
4344
event.sender!,

0 commit comments

Comments
 (0)