Repository navigation
Expand file tree
/
Copy pathmetro-session-hints.test.ts
More file actions
138 lines (124 loc) · 4.19 KB
/
Copy pathmetro-session-hints.test.ts
File metadata and controls
138 lines (124 loc) · 4.19 KB
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
import { test } from 'vitest';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import {
clearMetroSessionHints,
readMetroSessionHints,
writeMetroSessionHints,
} from '../metro/metro-session-hints.ts';
import { mkdtempForTestSync } from './test-utils/tmp-dir.ts';
function tempStateDir(): string {
const dir = path.join(
mkdtempForTestSync('agent-device-metro-session-hints'),
`agent-device-metro-session-hints-${randomUUID()}`,
);
mkdirSync(dir, { recursive: true });
return dir;
}
test('readMetroSessionHints returns undefined when no hints were written', () => {
const stateDir = tempStateDir();
try {
assert.equal(readMetroSessionHints({ stateDir, session: 'default' }), undefined);
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
});
test('writeMetroSessionHints and readMetroSessionHints round-trip per session', () => {
const stateDir = tempStateDir();
try {
writeMetroSessionHints({
stateDir,
session: 'proj-a',
hints: {
controlBaseUrl: 'http://127.0.0.1:8082/',
},
});
writeMetroSessionHints({
stateDir,
session: 'proj-b',
hints: { controlBaseUrl: 'http://127.0.0.1:8090/' },
});
assert.deepEqual(readMetroSessionHints({ stateDir, session: 'proj-a' }), {
controlBaseUrl: 'http://127.0.0.1:8082/',
});
assert.deepEqual(readMetroSessionHints({ stateDir, session: 'proj-b' }), {
controlBaseUrl: 'http://127.0.0.1:8090/',
});
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
});
test('writeMetroSessionHints overwrites a previous hint for the same session', () => {
const stateDir = tempStateDir();
try {
writeMetroSessionHints({
stateDir,
session: 'default',
hints: { controlBaseUrl: 'http://127.0.0.1:8081/' },
});
writeMetroSessionHints({
stateDir,
session: 'default',
hints: { controlBaseUrl: 'http://127.0.0.1:8082/' },
});
assert.deepEqual(readMetroSessionHints({ stateDir, session: 'default' }), {
controlBaseUrl: 'http://127.0.0.1:8082/',
});
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
});
test('clearMetroSessionHints removes a stored hint', () => {
const stateDir = tempStateDir();
try {
writeMetroSessionHints({
stateDir,
session: 'default',
hints: { controlBaseUrl: 'http://127.0.0.1:8082/' },
});
clearMetroSessionHints({ stateDir, session: 'default' });
assert.equal(readMetroSessionHints({ stateDir, session: 'default' }), undefined);
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
});
test('readMetroSessionHints ignores a corrupt hints file instead of throwing', () => {
const stateDir = tempStateDir();
try {
const filePath = path.join(stateDir, 'metro-sessions', 'default.json');
mkdirSync(path.dirname(filePath), { recursive: true });
writeFileSync(filePath, 'not json');
assert.equal(readMetroSessionHints({ stateDir, session: 'default' }), undefined);
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
});
test('sessions with unsafe characters get distinct sanitized files', () => {
const stateDir = tempStateDir();
try {
writeMetroSessionHints({
stateDir,
session: 'feature/branch-a',
hints: { controlBaseUrl: 'http://127.0.0.1:8082/' },
});
assert.deepEqual(readMetroSessionHints({ stateDir, session: 'feature/branch-a' }), {
controlBaseUrl: 'http://127.0.0.1:8082/',
});
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
});
test('released session records retain their reload address when read as a binding', () => {
const stateDir = tempStateDir();
const filePath = path.join(stateDir, 'metro-sessions', 'default.json');
mkdirSync(path.dirname(filePath), { recursive: true });
writeFileSync(
filePath,
JSON.stringify({ bundleUrl: 'https://metro.example.test/tenant-42/index.bundle?platform=ios' }),
);
assert.deepEqual(readMetroSessionHints({ stateDir, session: 'default' }), {
controlBaseUrl: 'https://metro.example.test/tenant-42',
});
});