-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathinfer-scope.test.ts
More file actions
186 lines (166 loc) · 5.22 KB
/
infer-scope.test.ts
File metadata and controls
186 lines (166 loc) · 5.22 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
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
import { inferScope, selectTeam } from "./project.js";
import {
beforeEach,
describe,
test,
vi,
Mock,
expect,
onTestFinished,
} from "vitest";
import { fetchApi } from "./api.js";
import { NotOk } from "./error.js";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import * as os from "node:os";
const fetchApiMock = fetchApi as Mock<typeof fetchApi>;
vi.mock("./api");
beforeEach(() => {
vi.clearAllMocks();
});
async function getTempDir(): Promise<string> {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "infer-scope-test-"));
onTestFinished(() => fs.rm(dir, { recursive: true }));
return dir;
}
describe("selectTeam", () => {
test("returns defaultTeamId when set", async () => {
fetchApiMock.mockResolvedValue({
user: { defaultTeamId: "team_abc123", username: "my-user" },
});
const team = await selectTeam("token");
expect(fetchApiMock).toHaveBeenCalledWith({
endpoint: "/v2/user",
token: "token",
});
expect(team).toBe("team_abc123");
});
test("falls back to username when defaultTeamId is null", async () => {
fetchApiMock.mockResolvedValue({
user: { defaultTeamId: null, username: "my-user" },
});
const team = await selectTeam("token");
expect(team).toBe("my-user");
});
});
describe("inferScope", () => {
test("uses provided teamId", async () => {
fetchApiMock.mockResolvedValue({});
const scope = await inferScope({ teamId: "my-team", token: "token" });
expect(scope).toEqual({
created: false,
projectId: "vercel-sandbox-default-project",
teamId: "my-team",
});
});
describe("team creation", () => {
test("project 404 triggers project creation", async () => {
fetchApiMock.mockImplementation(async ({ method }) => {
if (!method || method === "GET") {
throw new NotOk({ statusCode: 404, responseText: "Not Found" });
}
return {};
});
const scope = await inferScope({ teamId: "my-team", token: "token" });
expect(scope).toEqual({
created: true,
projectId: "vercel-sandbox-default-project",
teamId: "my-team",
});
});
test("non-404 throws", async () => {
fetchApiMock.mockImplementation(async ({ method }) => {
if (!method || method === "GET") {
throw new NotOk({ statusCode: 403, responseText: "Forbidden" });
}
return {};
});
await expect(
inferScope({ teamId: "my-team", token: "token" }),
).rejects.toThrowError(
new NotOk({ statusCode: 403, responseText: "Forbidden" }),
);
});
test("non-status errors are thrown", async () => {
fetchApiMock.mockImplementation(async ({ method }) => {
if (!method || method === "GET") {
throw new Error("Oops!");
}
return {};
});
await expect(inferScope({ token: "token" })).rejects.toThrowError(
"Oops!",
);
});
});
test("infers the team from the user's defaultTeamId", async () => {
fetchApiMock.mockImplementation(async ({ endpoint }) => {
if (endpoint === "/v2/user") {
return { user: { defaultTeamId: "team_default", username: "my-user" } };
}
return {};
});
const scope = await inferScope({ token: "token" });
expect(scope).toEqual({
created: false,
projectId: "vercel-sandbox-default-project",
teamId: "team_default",
});
});
describe("linked project", () => {
test("uses linked project when .vercel/project.json exists", async () => {
const dir = await getTempDir();
await fs.mkdir(path.join(dir, ".vercel"));
await fs.writeFile(
path.join(dir, ".vercel", "project.json"),
JSON.stringify({
projectId: "prj_linked",
orgId: "team_linked",
}),
);
const scope = await inferScope({ token: "token", cwd: dir });
expect(scope).toEqual({
created: false,
projectId: "prj_linked",
teamId: "team_linked",
});
// Should not call API when using linked project
expect(fetchApiMock).not.toHaveBeenCalled();
});
test("falls back to default project when .vercel/project.json does not exist", async () => {
const dir = await getTempDir();
fetchApiMock.mockResolvedValue({});
const scope = await inferScope({
token: "token",
teamId: "my-team",
cwd: dir,
});
expect(scope).toEqual({
created: false,
projectId: "vercel-sandbox-default-project",
teamId: "my-team",
});
expect(fetchApiMock).toHaveBeenCalled();
});
test("falls back to default project when .vercel/project.json is invalid", async () => {
const dir = await getTempDir();
await fs.mkdir(path.join(dir, ".vercel"));
await fs.writeFile(
path.join(dir, ".vercel", "project.json"),
"not valid json",
);
fetchApiMock.mockResolvedValue({});
const scope = await inferScope({
token: "token",
teamId: "my-team",
cwd: dir,
});
expect(scope).toEqual({
created: false,
projectId: "vercel-sandbox-default-project",
teamId: "my-team",
});
expect(fetchApiMock).toHaveBeenCalled();
});
});
});