rn-event-channel.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  2. import * as fs from 'node:fs';
  3. import * as path from 'node:path';
  4. import * as os from 'node:os';
  5. import { CodeGraph } from '../src';
  6. /**
  7. * End-to-end synthesizer test: write a fixture project with a native ObjC
  8. * `sendEventWithName:` site and a JS `addListener('x', fn)` subscriber,
  9. * index it, and verify the synthesized cross-language event edge.
  10. */
  11. describe('RN event channel synthesizer', () => {
  12. let dir: string;
  13. beforeEach(() => {
  14. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rn-event-fixture-'));
  15. });
  16. afterEach(() => {
  17. fs.rmSync(dir, { recursive: true, force: true });
  18. });
  19. it('synthesizes an edge from ObjC sendEventWithName: to JS addListener handler', async () => {
  20. // package.json so the RN detector / general resolver sees the project as RN.
  21. fs.writeFileSync(
  22. path.join(dir, 'package.json'),
  23. '{"name":"x","dependencies":{"react-native":"^0.73"}}'
  24. );
  25. fs.writeFileSync(
  26. path.join(dir, 'Emitter.m'),
  27. `
  28. @implementation Emitter
  29. - (void)reportLocation {
  30. [self sendEventWithName:@"locationUpdate" body:@{}];
  31. }
  32. @end
  33. `
  34. );
  35. fs.writeFileSync(
  36. path.join(dir, 'App.js'),
  37. `
  38. function onLocation(payload) {
  39. console.log(payload);
  40. }
  41. emitter.addListener('locationUpdate', onLocation);
  42. `
  43. );
  44. const cg = await CodeGraph.init(dir, { silent: true });
  45. await cg.indexAll();
  46. const db = (cg as any).db.db;
  47. const rows = db
  48. .prepare(
  49. `SELECT s.name source_name, s.language sl, t.name target_name, t.language tl,
  50. json_extract(e.metadata,'$.event') event
  51. FROM edges e
  52. JOIN nodes s ON s.id = e.source
  53. JOIN nodes t ON t.id = e.target
  54. WHERE json_extract(e.metadata,'$.synthesizedBy') = 'rn-event-channel'`
  55. )
  56. .all();
  57. cg.close?.();
  58. expect(rows.length).toBeGreaterThanOrEqual(1);
  59. // The edge should point from the ObjC method that emits to the JS handler.
  60. const edge = rows.find((r: any) => r.event === 'locationUpdate');
  61. expect(edge).toBeDefined();
  62. expect(edge.sl).toBe('objc');
  63. expect(edge.tl).toBe('javascript');
  64. expect(edge.target_name).toBe('onLocation');
  65. });
  66. it('falls back to enclosing JS function when addListener handler is a parameter (wrapper-API pattern)', async () => {
  67. // Matches the real RNFirebase shape: `messaging().onMessage(listener)`
  68. // is a subscribe-wrapper whose body does
  69. // `addListener('messaging_message_received', listener)` where `listener`
  70. // is the parameter — not a globally-named symbol. Synthesizer should
  71. // still produce an edge, attributed to the enclosing wrapper function.
  72. fs.writeFileSync(
  73. path.join(dir, 'package.json'),
  74. '{"dependencies":{"react-native":"^0.73"}}'
  75. );
  76. fs.writeFileSync(
  77. path.join(dir, 'Native.m'),
  78. `
  79. @implementation MyEmitter
  80. - (void)pushMessage {
  81. [[Shared shared] sendEventWithName:@"messaging_message_received" body:@{}];
  82. }
  83. @end
  84. `
  85. );
  86. fs.writeFileSync(
  87. path.join(dir, 'messaging.ts'),
  88. `
  89. import { NativeEventEmitter } from 'react-native';
  90. const emitter = new NativeEventEmitter();
  91. export function onMessage(listener: (m: any) => void) {
  92. return emitter.addListener('messaging_message_received', listener);
  93. }
  94. `
  95. );
  96. const cg = await CodeGraph.init(dir, { silent: true });
  97. await cg.indexAll();
  98. const db = (cg as any).db.db;
  99. const rows = db
  100. .prepare(
  101. `SELECT s.name source_name, t.name target_name, t.kind target_kind, t.language tl,
  102. json_extract(e.metadata,'$.event') event
  103. FROM edges e
  104. JOIN nodes s ON s.id = e.source
  105. JOIN nodes t ON t.id = e.target
  106. WHERE json_extract(e.metadata,'$.synthesizedBy') = 'rn-event-channel'`
  107. )
  108. .all();
  109. cg.close?.();
  110. const edge = rows.find((r: any) => r.event === 'messaging_message_received');
  111. expect(edge).toBeDefined();
  112. // Target should be the wrapper function `onMessage` — the enclosing
  113. // function of the addListener call, not a bareword named handler.
  114. expect(edge.target_name).toBe('onMessage');
  115. expect(['function', 'method']).toContain(edge.target_kind);
  116. });
  117. it('synthesizes an edge from a Java sendEvent(ctx, "X", body) wrapper to a JS handler', async () => {
  118. fs.writeFileSync(path.join(dir, 'package.json'), '{"dependencies":{"react-native":"^0.74.0"}}');
  119. // The literal event name lives in the WRAPPER CALL, not in `.emit` (whose
  120. // first arg is the `eventName` VARIABLE) — the common react-native-device-info
  121. // shape that RN_JVM_EMIT_RE alone misses.
  122. fs.writeFileSync(path.join(dir, 'BatteryModule.java'),
  123. 'public class BatteryModule extends ReactContextBaseJavaModule {\n' +
  124. ' @Override public String getName() { return "BatteryModule"; }\n' +
  125. ' public void onBatteryChanged() {\n' +
  126. ' sendEvent(getReactApplicationContext(),\n' +
  127. ' "myWrapperBatteryEvent", null);\n' +
  128. ' }\n' +
  129. ' private void sendEvent(ReactContext ctx, String eventName, Object data) {\n' +
  130. ' ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, data);\n' +
  131. ' }\n' +
  132. '}\n');
  133. fs.writeFileSync(path.join(dir, 'index.ts'),
  134. "function onBattery() {}\n" +
  135. "emitter.addListener('myWrapperBatteryEvent', onBattery);\n");
  136. const cg = await CodeGraph.init(dir, { silent: true });
  137. await cg.indexAll();
  138. const db = (cg as any).db.db;
  139. const rows = db.prepare(
  140. "SELECT s.name source_name, s.language sl, t.name target_name FROM edges e " +
  141. "JOIN nodes s ON s.id=e.source JOIN nodes t ON t.id=e.target " +
  142. "WHERE json_extract(e.metadata,'$.synthesizedBy')='rn-event-channel' AND json_extract(e.metadata,'$.event')='myWrapperBatteryEvent'"
  143. ).all();
  144. cg.close?.();
  145. expect(rows.length).toBeGreaterThanOrEqual(1);
  146. expect(rows[0].sl).toBe('java');
  147. expect(rows[0].source_name).toBe('onBatteryChanged');
  148. expect(rows[0].target_name).toBe('onBattery');
  149. });
  150. });