Path: blob/main/components/gitpod-protocol/src/util/generate-workspace-id.spec.ts
2500 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { suite, test } from "@testdeck/mocha";7import * as chai from "chai";8import { generateWorkspaceID, colors, animals } from "./generate-workspace-id";9import { GitpodHostUrl } from "./gitpod-host-url";1011const expect = chai.expect;1213@suite14class TestGenerateWorkspaceId {15@test public async testGenerateWorkspaceId() {16for (let i = 0; i < 10; i++) {17const id = await generateWorkspaceID();18expect(new GitpodHostUrl("https://gitpod.io").withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);19}20}2122@test public testLongestName() {23const longestColor = colors.sort((a, b) => b.length - a.length)[0];24const longestAnimal = animals.sort((a, b) => b.length - a.length)[0];25const longestName = `${longestColor}-${longestAnimal}-12345678`;26expect(longestName.length <= 36, `"${longestName}" is longer than 36 chars (${longestName.length})`).to.be.true;27}2829@test public async testCustomName() {30const data = [31["foo", "bar", "foo-bar-"],32["f", "bar", ".{2,16}-bar-"],33["gitpod-io", "gitpod", "gitpodio-gitpod-"],34["breatheco-de", "python-flask-api-tutorial", "breathecode-pythonflask-"],35["short", "muchlongerthaneleven", "short-muchlongerthanel-"],36["muchlongerthaneleven", "short", "muchlongerthanel-short-"],37[38'this is rather long and has some "§$"% special chars',39"also here pretty long and needs abbreviation",40"thisisrathe-alsoherepre-",41],42["UPPER", "CaSe", "upper-case-"],43[44"superlongfirstsegment",45"---------",46"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 -------*/,47],48];49for (const d of data) {50const id = await generateWorkspaceID(d[0], d[1]);51expect(id).match(new RegExp("^" + d[2]));52expect(new GitpodHostUrl("https://gitpod.io").withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);53expect(id.length <= 36, `"${id}" is longer than 36 chars (${id.length})`).to.be.true;54}55}56}57module.exports = new TestGenerateWorkspaceId();585960