File size: 2,901 Bytes
68465df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as fs from "fs"
import * as path from "path"

// Guards the invariant that `bun run compile` type-checks @mentra/bluetooth-sdk
// against the same files Metro bundles (src/, via the "react-native" exports
// condition), never against the gitignored build/ output. build/ is only
// regenerated when expo-module prepare runs, so once it goes stale tsc would
// otherwise green-light calls that are undefined at runtime — we shipped
// exactly that once (a method present in stale build/*.d.ts but absent from
// the src public surface).
//
// Two pieces make the invariant hold:
//  1. tsc activates the "react-native" condition: customConditions comes from
//     expo/tsconfig.base, which mobile/tsconfig.json extends.
//  2. In every bluetooth-sdk exports entry, "react-native" precedes "types".
//     Exports conditions match in declaration order and "types" is always
//     active for tsc, so listing "types" first sends tsc back to build/.

const mobileRoot = path.resolve(__dirname, "../..")

const readJson = (filePath: string) => {
  const raw = fs.readFileSync(filePath, "utf8")
  try {
    return JSON.parse(raw)
  } catch (error) {
    throw new Error(`Failed to parse ${filePath}: ${error}`)
  }
}

describe("bluetooth-sdk type/runtime surface alignment", () => {
  const packageJson = readJson(path.join(mobileRoot, "modules/bluetooth-sdk/package.json"))
  const conditionEntries = Object.entries(packageJson.exports as Record<string, unknown>).filter(
    ([, conditions]) => typeof conditions === "object" && conditions !== null && "react-native" in conditions,
  ) as Array<[string, Record<string, string>]>

  it("covers the conditional export subpaths", () => {
    expect(conditionEntries.map(([subpath]) => subpath)).toEqual(
      expect.arrayContaining([".", "./internal", "./react", "./photo-receiver", "./debug"]),
    )
  })

  it.each(conditionEntries)("lists react-native before types for %s", (_subpath, conditions) => {
    const keys = Object.keys(conditions)
    expect(keys.indexOf("react-native")).toBeGreaterThanOrEqual(0)
    expect(keys.indexOf("types")).toBeGreaterThan(keys.indexOf("react-native"))
    expect(conditions["react-native"]).toMatch(/^\.\/src\//)
  })

  it("activates the react-native condition for tsc", () => {
    const tsconfig = readJson(path.join(mobileRoot, "tsconfig.json"))
    expect(tsconfig.extends).toBe("expo/tsconfig.base")

    const expoBase = readJson(path.join(mobileRoot, "node_modules/expo/tsconfig.base.json"))
    const effective = (key: string) => tsconfig.compilerOptions?.[key] ?? expoBase.compilerOptions?.[key]

    expect(effective("customConditions")).toContain("react-native")
    // customConditions only applies under these moduleResolution modes.
    expect(["bundler", "node16", "nodenext"]).toContain(effective("moduleResolution"))
  })
})