File size: 9,866 Bytes
95b671e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import assert from "node:assert/strict";
import { test } from "node:test";
import { gzipSync } from "node:zlib";
import type { DB, IssueSample } from "@superlog/db";
import type * as schema from "@superlog/db/schema";
import {
  findSourceMapArtifact,
  symbolicateIssueSample,
  symbolicateStacktraceWithArtifact,
  symbolicateTelemetrySample,
  symbolicationAttrsForSample,
} from "./symbolication.js";

const sourceMap = JSON.stringify({
  version: 3,
  file: "index.android.bundle",
  sources: ["app/index.tsx"],
  names: ["HomeScreen"],
  mappings: "AAAAA",
});

const artifact = {
  id: "artifact-1",
  projectId: "project-1",
  platform: "android",
  release: "juno@1.2.3",
  dist: null,
  debugId: null,
  bundleFile: "index.android.bundle",
  mapFile: "index.android.bundle.map",
  sourceMapHash: "a".repeat(64),
  sourceMapBytes: Buffer.byteLength(sourceMap),
  storageBucket: "source-map-bucket",
  storageKey: "source-maps/project-1/android/hash.map.gz",
  contentEncoding: "gzip",
  uploadedByOrgApiKeyId: null,
  createdAt: new Date("2026-06-02T00:00:00.000Z"),
  updatedAt: new Date("2026-06-02T00:00:00.000Z"),
} satisfies schema.SourceMapArtifact;

test("symbolicateStacktraceWithArtifact rewrites generated stack frames", () => {
  const result = symbolicateStacktraceWithArtifact({
    stacktrace: "TypeError: bad\n    at useMemoCache (index.android.bundle:1:1)",
    sourceMap,
    artifact,
  });

  assert.ok(result);
  assert.equal(result.stacktrace, "TypeError: bad\n    at HomeScreen (app/index.tsx:1:1)");
  assert.deepEqual(result.frames[0], {
    functionName: "HomeScreen",
    source: "app/index.tsx",
    line: 1,
    column: 1,
    generatedFile: "index.android.bundle",
    generatedLine: 1,
    generatedColumn: 1,
  });
});

test("symbolicateStacktraceWithArtifact infers original function names from source content", () => {
  const sourceMapWithoutNames = JSON.stringify({
    version: 3,
    file: "entry.js",
    sources: ["/app/(tabs)/index.tsx"],
    sourcesContent: [
      [
        "async function emitError() {",
        "  const superlog = await ensureSuperlog();",
        "  superlog?.captureException(new Error('Expo SDK demo error'), { component: 'home' });",
        "}",
      ].join("\n"),
    ],
    names: [],
    mappings: "AAAA",
  });

  const result = symbolicateStacktraceWithArtifact({
    stacktrace: "Error: Expo SDK demo error\n    at L (entry.js:1:1)",
    sourceMap: sourceMapWithoutNames,
    artifact: { ...artifact, platform: "web" },
  });

  assert.ok(result);
  assert.equal(
    result.stacktrace,
    "Error: Expo SDK demo error\n    at emitError (/app/(tabs)/index.tsx:1:1)",
  );
  assert.equal(result.frames[0]?.functionName, "emitError");
});

test("symbolicationAttrsForSample extracts release, platform, dist, and debug id", () => {
  const sample = {
    kind: "log",
    service: "juno",
    severity: "ERROR",
    message: "bad",
    body: "bad",
    exceptionType: "TypeError",
    topFrame: null,
    normalizedFrames: [],
    stacktrace: "TypeError: bad\n    at index.android.bundle:1:1",
    seenAt: "2026-06-02T00:00:00.000Z",
    logAttrs: {
      "service.version": "juno@1.2.3",
      "device.platform": "Android",
      "expo.update_id": "update-1",
      "sourcemap.debug_id": "debug-1",
    },
    resourceAttrs: null,
  } satisfies IssueSample;

  assert.deepEqual(symbolicationAttrsForSample(sample), {
    debugId: "debug-1",
    release: "juno@1.2.3",
    dist: "update-1",
    platform: "android",
  });
});

test("symbolicateIssueSample loads matching source map object and symbolicates", async () => {
  const sample = {
    kind: "log",
    service: "juno",
    severity: "ERROR",
    message: "bad",
    body: "bad",
    exceptionType: "TypeError",
    topFrame: null,
    normalizedFrames: [],
    stacktrace: "TypeError: bad\n    at useMemoCache (index.android.bundle:1:1)",
    seenAt: "2026-06-02T00:00:00.000Z",
    logAttrs: {
      "service.version": "juno@1.2.3",
      "device.platform": "android",
    },
    resourceAttrs: null,
  } satisfies IssueSample;
  const database = {
    query: {
      sourceMapArtifacts: {
        findFirst: async () => null,
        findMany: async () => [artifact],
      },
    },
  } as unknown as DB;

  const result = await symbolicateIssueSample({
    database,
    objectReader: {
      async getSourceMapObject(input) {
        assert.equal(input.bucket, "source-map-bucket");
        assert.equal(input.key, "source-maps/project-1/android/hash.map.gz");
        return gzipSync(sourceMap);
      },
    },
    projectId: "project-1",
    sample,
  });

  assert.ok(result);
  assert.equal(result.artifact.id, "artifact-1");
  assert.equal(result.frames[0]?.source, "app/index.tsx");
});

test("symbolicateTelemetrySample symbolicates logs without issue rows", async () => {
  const database = {
    query: {
      sourceMapArtifacts: {
        findFirst: async () => null,
        findMany: async () => [artifact],
      },
    },
  } as unknown as DB;

  const result = await symbolicateTelemetrySample({
    database,
    objectReader: {
      async getSourceMapObject(input) {
        assert.equal(input.bucket, "source-map-bucket");
        assert.equal(input.key, "source-maps/project-1/android/hash.map.gz");
        return gzipSync(sourceMap);
      },
    },
    projectId: "project-1",
    sample: {
      stacktrace: "TypeError: bad\n    at useMemoCache (index.android.bundle:1:1)",
      logAttrs: {
        "service.version": "juno@1.2.3",
        "device.platform": "android",
      },
      resourceAttrs: null,
    },
  });

  assert.ok(result);
  assert.equal(result.artifact.id, "artifact-1");
  assert.equal(result.frames[0]?.source, "app/index.tsx");
});

test("symbolicateTelemetrySample returns null when a log has no stacktrace", async () => {
  const result = await symbolicateTelemetrySample({
    database: {} as DB,
    objectReader: null,
    projectId: "project-1",
    sample: {
      stacktrace: null,
      logAttrs: null,
      resourceAttrs: null,
    },
  });

  assert.equal(result, null);
});

test("findSourceMapArtifact prefers artifact matching generated stack frame file", async () => {
  const entryArtifact = {
    ...artifact,
    id: "entry-artifact",
    platform: "web",
    bundleFile: "dist/_expo/static/js/web/entry-abc123.js",
    mapFile: "dist/_expo/static/js/web/entry-abc123.js.map",
    createdAt: new Date("2026-06-02T00:00:00.000Z"),
    updatedAt: new Date("2026-06-02T00:00:00.000Z"),
  } satisfies schema.SourceMapArtifact;
  const indexArtifact = {
    ...artifact,
    id: "index-artifact",
    platform: "web",
    bundleFile: "dist/_expo/static/js/web/index-def456.js",
    mapFile: "dist/_expo/static/js/web/index-def456.js.map",
    createdAt: new Date("2026-06-02T00:01:00.000Z"),
    updatedAt: new Date("2026-06-02T00:01:00.000Z"),
  } satisfies schema.SourceMapArtifact;
  const database = {
    query: {
      sourceMapArtifacts: {
        findFirst: async () => null,
        findMany: async () => [indexArtifact, entryArtifact],
      },
    },
  } as unknown as DB;

  const result = await findSourceMapArtifact({
    database,
    projectId: "project-1",
    attrs: {
      debugId: null,
      release: "juno@1.2.3",
      dist: null,
      platform: "web",
    },
    stacktrace:
      "TypeError: bad\n    at useMemoCache (https://app.example.com/_expo/static/js/web/entry-abc123.js:1:1)",
  });

  assert.equal(result?.id, "entry-artifact");
});

test("findSourceMapArtifact falls back to generated stack frame file when release is missing", async () => {
  const entryArtifact = {
    ...artifact,
    id: "entry-artifact",
    platform: "web",
    release: "expo-test-2@local",
    bundleFile: "dist-superlog/_expo/static/js/web/entry-90d9514ed4f128f73d2feaf2e08ff315.js",
    mapFile: "dist-superlog/_expo/static/js/web/entry-90d9514ed4f128f73d2feaf2e08ff315.js.map",
    createdAt: new Date("2026-06-02T00:00:00.000Z"),
    updatedAt: new Date("2026-06-02T00:00:00.000Z"),
  } satisfies schema.SourceMapArtifact;
  const database = {
    query: {
      sourceMapArtifacts: {
        findFirst: async () => null,
        findMany: async () => [entryArtifact],
      },
    },
  } as unknown as DB;

  const result = await findSourceMapArtifact({
    database,
    projectId: "project-1",
    attrs: {
      debugId: null,
      release: null,
      dist: null,
      platform: null,
    },
    stacktrace:
      "Error: Expo SDK demo error\n    at L (http://localhost:8093/_expo/static/js/web/entry-90d9514ed4f128f73d2feaf2e08ff315.js:1095:1077)",
  });

  assert.equal(result?.id, "entry-artifact");
});

test("findSourceMapArtifact falls back to generated stack frame file when release attrs do not match upload release", async () => {
  const entryArtifact = {
    ...artifact,
    id: "entry-artifact",
    platform: "web",
    release: "expo-test-2@local",
    dist: null,
    bundleFile: "dist-superlog/_expo/static/js/web/entry-90d9514ed4f128f73d2feaf2e08ff315.js",
    mapFile: "dist-superlog/_expo/static/js/web/entry-90d9514ed4f128f73d2feaf2e08ff315.js.map",
    createdAt: new Date("2026-06-02T00:00:00.000Z"),
    updatedAt: new Date("2026-06-02T00:00:00.000Z"),
  } satisfies schema.SourceMapArtifact;
  const database = {
    query: {
      sourceMapArtifacts: {
        findFirst: async () => null,
        findMany: async (_query: unknown) => [entryArtifact],
      },
    },
  } as unknown as DB;

  const result = await findSourceMapArtifact({
    database,
    projectId: "project-1",
    attrs: {
      debugId: null,
      release: "1.0.0",
      dist: "embedded",
      platform: "web",
    },
    stacktrace:
      "Error: Expo SDK demo error\n    at L (http://localhost:8093/_expo/static/js/web/entry-90d9514ed4f128f73d2feaf2e08ff315.js:1095:1077)",
  });

  assert.equal(result?.id, "entry-artifact");
});