aboutsummaryrefslogtreecommitdiff
path: root/test/unit/repository.unit.test.ts
blob: 9aaa748dcd4fd042ab291de494efc905ced43a82 (plain)
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
import { Repository } from "server/src/git/repository";
import { Commit } from "server/src/git/commit";
import { Tree } from "server/src/git/tree";
import { Branch } from "server/src/git/branch";
import { Tag } from "server/src/git/tag";
import { EnvironmentVariables } from "../util";
import { ServerError } from "server/src/git/error";

const env = process.env as EnvironmentVariables;

function expectRepositoryProperties(repository: Repository) {
	expect(repository).toHaveProperty("git_dir");
	expect(repository).toHaveProperty("description");
	expect(repository).toHaveProperty("name");
	expect(repository).toHaveProperty("name.full");
	expect(repository).toHaveProperty("name.short");
	expect(repository).toHaveProperty("owner");
}

describe("Repository", () => {
	describe("Class methods", () => {
		it("Should open a repository", async() => {
			expect.assertions(8);

			const repository = await Repository.open(env.GIT_DIR, env.AVAIL_REPO);

			expect(repository).toBeDefined();
			expect(repository).toBeInstanceOf(Repository);

			expectRepositoryProperties(repository);
		});

		it("Should fail to open a nonexistant repository", async() => {
			expect.assertions(1);

			await expect(Repository.open(env.GIT_DIR, env.UNAVAIL_REPO)).rejects.toBeInstanceOf(ServerError);
		});

		it("Should fail to open a repository with a nonexistant branch", async() => {
			expect.assertions(1);

			await expect(Repository.open(env.GIT_DIR, env.AVAIL_REPO, "wubbalubbadubdub")).rejects.toBeInstanceOf(ServerError);
		});

		it("Should open all repositories", async() => {
			expect.hasAssertions();

			const all_repositories = await Repository.openAll(env.GIT_DIR);

			expect(all_repositories).toBeDefined();

			for(const repository of all_repositories) {
				expect(repository).toBeDefined();
				expect(repository).toBeInstanceOf(Repository);

				expectRepositoryProperties(repository);
			}
		});
	});

	describe("Instance methods", () => {
		let repository: Repository;

		beforeAll(async() => {
			repository = await Repository.open(env.GIT_DIR, env.AVAIL_REPO);
		});

		it("Should get the description", async() => {
			expect.assertions(2);

			const description = await repository.description();

			expect(description).toBeDefined();
			expect(description.length).toBeGreaterThan(1);
		});

		it("Should get the owner", async() => {
			expect.assertions(2);

			const owner = await repository.owner();

			expect(owner).toBeDefined();
			expect(owner).toEqual("Bob");
		});

		it("Should get the branch", async() => {
			expect.assertions(2);

			const branch = await repository.branch();

			expect(branch).toBeDefined();
			expect(branch).toBeInstanceOf(Branch);
		});

		it("Should look up if an existent object exists and respond true", async() => {
			expect.assertions(1);

			await expect(repository.lookupExists(env.AVAIL_OBJECT)).resolves.toBeTruthy();
		});

		it("Should look up if an nonexistant object exists and respond false", async() => {
			expect.assertions(1);

			await expect(repository.lookupExists(env.UNAVAIL_OBJECT)).resolves.toBeFalsy();
		});

		it("Should get the head commit", async() => {
			expect.assertions(2);

			const master_commit = await repository.head();

			expect(master_commit).toBeDefined();
			expect(master_commit).toBeInstanceOf(Commit);
		});

		it("Should get the commits", async() => {
			expect.hasAssertions();

			const commits = await repository.commits();

			expect(commits).toBeDefined();

			for(const commit of commits) {
				expect(commit).toBeDefined();
				expect(commit).toBeInstanceOf(Commit);
			}
		});

		it("Should get the tree", async() => {
			expect.assertions(2);

			const tree = await repository.tree();

			expect(tree).toBeDefined();
			expect(tree).toBeInstanceOf(Tree);
		});

		it("Should get the branches", async() => {
			expect.hasAssertions();

			const branches = await repository.branches();

			expect(branches).toBeDefined();

			for(const branch of branches) {
				expect(branch).toBeDefined();
				expect(branch).toBeInstanceOf(Branch);

				expect(branch).toHaveProperty("id");
				expect(branch).toHaveProperty("name");
			}
		});

		it("Should get the tags", async() => {
			expect.hasAssertions();

			const tags = await repository.tags();

			expect(tags).toBeDefined();

			for(const tag of tags) {
				expect(tag).toBeDefined();
				expect(tag).toBeInstanceOf(Tag);

				expect(tag).toHaveProperty("id");
				expect(tag).toHaveProperty("name");
			}
		});
	});
});