Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/util/generate-workspace-id.spec.ts
2500 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { suite, test } from "@testdeck/mocha";
8
import * as chai from "chai";
9
import { generateWorkspaceID, colors, animals } from "./generate-workspace-id";
10
import { GitpodHostUrl } from "./gitpod-host-url";
11
12
const expect = chai.expect;
13
14
@suite
15
class TestGenerateWorkspaceId {
16
@test public async testGenerateWorkspaceId() {
17
for (let i = 0; i < 10; i++) {
18
const id = await generateWorkspaceID();
19
expect(new GitpodHostUrl("https://gitpod.io").withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);
20
}
21
}
22
23
@test public testLongestName() {
24
const longestColor = colors.sort((a, b) => b.length - a.length)[0];
25
const longestAnimal = animals.sort((a, b) => b.length - a.length)[0];
26
const longestName = `${longestColor}-${longestAnimal}-12345678`;
27
expect(longestName.length <= 36, `"${longestName}" is longer than 36 chars (${longestName.length})`).to.be.true;
28
}
29
30
@test public async testCustomName() {
31
const data = [
32
["foo", "bar", "foo-bar-"],
33
["f", "bar", ".{2,16}-bar-"],
34
["gitpod-io", "gitpod", "gitpodio-gitpod-"],
35
["breatheco-de", "python-flask-api-tutorial", "breathecode-pythonflask-"],
36
["short", "muchlongerthaneleven", "short-muchlongerthanel-"],
37
["muchlongerthaneleven", "short", "muchlongerthanel-short-"],
38
[
39
'this is rather long and has some "§$"% special chars',
40
"also here pretty long and needs abbreviation",
41
"thisisrathe-alsoherepre-",
42
],
43
["UPPER", "CaSe", "upper-case-"],
44
[
45
"superlongfirstsegment",
46
"---------",
47
"superlong" /* we don't mantch for the whole first segment, because it has different length depending on the animal that is used to replace the -------*/,
48
],
49
];
50
for (const d of data) {
51
const id = await generateWorkspaceID(d[0], d[1]);
52
expect(id).match(new RegExp("^" + d[2]));
53
expect(new GitpodHostUrl("https://gitpod.io").withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);
54
expect(id.length <= 36, `"${id}" is longer than 36 chars (${id.length})`).to.be.true;
55
}
56
}
57
}
58
module.exports = new TestGenerateWorkspaceId();
59
60