@leed-ai/git-gateway-contract 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +50 -2
- package/dist/behaviour/assertions.d.ts +23 -0
- package/dist/behaviour/assertions.js +56 -0
- package/dist/behaviour/assertions.js.map +1 -0
- package/dist/behaviour/behaviour-case.d.ts +19 -0
- package/dist/behaviour/behaviour-case.js +12 -0
- package/dist/behaviour/behaviour-case.js.map +1 -0
- package/dist/behaviour/behaviour-suite.d.ts +30 -0
- package/dist/behaviour/behaviour-suite.js +50 -0
- package/dist/behaviour/behaviour-suite.js.map +1 -0
- package/dist/behaviour/commit-action-cases.d.ts +4 -0
- package/dist/behaviour/commit-action-cases.js +342 -0
- package/dist/behaviour/commit-action-cases.js.map +1 -0
- package/dist/behaviour/commit-write-cases.d.ts +4 -0
- package/dist/behaviour/commit-write-cases.js +234 -0
- package/dist/behaviour/commit-write-cases.js.map +1 -0
- package/dist/behaviour/fixtures.d.ts +29 -0
- package/dist/behaviour/fixtures.js +83 -0
- package/dist/behaviour/fixtures.js.map +1 -0
- package/dist/behaviour/lifecycle-cases.d.ts +4 -0
- package/dist/behaviour/lifecycle-cases.js +158 -0
- package/dist/behaviour/lifecycle-cases.js.map +1 -0
- package/dist/behaviour/promote-cases.d.ts +4 -0
- package/dist/behaviour/promote-cases.js +122 -0
- package/dist/behaviour/promote-cases.js.map +1 -0
- package/dist/behaviour/read-cases.d.ts +4 -0
- package/dist/behaviour/read-cases.js +248 -0
- package/dist/behaviour/read-cases.js.map +1 -0
- package/dist/behaviour/sync-cases.d.ts +4 -0
- package/dist/behaviour/sync-cases.js +205 -0
- package/dist/behaviour/sync-cases.js.map +1 -0
- package/dist/clock-B3MlpmVY.d.ts +10 -0
- package/dist/commit-BEZmeAOl.d.ts +23 -0
- package/dist/file-map-CxUveLWz.d.ts +18 -0
- package/dist/gateway-config-oVsdTEeK.d.ts +39 -0
- package/dist/gateway-error-body-BWzZFAQI.d.ts +214 -0
- package/dist/gateway-error.d.ts +30 -0
- package/dist/gateway-error.js +63 -0
- package/dist/gateway-error.js.map +1 -0
- package/dist/git-gateway-VA9Tc5tn.d.ts +358 -0
- package/dist/git-gateway.d.ts +2 -0
- package/dist/git-gateway.js +65 -0
- package/dist/git-gateway.js.map +1 -0
- package/dist/health.d.ts +9 -0
- package/dist/health.js +18 -0
- package/dist/health.js.map +1 -0
- package/dist/history-CqvHaUE-.d.ts +20 -0
- package/dist/package-name.d.ts +3 -0
- package/dist/package-name.js +6 -0
- package/dist/package-name.js.map +1 -0
- package/dist/proxy/audit-event.d.ts +61 -0
- package/dist/proxy/audit-event.js +97 -0
- package/dist/proxy/audit-event.js.map +1 -0
- package/dist/proxy/branch-rule.d.ts +72 -0
- package/dist/proxy/branch-rule.js +106 -0
- package/dist/proxy/branch-rule.js.map +1 -0
- package/dist/proxy/gateway-hooks.d.ts +58 -0
- package/dist/proxy/gateway-hooks.js +0 -0
- package/dist/proxy/git-operation.d.ts +17 -0
- package/dist/proxy/git-operation.js +32 -0
- package/dist/proxy/git-operation.js.map +1 -0
- package/dist/proxy/policy-result.d.ts +51 -0
- package/dist/proxy/policy-result.js +90 -0
- package/dist/proxy/policy-result.js.map +1 -0
- package/dist/proxy/principal.d.ts +18 -0
- package/dist/proxy/principal.js +32 -0
- package/dist/proxy/principal.js.map +1 -0
- package/dist/proxy/push-policy-result.d.ts +34 -0
- package/dist/proxy/push-policy-result.js +48 -0
- package/dist/proxy/push-policy-result.js.map +1 -0
- package/dist/proxy/push-policy.d.ts +146 -0
- package/dist/proxy/push-policy.js +112 -0
- package/dist/proxy/push-policy.js.map +1 -0
- package/dist/proxy/request-credential.d.ts +22 -0
- package/dist/proxy/request-credential.js +46 -0
- package/dist/proxy/request-credential.js.map +1 -0
- package/dist/proxy/smart-http-config.d.ts +22 -0
- package/dist/proxy/smart-http-config.js +40 -0
- package/dist/proxy/smart-http-config.js.map +1 -0
- package/dist/reference/apply-actions.d.ts +9 -0
- package/dist/reference/apply-actions.js +84 -0
- package/dist/reference/apply-actions.js.map +1 -0
- package/dist/reference/branch-methods.d.ts +7 -0
- package/dist/reference/branch-methods.js +105 -0
- package/dist/reference/branch-methods.js.map +1 -0
- package/dist/reference/bytes.d.ts +22 -0
- package/dist/reference/bytes.js +62 -0
- package/dist/reference/bytes.js.map +1 -0
- package/dist/reference/clock.d.ts +2 -0
- package/dist/reference/clock.js +18 -0
- package/dist/reference/clock.js.map +1 -0
- package/dist/reference/commit-method.d.ts +5 -0
- package/dist/reference/commit-method.js +83 -0
- package/dist/reference/commit-method.js.map +1 -0
- package/dist/reference/file-map.d.ts +2 -0
- package/dist/reference/file-map.js +24 -0
- package/dist/reference/file-map.js.map +1 -0
- package/dist/reference/hash.d.ts +10 -0
- package/dist/reference/hash.js +25 -0
- package/dist/reference/hash.js.map +1 -0
- package/dist/reference/history.d.ts +2 -0
- package/dist/reference/history.js +65 -0
- package/dist/reference/history.js.map +1 -0
- package/dist/reference/lifecycle-methods.d.ts +9 -0
- package/dist/reference/lifecycle-methods.js +48 -0
- package/dist/reference/lifecycle-methods.js.map +1 -0
- package/dist/reference/read-methods.d.ts +13 -0
- package/dist/reference/read-methods.js +68 -0
- package/dist/reference/read-methods.js.map +1 -0
- package/dist/reference/reference-gateway.d.ts +18 -0
- package/dist/reference/reference-gateway.js +35 -0
- package/dist/reference/reference-gateway.js.map +1 -0
- package/dist/reference/repo-state.d.ts +43 -0
- package/dist/reference/repo-state.js +45 -0
- package/dist/reference/repo-state.js.map +1 -0
- package/dist/reference/run-method.d.ts +13 -0
- package/dist/reference/run-method.js +15 -0
- package/dist/reference/run-method.js.map +1 -0
- package/dist/reference/three-way-merge.d.ts +16 -0
- package/dist/reference/three-way-merge.js +45 -0
- package/dist/reference/three-way-merge.js.map +1 -0
- package/dist/reference/trailer.d.ts +7 -0
- package/dist/reference/trailer.js +16 -0
- package/dist/reference/trailer.js.map +1 -0
- package/dist/reference/tree-index.d.ts +2 -0
- package/dist/reference/tree-index.js +66 -0
- package/dist/reference/tree-index.js.map +1 -0
- package/dist/reference/write-commit.d.ts +15 -0
- package/dist/reference/write-commit.js +44 -0
- package/dist/reference/write-commit.js.map +1 -0
- package/dist/rpc/commit-exists.d.ts +16 -0
- package/dist/rpc/commit-exists.js +30 -0
- package/dist/rpc/commit-exists.js.map +1 -0
- package/dist/rpc/commit.d.ts +172 -0
- package/dist/rpc/commit.js +115 -0
- package/dist/rpc/commit.js.map +1 -0
- package/dist/rpc/list-tree.d.ts +59 -0
- package/dist/rpc/list-tree.js +54 -0
- package/dist/rpc/list-tree.js.map +1 -0
- package/dist/rpc/log.d.ts +35 -0
- package/dist/rpc/log.js +41 -0
- package/dist/rpc/log.js.map +1 -0
- package/dist/rpc/mint-read-token.d.ts +17 -0
- package/dist/rpc/mint-read-token.js +36 -0
- package/dist/rpc/mint-read-token.js.map +1 -0
- package/dist/rpc/promote.d.ts +39 -0
- package/dist/rpc/promote.js +69 -0
- package/dist/rpc/promote.js.map +1 -0
- package/dist/rpc/read-file.d.ts +44 -0
- package/dist/rpc/read-file.js +56 -0
- package/dist/rpc/read-file.js.map +1 -0
- package/dist/rpc/repo-lifecycle.d.ts +39 -0
- package/dist/rpc/repo-lifecycle.js +64 -0
- package/dist/rpc/repo-lifecycle.js.map +1 -0
- package/dist/rpc/resolve-ref.d.ts +15 -0
- package/dist/rpc/resolve-ref.js +28 -0
- package/dist/rpc/resolve-ref.js.map +1 -0
- package/dist/rpc/sync-branch.d.ts +107 -0
- package/dist/rpc/sync-branch.js +114 -0
- package/dist/rpc/sync-branch.js.map +1 -0
- package/dist/schemas/commit-action.d.ts +100 -0
- package/dist/schemas/commit-action.js +138 -0
- package/dist/schemas/commit-action.js.map +1 -0
- package/dist/schemas/commit.d.ts +2 -0
- package/dist/schemas/commit.js +24 -0
- package/dist/schemas/commit.js.map +1 -0
- package/dist/schemas/content-encoding.d.ts +24 -0
- package/dist/schemas/content-encoding.js +41 -0
- package/dist/schemas/content-encoding.js.map +1 -0
- package/dist/schemas/examples.d.ts +38 -0
- package/dist/schemas/examples.js +32 -0
- package/dist/schemas/examples.js.map +1 -0
- package/dist/schemas/gateway-config.d.ts +2 -0
- package/dist/schemas/gateway-config.js +72 -0
- package/dist/schemas/gateway-config.js.map +1 -0
- package/dist/schemas/gateway-error-body.d.ts +2 -0
- package/dist/schemas/gateway-error-body.js +188 -0
- package/dist/schemas/gateway-error-body.js.map +1 -0
- package/dist/schemas/identity.d.ts +21 -0
- package/dist/schemas/identity.js +40 -0
- package/dist/schemas/identity.js.map +1 -0
- package/dist/schemas/names.d.ts +26 -0
- package/dist/schemas/names.js +58 -0
- package/dist/schemas/names.js.map +1 -0
- package/dist/schemas/ref-update.d.ts +10 -0
- package/dist/schemas/ref-update.js +25 -0
- package/dist/schemas/ref-update.js.map +1 -0
- package/dist/schemas/sha.d.ts +8 -0
- package/dist/schemas/sha.js +14 -0
- package/dist/schemas/sha.js.map +1 -0
- package/dist/schemas/tree-entry.d.ts +2 -0
- package/dist/schemas/tree-entry.js +62 -0
- package/dist/schemas/tree-entry.js.map +1 -0
- package/dist/storage/git-remote.d.ts +17 -0
- package/dist/storage/git-remote.js +31 -0
- package/dist/storage/git-remote.js.map +1 -0
- package/dist/storage/storage-backend.d.ts +61 -0
- package/dist/storage/storage-backend.js +0 -0
- package/dist/storage/storage-errors.d.ts +12 -0
- package/dist/storage/storage-errors.js +22 -0
- package/dist/storage/storage-errors.js.map +1 -0
- package/dist/storage/storage-input.d.ts +7 -0
- package/dist/storage/storage-input.js +14 -0
- package/dist/storage/storage-input.js.map +1 -0
- package/dist/storage/storage-read.d.ts +13 -0
- package/dist/storage/storage-read.js +30 -0
- package/dist/storage/storage-read.js.map +1 -0
- package/dist/storage/storage-repo-info.d.ts +16 -0
- package/dist/storage/storage-repo-info.js +30 -0
- package/dist/storage/storage-repo-info.js.map +1 -0
- package/dist/storage/storage-tree-entry.d.ts +21 -0
- package/dist/storage/storage-tree-entry.js +17 -0
- package/dist/storage/storage-tree-entry.js.map +1 -0
- package/dist/tree-entry-BM_1gou1.d.ts +41 -0
- package/dist/tree-index-Bk721Phv.d.ts +14 -0
- package/package.json +401 -4
- package/src/behaviour/assertions.ts +75 -0
- package/src/behaviour/behaviour-case.ts +28 -0
- package/src/behaviour/behaviour-suite.ts +76 -0
- package/src/behaviour/commit-action-cases.ts +253 -0
- package/src/behaviour/commit-write-cases.ts +205 -0
- package/src/behaviour/fixtures.ts +103 -0
- package/src/behaviour/lifecycle-cases.ts +104 -0
- package/src/behaviour/promote-cases.ts +78 -0
- package/src/behaviour/read-cases.ts +176 -0
- package/src/behaviour/sync-cases.ts +156 -0
- package/src/gateway-error.ts +72 -0
- package/src/git-gateway.ts +76 -0
- package/src/health.ts +26 -0
- package/src/package-name.ts +4 -0
- package/src/proxy/audit-event.ts +135 -0
- package/src/proxy/branch-rule.ts +190 -0
- package/src/proxy/gateway-hooks.ts +67 -0
- package/src/proxy/git-operation.ts +36 -0
- package/src/proxy/policy-result.ts +127 -0
- package/src/proxy/principal.ts +81 -0
- package/src/proxy/push-policy-result.ts +86 -0
- package/src/proxy/push-policy.ts +158 -0
- package/src/proxy/request-credential.ts +47 -0
- package/src/proxy/smart-http-config.ts +158 -0
- package/src/reference/apply-actions.ts +96 -0
- package/src/reference/branch-methods.ts +105 -0
- package/src/reference/bytes.ts +66 -0
- package/src/reference/clock.ts +19 -0
- package/src/reference/commit-method.ts +97 -0
- package/src/reference/file-map.ts +35 -0
- package/src/reference/hash.ts +25 -0
- package/src/reference/history.ts +78 -0
- package/src/reference/lifecycle-methods.ts +52 -0
- package/src/reference/read-methods.ts +87 -0
- package/src/reference/reference-gateway.ts +50 -0
- package/src/reference/repo-state.ts +83 -0
- package/src/reference/run-method.ts +25 -0
- package/src/reference/three-way-merge.ts +42 -0
- package/src/reference/trailer.ts +18 -0
- package/src/reference/tree-index.ts +65 -0
- package/src/reference/write-commit.ts +44 -0
- package/src/rpc/commit-exists.ts +46 -0
- package/src/rpc/commit.ts +156 -0
- package/src/rpc/list-tree.ts +78 -0
- package/src/rpc/log.ts +51 -0
- package/src/rpc/mint-read-token.ts +59 -0
- package/src/rpc/promote.ts +72 -0
- package/src/rpc/read-file.ts +75 -0
- package/src/rpc/repo-lifecycle.ts +103 -0
- package/src/rpc/resolve-ref.ts +41 -0
- package/src/rpc/sync-branch.ts +149 -0
- package/src/schemas/commit-action.ts +157 -0
- package/src/schemas/commit.ts +43 -0
- package/src/schemas/content-encoding.ts +58 -0
- package/src/schemas/examples.ts +24 -0
- package/src/schemas/gateway-config.ts +116 -0
- package/src/schemas/gateway-error-body.ts +230 -0
- package/src/schemas/identity.ts +57 -0
- package/src/schemas/names.ts +126 -0
- package/src/schemas/ref-update.ts +38 -0
- package/src/schemas/sha.ts +20 -0
- package/src/schemas/tree-entry.ts +72 -0
- package/src/storage/git-remote.ts +58 -0
- package/src/storage/storage-backend.ts +65 -0
- package/src/storage/storage-errors.ts +24 -0
- package/src/storage/storage-input.ts +14 -0
- package/src/storage/storage-read.ts +45 -0
- package/src/storage/storage-repo-info.ts +46 -0
- package/src/storage/storage-tree-entry.ts +20 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { assertEqual, assertTrue, expectGatewayError } from './assertions.ts';
|
|
3
|
+
import { behaviourCase, type BehaviourCase } from './behaviour-case.ts';
|
|
4
|
+
import { AUTHOR, commitActions, filePaths, freshKey, readText, requireHead, seedRepo } from './fixtures.ts';
|
|
5
|
+
|
|
6
|
+
const GROUP = 'commit actions';
|
|
7
|
+
/** git's id for the empty blob. */
|
|
8
|
+
const EMPTY_BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391';
|
|
9
|
+
const SEED = { 'docs/a.md': 'a\n', 'docs/b.md': 'b\n', 'top.txt': 'top\n' };
|
|
10
|
+
|
|
11
|
+
/** Every rule of the commit-action table: create, update, upsert, delete, move, chmod. */
|
|
12
|
+
export const commitActionCases: readonly BehaviourCase[] = [
|
|
13
|
+
behaviourCase(GROUP, 'create adds a new file', async ({ gateway, repo }) => {
|
|
14
|
+
await seedRepo(gateway, repo, SEED);
|
|
15
|
+
await commitActions(gateway, repo, 'main', [{ action: 'create', path: 'docs/c.md', content: 'c\n' }]);
|
|
16
|
+
assertEqual(await readText(gateway, repo, 'main', 'docs/c.md'), 'c\n', 'created file content');
|
|
17
|
+
}),
|
|
18
|
+
behaviourCase(GROUP, 'create fails with path_exists when the file exists', async ({ gateway, repo }) => {
|
|
19
|
+
await seedRepo(gateway, repo, SEED);
|
|
20
|
+
const error = await expectGatewayError(
|
|
21
|
+
commitActions(gateway, repo, 'main', [{ action: 'create', path: 'docs/a.md', content: 'x' }]),
|
|
22
|
+
'path_exists',
|
|
23
|
+
);
|
|
24
|
+
assertEqual(error.details.path, 'docs/a.md', 'path_exists names the path');
|
|
25
|
+
}),
|
|
26
|
+
behaviourCase(
|
|
27
|
+
GROUP,
|
|
28
|
+
'create fails with path_exists when the path is a directory',
|
|
29
|
+
async ({ gateway, repo }) => {
|
|
30
|
+
await seedRepo(gateway, repo, SEED);
|
|
31
|
+
await expectGatewayError(
|
|
32
|
+
commitActions(gateway, repo, 'main', [{ action: 'create', path: 'docs', content: 'x' }]),
|
|
33
|
+
'path_exists',
|
|
34
|
+
);
|
|
35
|
+
},
|
|
36
|
+
),
|
|
37
|
+
behaviourCase(GROUP, 'create fails with path_conflict beneath a file', async ({ gateway, repo }) => {
|
|
38
|
+
await seedRepo(gateway, repo, SEED);
|
|
39
|
+
const error = await expectGatewayError(
|
|
40
|
+
commitActions(gateway, repo, 'main', [{ action: 'create', path: 'top.txt/inner.md', content: 'x' }]),
|
|
41
|
+
'path_conflict',
|
|
42
|
+
);
|
|
43
|
+
assertEqual(error.details, { path: 'top.txt/inner.md', conflictingPath: 'top.txt' }, 'conflict details');
|
|
44
|
+
}),
|
|
45
|
+
behaviourCase(GROUP, 'update replaces an existing file', async ({ gateway, repo }) => {
|
|
46
|
+
await seedRepo(gateway, repo, SEED);
|
|
47
|
+
await commitActions(gateway, repo, 'main', [{ action: 'update', path: 'docs/a.md', content: 'A\n' }]);
|
|
48
|
+
assertEqual(await readText(gateway, repo, 'main', 'docs/a.md'), 'A\n', 'updated content');
|
|
49
|
+
}),
|
|
50
|
+
behaviourCase(
|
|
51
|
+
GROUP,
|
|
52
|
+
'update fails with path_not_found when the file is absent',
|
|
53
|
+
async ({ gateway, repo }) => {
|
|
54
|
+
await seedRepo(gateway, repo, SEED);
|
|
55
|
+
const error = await expectGatewayError(
|
|
56
|
+
commitActions(gateway, repo, 'main', [{ action: 'update', path: 'docs/zz.md', content: 'x' }]),
|
|
57
|
+
'path_not_found',
|
|
58
|
+
);
|
|
59
|
+
assertEqual(error.details.path, 'docs/zz.md', 'path_not_found names the path');
|
|
60
|
+
},
|
|
61
|
+
),
|
|
62
|
+
behaviourCase(GROUP, 'update fails with path_not_found on a directory', async ({ gateway, repo }) => {
|
|
63
|
+
await seedRepo(gateway, repo, SEED);
|
|
64
|
+
await expectGatewayError(
|
|
65
|
+
commitActions(gateway, repo, 'main', [{ action: 'update', path: 'docs', content: 'x' }]),
|
|
66
|
+
'path_not_found',
|
|
67
|
+
);
|
|
68
|
+
}),
|
|
69
|
+
behaviourCase(
|
|
70
|
+
GROUP,
|
|
71
|
+
'upsert creates a missing file and replaces an existing one',
|
|
72
|
+
async ({ gateway, repo }) => {
|
|
73
|
+
await seedRepo(gateway, repo, SEED);
|
|
74
|
+
await commitActions(gateway, repo, 'main', [
|
|
75
|
+
{ action: 'upsert', path: 'docs/new.md', content: 'new\n' },
|
|
76
|
+
{ action: 'upsert', path: 'docs/a.md', content: 'replaced\n' },
|
|
77
|
+
]);
|
|
78
|
+
assertEqual(await readText(gateway, repo, 'main', 'docs/new.md'), 'new\n', 'upsert created');
|
|
79
|
+
assertEqual(await readText(gateway, repo, 'main', 'docs/a.md'), 'replaced\n', 'upsert replaced');
|
|
80
|
+
},
|
|
81
|
+
),
|
|
82
|
+
behaviourCase(GROUP, 'upsert fails with path_exists on a directory', async ({ gateway, repo }) => {
|
|
83
|
+
await seedRepo(gateway, repo, SEED);
|
|
84
|
+
await expectGatewayError(
|
|
85
|
+
commitActions(gateway, repo, 'main', [{ action: 'upsert', path: 'docs', content: 'x' }]),
|
|
86
|
+
'path_exists',
|
|
87
|
+
);
|
|
88
|
+
}),
|
|
89
|
+
behaviourCase(GROUP, 'delete removes a file and empty directories vanish', async ({ gateway, repo }) => {
|
|
90
|
+
await seedRepo(gateway, repo, { 'only/one.md': '1', 'keep.md': 'k' });
|
|
91
|
+
await commitActions(gateway, repo, 'main', [{ action: 'delete', path: 'only/one.md' }]);
|
|
92
|
+
assertEqual(await filePaths(gateway, repo, 'main'), ['keep.md'], 'remaining files');
|
|
93
|
+
assertEqual(await gateway.listTree({ repo, ref: 'main', path: 'only' }), null, 'empty directory is gone');
|
|
94
|
+
}),
|
|
95
|
+
behaviourCase(
|
|
96
|
+
GROUP,
|
|
97
|
+
'delete fails with path_not_found when the file is absent',
|
|
98
|
+
async ({ gateway, repo }) => {
|
|
99
|
+
await seedRepo(gateway, repo, SEED);
|
|
100
|
+
await expectGatewayError(
|
|
101
|
+
commitActions(gateway, repo, 'main', [{ action: 'delete', path: 'nope.md' }]),
|
|
102
|
+
'path_not_found',
|
|
103
|
+
);
|
|
104
|
+
},
|
|
105
|
+
),
|
|
106
|
+
behaviourCase(GROUP, 'move renames a file, keeping content and mode', async ({ gateway, repo }) => {
|
|
107
|
+
await seedRepo(gateway, repo, SEED);
|
|
108
|
+
await commitActions(gateway, repo, 'main', [
|
|
109
|
+
{ action: 'chmod', path: 'top.txt', executeFilemode: true },
|
|
110
|
+
{ action: 'move', previousPath: 'top.txt', path: 'bin/top.sh' },
|
|
111
|
+
]);
|
|
112
|
+
const moved = await gateway.readFile({ repo, ref: 'main', path: 'bin/top.sh' });
|
|
113
|
+
assertEqual(moved?.content, 'top\n', 'content kept');
|
|
114
|
+
assertEqual(moved?.executable, true, 'executable bit kept');
|
|
115
|
+
assertEqual(await readText(gateway, repo, 'main', 'top.txt'), null, 'old path gone');
|
|
116
|
+
}),
|
|
117
|
+
behaviourCase(GROUP, 'move with content replaces the content', async ({ gateway, repo }) => {
|
|
118
|
+
await seedRepo(gateway, repo, SEED);
|
|
119
|
+
await commitActions(gateway, repo, 'main', [
|
|
120
|
+
{ action: 'move', previousPath: 'docs/a.md', path: 'docs/a2.md', content: 'moved\n' },
|
|
121
|
+
]);
|
|
122
|
+
assertEqual(await readText(gateway, repo, 'main', 'docs/a2.md'), 'moved\n', 'new content');
|
|
123
|
+
}),
|
|
124
|
+
behaviourCase(
|
|
125
|
+
GROUP,
|
|
126
|
+
'move fails with path_not_found when previousPath is absent',
|
|
127
|
+
async ({ gateway, repo }) => {
|
|
128
|
+
await seedRepo(gateway, repo, SEED);
|
|
129
|
+
const error = await expectGatewayError(
|
|
130
|
+
commitActions(gateway, repo, 'main', [{ action: 'move', previousPath: 'gone.md', path: 'x.md' }]),
|
|
131
|
+
'path_not_found',
|
|
132
|
+
);
|
|
133
|
+
assertEqual(error.details.path, 'gone.md', 'names previousPath');
|
|
134
|
+
},
|
|
135
|
+
),
|
|
136
|
+
behaviourCase(GROUP, 'move fails with path_exists when path exists', async ({ gateway, repo }) => {
|
|
137
|
+
await seedRepo(gateway, repo, SEED);
|
|
138
|
+
await expectGatewayError(
|
|
139
|
+
commitActions(gateway, repo, 'main', [
|
|
140
|
+
{ action: 'move', previousPath: 'docs/a.md', path: 'docs/b.md' },
|
|
141
|
+
]),
|
|
142
|
+
'path_exists',
|
|
143
|
+
);
|
|
144
|
+
}),
|
|
145
|
+
behaviourCase(GROUP, 'move may place a file beneath its own old path', async ({ gateway, repo }) => {
|
|
146
|
+
await seedRepo(gateway, repo, SEED);
|
|
147
|
+
await commitActions(gateway, repo, 'main', [
|
|
148
|
+
{ action: 'move', previousPath: 'top.txt', path: 'top.txt/x' },
|
|
149
|
+
]);
|
|
150
|
+
assertEqual(await readText(gateway, repo, 'main', 'top.txt/x'), 'top\n', 'moved beneath old path');
|
|
151
|
+
}),
|
|
152
|
+
behaviourCase(GROUP, 'chmod sets and clears the executable bit', async ({ gateway, repo }) => {
|
|
153
|
+
await seedRepo(gateway, repo, SEED);
|
|
154
|
+
await commitActions(gateway, repo, 'main', [{ action: 'chmod', path: 'top.txt', executeFilemode: true }]);
|
|
155
|
+
const listing = await gateway.listTree({ repo, ref: 'main' });
|
|
156
|
+
const entry = listing?.entries.find((candidate) => candidate.path === 'top.txt');
|
|
157
|
+
assertEqual(entry?.mode, '100755', 'mode after setting');
|
|
158
|
+
await commitActions(gateway, repo, 'main', [
|
|
159
|
+
{ action: 'chmod', path: 'top.txt', executeFilemode: false },
|
|
160
|
+
]);
|
|
161
|
+
assertEqual(
|
|
162
|
+
(await gateway.readFile({ repo, ref: 'main', path: 'top.txt' }))?.executable,
|
|
163
|
+
false,
|
|
164
|
+
'cleared',
|
|
165
|
+
);
|
|
166
|
+
}),
|
|
167
|
+
behaviourCase(
|
|
168
|
+
GROUP,
|
|
169
|
+
'chmod fails with path_not_found when the file is absent',
|
|
170
|
+
async ({ gateway, repo }) => {
|
|
171
|
+
await seedRepo(gateway, repo, SEED);
|
|
172
|
+
await expectGatewayError(
|
|
173
|
+
commitActions(gateway, repo, 'main', [{ action: 'chmod', path: 'nope', executeFilemode: true }]),
|
|
174
|
+
'path_not_found',
|
|
175
|
+
);
|
|
176
|
+
},
|
|
177
|
+
),
|
|
178
|
+
behaviourCase(GROUP, 'update keeps the executable bit', async ({ gateway, repo }) => {
|
|
179
|
+
await seedRepo(gateway, repo, SEED);
|
|
180
|
+
await commitActions(gateway, repo, 'main', [
|
|
181
|
+
{ action: 'chmod', path: 'top.txt', executeFilemode: true },
|
|
182
|
+
{ action: 'update', path: 'top.txt', content: 'changed\n' },
|
|
183
|
+
]);
|
|
184
|
+
assertEqual((await gateway.readFile({ repo, ref: 'main', path: 'top.txt' }))?.executable, true, 'kept');
|
|
185
|
+
}),
|
|
186
|
+
behaviourCase(
|
|
187
|
+
GROUP,
|
|
188
|
+
'actions apply in order, each to the result of the one before',
|
|
189
|
+
async ({ gateway, repo }) => {
|
|
190
|
+
await seedRepo(gateway, repo, SEED);
|
|
191
|
+
await commitActions(gateway, repo, 'main', [
|
|
192
|
+
{ action: 'create', path: 'seq.md', content: '1' },
|
|
193
|
+
{ action: 'update', path: 'seq.md', content: '2' },
|
|
194
|
+
{ action: 'move', previousPath: 'seq.md', path: 'seq2.md' },
|
|
195
|
+
]);
|
|
196
|
+
assertEqual(await readText(gateway, repo, 'main', 'seq2.md'), '2', 'final state of the sequence');
|
|
197
|
+
},
|
|
198
|
+
),
|
|
199
|
+
behaviourCase(GROUP, 'a failing action writes nothing at all', async ({ gateway, repo }) => {
|
|
200
|
+
const seed = await seedRepo(gateway, repo, SEED);
|
|
201
|
+
await expectGatewayError(
|
|
202
|
+
commitActions(gateway, repo, 'main', [
|
|
203
|
+
{ action: 'create', path: 'fresh.md', content: 'x' },
|
|
204
|
+
{ action: 'delete', path: 'missing.md' },
|
|
205
|
+
]),
|
|
206
|
+
'path_not_found',
|
|
207
|
+
);
|
|
208
|
+
assertEqual(await requireHead(gateway, repo, 'main'), seed.sha, 'head unchanged');
|
|
209
|
+
assertEqual(await readText(gateway, repo, 'main', 'fresh.md'), null, 'earlier action not applied');
|
|
210
|
+
}),
|
|
211
|
+
behaviourCase(GROUP, 'base64 content round-trips binary bytes', async ({ gateway, repo }) => {
|
|
212
|
+
await seedRepo(gateway, repo, SEED);
|
|
213
|
+
const binary = '/wD+AQ==';
|
|
214
|
+
await commitActions(gateway, repo, 'main', [
|
|
215
|
+
{ action: 'create', path: 'bin.dat', content: binary, encoding: 'base64' },
|
|
216
|
+
]);
|
|
217
|
+
const asText = await gateway.readFile({ repo, ref: 'main', path: 'bin.dat', encoding: 'text' });
|
|
218
|
+
assertEqual(
|
|
219
|
+
[asText?.encoding, asText?.content, asText?.size],
|
|
220
|
+
['base64', binary, 4],
|
|
221
|
+
'non-UTF-8 → base64',
|
|
222
|
+
);
|
|
223
|
+
const asBase64 = await gateway.readFile({ repo, ref: 'main', path: 'top.txt', encoding: 'base64' });
|
|
224
|
+
assertEqual([asBase64?.encoding, asBase64?.content], ['base64', 'dG9wCg=='], 'base64 on request');
|
|
225
|
+
}),
|
|
226
|
+
behaviourCase(GROUP, 'invalid base64 content is refused as invalid_input', async ({ gateway, repo }) => {
|
|
227
|
+
await seedRepo(gateway, repo, SEED);
|
|
228
|
+
const call = gateway.commit({
|
|
229
|
+
repo,
|
|
230
|
+
branch: 'main',
|
|
231
|
+
message: 'bad',
|
|
232
|
+
author: AUTHOR,
|
|
233
|
+
idempotencyKey: freshKey(),
|
|
234
|
+
actions: [{ action: 'create', path: 'x.bin', content: 'not base64!', encoding: 'base64' }],
|
|
235
|
+
});
|
|
236
|
+
const error = await expectGatewayError(call, 'invalid_input');
|
|
237
|
+
assertTrue(error.details.issues.length > 0, 'issues listed');
|
|
238
|
+
}),
|
|
239
|
+
behaviourCase(
|
|
240
|
+
GROUP,
|
|
241
|
+
'unicode paths, unicode text and empty files round-trip',
|
|
242
|
+
async ({ gateway, repo }) => {
|
|
243
|
+
await seedRepo(gateway, repo, SEED);
|
|
244
|
+
await commitActions(gateway, repo, 'main', [
|
|
245
|
+
{ action: 'create', path: 'données/résumé ✓.md', content: 'héllo 🌍\n' },
|
|
246
|
+
{ action: 'create', path: 'empty.txt', content: '' },
|
|
247
|
+
]);
|
|
248
|
+
assertEqual(await readText(gateway, repo, 'main', 'données/résumé ✓.md'), 'héllo 🌍\n', 'unicode');
|
|
249
|
+
const empty = await gateway.readFile({ repo, ref: 'main', path: 'empty.txt' });
|
|
250
|
+
assertEqual([empty?.content, empty?.size, empty?.sha], ['', 0, EMPTY_BLOB], 'empty file');
|
|
251
|
+
},
|
|
252
|
+
),
|
|
253
|
+
];
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { ZERO_SHA } from '../schemas/sha.ts';
|
|
3
|
+
import { assertEqual, assertTrue, expectGatewayError, expectStatus } from './assertions.ts';
|
|
4
|
+
import { behaviourCase, type BehaviourCase } from './behaviour-case.ts';
|
|
5
|
+
import { AUTHOR, commitActions, freshKey, readText, requireHead, seedRepo, writeFiles } from './fixtures.ts';
|
|
6
|
+
|
|
7
|
+
const GROUP = 'commit';
|
|
8
|
+
|
|
9
|
+
/** Branch creation, no-op detection, idempotency, stale-head refusal and attribution. */
|
|
10
|
+
export const commitWriteCases: readonly BehaviourCase[] = [
|
|
11
|
+
behaviourCase(
|
|
12
|
+
GROUP,
|
|
13
|
+
'the first commit to an empty repository is a root commit',
|
|
14
|
+
async ({ gateway, repo }) => {
|
|
15
|
+
await gateway.createRepo({ repo });
|
|
16
|
+
const output = await gateway.commit({
|
|
17
|
+
repo,
|
|
18
|
+
branch: 'main',
|
|
19
|
+
message: 'Initial',
|
|
20
|
+
author: AUTHOR,
|
|
21
|
+
idempotencyKey: freshKey(),
|
|
22
|
+
actions: [{ action: 'create', path: 'a.md', content: 'a' }],
|
|
23
|
+
});
|
|
24
|
+
const { commit, refUpdate } = expectStatus(output, 'committed');
|
|
25
|
+
assertEqual(commit.parents, [], 'root commit has no parents');
|
|
26
|
+
assertEqual(
|
|
27
|
+
refUpdate,
|
|
28
|
+
{ ref: 'refs/heads/main', oldSha: ZERO_SHA, newSha: commit.sha },
|
|
29
|
+
'branch created',
|
|
30
|
+
);
|
|
31
|
+
assertEqual(await requireHead(gateway, repo, 'main'), commit.sha, 'branch points at the commit');
|
|
32
|
+
},
|
|
33
|
+
),
|
|
34
|
+
behaviourCase(GROUP, 'a commit names its parent and moves the branch', async ({ gateway, repo }) => {
|
|
35
|
+
const seed = await seedRepo(gateway, repo);
|
|
36
|
+
const output = await gateway.commit({
|
|
37
|
+
repo,
|
|
38
|
+
branch: 'main',
|
|
39
|
+
message: 'Second',
|
|
40
|
+
author: AUTHOR,
|
|
41
|
+
idempotencyKey: freshKey(),
|
|
42
|
+
actions: [{ action: 'create', path: 'b.md', content: 'b' }],
|
|
43
|
+
});
|
|
44
|
+
const { commit, refUpdate } = expectStatus(output, 'committed');
|
|
45
|
+
assertEqual(commit.parents, [seed.sha], 'parent is the old head');
|
|
46
|
+
assertEqual(refUpdate.oldSha, seed.sha, 'ref update names the old head');
|
|
47
|
+
assertTrue(commit.tree !== seed.tree, 'tree changed');
|
|
48
|
+
}),
|
|
49
|
+
behaviourCase(GROUP, 'a missing branch is created from startBranch', async ({ gateway, repo }) => {
|
|
50
|
+
const seed = await seedRepo(gateway, repo);
|
|
51
|
+
const commit = await writeFiles(gateway, repo, 'feature', { 'f.md': 'f' }, { startBranch: 'main' });
|
|
52
|
+
assertEqual(commit.parents, [seed.sha], 'parent is the start branch head');
|
|
53
|
+
assertEqual(await requireHead(gateway, repo, 'main'), seed.sha, 'start branch unchanged');
|
|
54
|
+
}),
|
|
55
|
+
behaviourCase(GROUP, 'startBranch is ignored when the branch exists', async ({ gateway, repo }) => {
|
|
56
|
+
await seedRepo(gateway, repo);
|
|
57
|
+
const feature = await writeFiles(gateway, repo, 'feature', { 'f.md': 'f' }, { startBranch: 'main' });
|
|
58
|
+
const next = await writeFiles(gateway, repo, 'feature', { 'g.md': 'g' }, { startBranch: 'main' });
|
|
59
|
+
assertEqual(next.parents, [feature.sha], 'built on the existing branch');
|
|
60
|
+
}),
|
|
61
|
+
behaviourCase(GROUP, 'a missing branch without startBranch is ref_not_found', async ({ gateway, repo }) => {
|
|
62
|
+
await seedRepo(gateway, repo);
|
|
63
|
+
const error = await expectGatewayError(
|
|
64
|
+
writeFiles(gateway, repo, 'other', { 'x.md': 'x' }),
|
|
65
|
+
'ref_not_found',
|
|
66
|
+
);
|
|
67
|
+
assertEqual(error.details.ref, 'other', 'names the branch');
|
|
68
|
+
}),
|
|
69
|
+
behaviourCase(GROUP, 'a missing startBranch is ref_not_found', async ({ gateway, repo }) => {
|
|
70
|
+
await seedRepo(gateway, repo);
|
|
71
|
+
const call = writeFiles(gateway, repo, 'other', { 'x.md': 'x' }, { startBranch: 'nope' });
|
|
72
|
+
assertEqual((await expectGatewayError(call, 'ref_not_found')).details.ref, 'nope', 'names startBranch');
|
|
73
|
+
}),
|
|
74
|
+
behaviourCase(GROUP, 'a commit that changes nothing is a noop', async ({ gateway, repo }) => {
|
|
75
|
+
const seed = await seedRepo(gateway, repo, { 'a.md': 'same' });
|
|
76
|
+
for (const actions of [
|
|
77
|
+
[{ action: 'update' as const, path: 'a.md', content: 'same' }],
|
|
78
|
+
[{ action: 'chmod' as const, path: 'a.md', executeFilemode: false }],
|
|
79
|
+
[
|
|
80
|
+
{ action: 'create' as const, path: 'tmp.md', content: 't' },
|
|
81
|
+
{ action: 'delete' as const, path: 'tmp.md' },
|
|
82
|
+
],
|
|
83
|
+
]) {
|
|
84
|
+
const output = await gateway.commit({
|
|
85
|
+
repo,
|
|
86
|
+
branch: 'main',
|
|
87
|
+
message: 'noop',
|
|
88
|
+
author: AUTHOR,
|
|
89
|
+
idempotencyKey: freshKey(),
|
|
90
|
+
actions,
|
|
91
|
+
});
|
|
92
|
+
assertEqual(output, { status: 'noop', head: seed.sha }, `noop for ${actions[0]?.action}`);
|
|
93
|
+
}
|
|
94
|
+
const log = await gateway.log({ repo, ref: 'main' });
|
|
95
|
+
assertEqual(log.commits.length, 1, 'no commit written');
|
|
96
|
+
}),
|
|
97
|
+
behaviourCase(GROUP, 'a repeated idempotency key replays the first commit', async ({ gateway, repo }) => {
|
|
98
|
+
await seedRepo(gateway, repo);
|
|
99
|
+
const key = freshKey('replay');
|
|
100
|
+
const first = await writeFiles(gateway, repo, 'main', { 'a.md': '1' }, { idempotencyKey: key });
|
|
101
|
+
const again = await gateway.commit({
|
|
102
|
+
repo,
|
|
103
|
+
branch: 'main',
|
|
104
|
+
message: 'retry with other content',
|
|
105
|
+
author: AUTHOR,
|
|
106
|
+
idempotencyKey: key,
|
|
107
|
+
actions: [{ action: 'upsert', path: 'a.md', content: '2' }],
|
|
108
|
+
});
|
|
109
|
+
assertEqual(again, { status: 'replayed', commit: first }, 'the earlier commit is returned');
|
|
110
|
+
assertEqual(await readText(gateway, repo, 'main', 'a.md'), '1', 'nothing new written');
|
|
111
|
+
}),
|
|
112
|
+
behaviourCase(
|
|
113
|
+
GROUP,
|
|
114
|
+
'replay is found behind later commits and wins over a stale head',
|
|
115
|
+
async ({ gateway, repo }) => {
|
|
116
|
+
const seed = await seedRepo(gateway, repo);
|
|
117
|
+
const key = freshKey('replay');
|
|
118
|
+
const first = await writeFiles(gateway, repo, 'main', { 'a.md': '1' }, { idempotencyKey: key });
|
|
119
|
+
await writeFiles(gateway, repo, 'main', { 'b.md': '2' });
|
|
120
|
+
const again = await gateway.commit({
|
|
121
|
+
repo,
|
|
122
|
+
branch: 'main',
|
|
123
|
+
message: 'retry',
|
|
124
|
+
author: AUTHOR,
|
|
125
|
+
expectedHead: seed.sha,
|
|
126
|
+
idempotencyKey: key,
|
|
127
|
+
actions: [{ action: 'upsert', path: 'a.md', content: '1' }],
|
|
128
|
+
});
|
|
129
|
+
assertEqual(again, { status: 'replayed', commit: first }, 'replayed, not stale_head');
|
|
130
|
+
},
|
|
131
|
+
),
|
|
132
|
+
behaviourCase(GROUP, 'the idempotency key is recorded as a message trailer', async ({ gateway, repo }) => {
|
|
133
|
+
await seedRepo(gateway, repo);
|
|
134
|
+
const key = freshKey('trailer');
|
|
135
|
+
const commit = await writeFiles(
|
|
136
|
+
gateway,
|
|
137
|
+
repo,
|
|
138
|
+
'main',
|
|
139
|
+
{ 'a.md': '1' },
|
|
140
|
+
{ idempotencyKey: key, message: 'Hello' },
|
|
141
|
+
);
|
|
142
|
+
assertEqual(commit.idempotencyKey, key, 'parsed key');
|
|
143
|
+
assertTrue(commit.message.startsWith('Hello'), 'message kept');
|
|
144
|
+
assertTrue(commit.message.includes(`Gateway-Idempotency-Key: ${key}`), 'trailer present');
|
|
145
|
+
}),
|
|
146
|
+
behaviourCase(GROUP, 'a stale expectedHead is refused with stale_head', async ({ gateway, repo }) => {
|
|
147
|
+
const seed = await seedRepo(gateway, repo);
|
|
148
|
+
const moved = await writeFiles(gateway, repo, 'main', { 'a.md': '1' });
|
|
149
|
+
const call = writeFiles(gateway, repo, 'main', { 'b.md': '2' }, { expectedHead: seed.sha });
|
|
150
|
+
const error = await expectGatewayError(call, 'stale_head');
|
|
151
|
+
assertEqual(error.details, { branch: 'main', expectedHead: seed.sha, actualHead: moved.sha }, 'details');
|
|
152
|
+
assertEqual(await readText(gateway, repo, 'main', 'b.md'), null, 'nothing written');
|
|
153
|
+
}),
|
|
154
|
+
behaviourCase(GROUP, 'expectedHead null requires the branch not to exist', async ({ gateway, repo }) => {
|
|
155
|
+
const seed = await seedRepo(gateway, repo);
|
|
156
|
+
const call = writeFiles(gateway, repo, 'main', { 'b.md': '2' }, { expectedHead: null });
|
|
157
|
+
assertEqual((await expectGatewayError(call, 'stale_head')).details.actualHead, seed.sha, 'actual head');
|
|
158
|
+
const created = await writeFiles(
|
|
159
|
+
gateway,
|
|
160
|
+
repo,
|
|
161
|
+
'fresh',
|
|
162
|
+
{ 'c.md': '3' },
|
|
163
|
+
{ expectedHead: null, startBranch: 'main' },
|
|
164
|
+
);
|
|
165
|
+
assertEqual(created.parents, [seed.sha], 'new branch accepted with expectedHead null');
|
|
166
|
+
}),
|
|
167
|
+
behaviourCase(GROUP, 'a matching expectedHead commits', async ({ gateway, repo }) => {
|
|
168
|
+
const seed = await seedRepo(gateway, repo);
|
|
169
|
+
const commit = await writeFiles(gateway, repo, 'main', { 'a.md': '1' }, { expectedHead: seed.sha });
|
|
170
|
+
assertEqual(commit.parents, [seed.sha], 'committed on the expected head');
|
|
171
|
+
}),
|
|
172
|
+
behaviourCase(GROUP, 'the committer defaults to the author and may be set', async ({ gateway, repo }) => {
|
|
173
|
+
const seed = await seedRepo(gateway, repo);
|
|
174
|
+
assertEqual([seed.committer.name, seed.committer.email], [AUTHOR.name, AUTHOR.email], 'defaulted');
|
|
175
|
+
const committer = { name: 'Robot', email: 'robot@example.com' };
|
|
176
|
+
const commit = await writeFiles(gateway, repo, 'main', { 'a.md': '1' }, { committer });
|
|
177
|
+
assertEqual([commit.author.name, commit.committer.name], [AUTHOR.name, 'Robot'], 'explicit committer');
|
|
178
|
+
}),
|
|
179
|
+
behaviourCase(GROUP, 'concurrent commits without expectedHead all land', async ({ gateway, repo }) => {
|
|
180
|
+
await seedRepo(gateway, repo);
|
|
181
|
+
const outputs = await Promise.all(
|
|
182
|
+
['x', 'y', 'z'].map((name) =>
|
|
183
|
+
commitActions(gateway, repo, 'main', [{ action: 'create', path: `${name}.md`, content: name }]),
|
|
184
|
+
),
|
|
185
|
+
);
|
|
186
|
+
assertEqual(new Set(outputs.map((commit) => commit.sha)).size, 3, 'three distinct commits');
|
|
187
|
+
for (const name of ['x', 'y', 'z'])
|
|
188
|
+
assertEqual(await readText(gateway, repo, 'main', `${name}.md`), name, name);
|
|
189
|
+
}),
|
|
190
|
+
behaviourCase(
|
|
191
|
+
GROUP,
|
|
192
|
+
'concurrent commits on the same expectedHead: exactly one wins',
|
|
193
|
+
async ({ gateway, repo }) => {
|
|
194
|
+
const seed = await seedRepo(gateway, repo);
|
|
195
|
+
const results = await Promise.allSettled(
|
|
196
|
+
['x', 'y'].map((name) =>
|
|
197
|
+
writeFiles(gateway, repo, 'main', { [`${name}.md`]: name }, { expectedHead: seed.sha }),
|
|
198
|
+
),
|
|
199
|
+
);
|
|
200
|
+
assertEqual(results.filter((result) => result.status === 'fulfilled').length, 1, 'one commit lands');
|
|
201
|
+
const rejected = results.find((result) => result.status === 'rejected');
|
|
202
|
+
await expectGatewayError(Promise.reject(rejected?.reason), 'stale_head');
|
|
203
|
+
},
|
|
204
|
+
),
|
|
205
|
+
];
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { GitGateway, RpcInput } from '../git-gateway.ts';
|
|
3
|
+
import type { Commit } from '../schemas/commit.ts';
|
|
4
|
+
import type { CommitActionInput } from '../schemas/commit-action.ts';
|
|
5
|
+
import { expectStatus, present } from './assertions.ts';
|
|
6
|
+
|
|
7
|
+
/** The author behaviour cases commit as. */
|
|
8
|
+
export const AUTHOR = { name: 'Behaviour Suite', email: 'behaviour@example.com' };
|
|
9
|
+
|
|
10
|
+
let keyCounter = 0;
|
|
11
|
+
|
|
12
|
+
/** A fresh idempotency key, unique across runs. */
|
|
13
|
+
export function freshKey(label = 'case'): string {
|
|
14
|
+
keyCounter += 1;
|
|
15
|
+
return `${label}-${keyCounter}-${crypto.randomUUID()}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Extra commit fields a case may set. */
|
|
19
|
+
export type CommitOptions = Partial<Omit<RpcInput<'commit'>, 'repo' | 'branch' | 'actions'>>;
|
|
20
|
+
|
|
21
|
+
/** Commits the actions to the branch, failing unless a new commit was written; returns it. */
|
|
22
|
+
export async function commitActions(
|
|
23
|
+
gateway: GitGateway,
|
|
24
|
+
repo: string,
|
|
25
|
+
branch: string,
|
|
26
|
+
actions: CommitActionInput[],
|
|
27
|
+
options: CommitOptions = {},
|
|
28
|
+
): Promise<Commit> {
|
|
29
|
+
const output = await gateway.commit({
|
|
30
|
+
repo,
|
|
31
|
+
branch,
|
|
32
|
+
actions,
|
|
33
|
+
message: options.message ?? `Change ${branch}`,
|
|
34
|
+
author: options.author ?? AUTHOR,
|
|
35
|
+
idempotencyKey: options.idempotencyKey ?? freshKey(),
|
|
36
|
+
...(options.committer === undefined ? {} : { committer: options.committer }),
|
|
37
|
+
...(options.expectedHead === undefined ? {} : { expectedHead: options.expectedHead }),
|
|
38
|
+
...(options.startBranch === undefined ? {} : { startBranch: options.startBranch }),
|
|
39
|
+
});
|
|
40
|
+
return expectStatus(output, 'committed').commit;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Upserts each file with text content (null deletes it) in one commit; returns the commit. */
|
|
44
|
+
export function writeFiles(
|
|
45
|
+
gateway: GitGateway,
|
|
46
|
+
repo: string,
|
|
47
|
+
branch: string,
|
|
48
|
+
files: Record<string, string | null>,
|
|
49
|
+
options: CommitOptions = {},
|
|
50
|
+
): Promise<Commit> {
|
|
51
|
+
const actions: CommitActionInput[] = Object.entries(files).map(([path, content]) =>
|
|
52
|
+
content === null ? { action: 'delete', path } : { action: 'upsert', path, content },
|
|
53
|
+
);
|
|
54
|
+
return commitActions(gateway, repo, branch, actions, options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Creates the repository with `main` holding the given text files; returns the first commit. */
|
|
58
|
+
export async function seedRepo(
|
|
59
|
+
gateway: GitGateway,
|
|
60
|
+
repo: string,
|
|
61
|
+
files: Record<string, string> = { 'README.md': '# Seed\n' },
|
|
62
|
+
): Promise<Commit> {
|
|
63
|
+
await gateway.createRepo({ repo, defaultBranch: 'main' });
|
|
64
|
+
return writeFiles(gateway, repo, 'main', files);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Creates `branch` from `from` with one commit of the given files; returns that commit. */
|
|
68
|
+
export function branchFrom(
|
|
69
|
+
gateway: GitGateway,
|
|
70
|
+
repo: string,
|
|
71
|
+
from: string,
|
|
72
|
+
branch: string,
|
|
73
|
+
files: Record<string, string | null>,
|
|
74
|
+
): Promise<Commit> {
|
|
75
|
+
return writeFiles(gateway, repo, branch, files, { startBranch: from });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The head of a branch, or null. */
|
|
79
|
+
export async function headOf(gateway: GitGateway, repo: string, branch: string): Promise<string | null> {
|
|
80
|
+
return (await gateway.resolveRef({ repo, ref: branch }))?.sha ?? null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The head of a branch, failing when it does not exist. */
|
|
84
|
+
export async function requireHead(gateway: GitGateway, repo: string, branch: string): Promise<string> {
|
|
85
|
+
return present(await headOf(gateway, repo, branch), `branch ${branch} should exist`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The text content of a file at a ref, or null when absent. */
|
|
89
|
+
export async function readText(
|
|
90
|
+
gateway: GitGateway,
|
|
91
|
+
repo: string,
|
|
92
|
+
ref: string,
|
|
93
|
+
path: string,
|
|
94
|
+
): Promise<string | null> {
|
|
95
|
+
const file = await gateway.readFile({ repo, ref, path, encoding: 'text' });
|
|
96
|
+
return file === null ? null : file.content;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Every file path at a ref, sorted. */
|
|
100
|
+
export async function filePaths(gateway: GitGateway, repo: string, ref: string): Promise<string[]> {
|
|
101
|
+
const listing = await gateway.listTree({ repo, ref, recursive: true });
|
|
102
|
+
return (listing?.entries ?? []).filter((entry) => entry.type === 'blob').map((entry) => entry.path);
|
|
103
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { assertEqual, assertTrue, expectGatewayError } from './assertions.ts';
|
|
3
|
+
import { behaviourCase, type BehaviourCase } from './behaviour-case.ts';
|
|
4
|
+
import { AUTHOR, freshKey, headOf, seedRepo } from './fixtures.ts';
|
|
5
|
+
|
|
6
|
+
const GROUP = 'repositories';
|
|
7
|
+
const SHA = 'ffffffffffffffffffffffffffffffffffffffff';
|
|
8
|
+
|
|
9
|
+
/** createRepo, deleteRepo, mintReadToken, missing repositories and input validation. */
|
|
10
|
+
export const lifecycleCases: readonly BehaviourCase[] = [
|
|
11
|
+
behaviourCase(GROUP, 'createRepo creates an empty repository', async ({ gateway, repo }) => {
|
|
12
|
+
const info = await gateway.createRepo({ repo });
|
|
13
|
+
assertEqual([info.name, info.defaultBranch], [repo, 'main'], 'name and default branch');
|
|
14
|
+
assertTrue(info.remoteUrl.endsWith(`/${repo}.git`), 'remote URL ends in /<repo>.git');
|
|
15
|
+
assertEqual(await headOf(gateway, repo, 'main'), null, 'no branches until the first commit');
|
|
16
|
+
}),
|
|
17
|
+
behaviourCase(GROUP, 'createRepo honours defaultBranch', async ({ gateway, repo }) => {
|
|
18
|
+
assertEqual(
|
|
19
|
+
(await gateway.createRepo({ repo, defaultBranch: 'trunk' })).defaultBranch,
|
|
20
|
+
'trunk',
|
|
21
|
+
'default',
|
|
22
|
+
);
|
|
23
|
+
}),
|
|
24
|
+
behaviourCase(GROUP, 'createRepo of an existing name is repo_exists', async ({ gateway, repo }) => {
|
|
25
|
+
await seedRepo(gateway, repo);
|
|
26
|
+
const error = await expectGatewayError(gateway.createRepo({ repo }), 'repo_exists');
|
|
27
|
+
assertEqual(error.details.repo, repo, 'names the repository');
|
|
28
|
+
assertTrue((await headOf(gateway, repo, 'main')) !== null, 'existing repository untouched');
|
|
29
|
+
}),
|
|
30
|
+
behaviourCase(GROUP, 'deleteRepo deletes, and deleting again reports false', async ({ gateway, repo }) => {
|
|
31
|
+
await seedRepo(gateway, repo);
|
|
32
|
+
assertEqual(await gateway.deleteRepo({ repo }), { deleted: true }, 'deleted');
|
|
33
|
+
assertEqual(await gateway.deleteRepo({ repo }), { deleted: false }, 'already gone');
|
|
34
|
+
await expectGatewayError(gateway.resolveRef({ repo, ref: 'main' }), 'repo_not_found');
|
|
35
|
+
}),
|
|
36
|
+
behaviourCase(GROUP, 'every call on a missing repository is repo_not_found', async ({ gateway, repo }) => {
|
|
37
|
+
const calls: (() => Promise<unknown>)[] = [
|
|
38
|
+
() =>
|
|
39
|
+
gateway.commit({
|
|
40
|
+
repo,
|
|
41
|
+
branch: 'main',
|
|
42
|
+
message: 'm',
|
|
43
|
+
author: AUTHOR,
|
|
44
|
+
idempotencyKey: freshKey(),
|
|
45
|
+
actions: [{ action: 'create', path: 'a', content: 'a' }],
|
|
46
|
+
}),
|
|
47
|
+
() => gateway.readFile({ repo, ref: 'main', path: 'a' }),
|
|
48
|
+
() => gateway.listTree({ repo, ref: 'main' }),
|
|
49
|
+
() => gateway.resolveRef({ repo, ref: 'main' }),
|
|
50
|
+
() => gateway.log({ repo, ref: 'main' }),
|
|
51
|
+
() => gateway.commitExists({ repo, sha: SHA }),
|
|
52
|
+
() => gateway.syncBranch({ repo, from: 'main', into: 'staging' }),
|
|
53
|
+
() => gateway.promote({ repo, from: 'staging', into: 'main', sha: SHA }),
|
|
54
|
+
() => gateway.mintReadToken({ repo }),
|
|
55
|
+
];
|
|
56
|
+
for (const call of calls) {
|
|
57
|
+
assertEqual(
|
|
58
|
+
(await expectGatewayError(call(), 'repo_not_found')).details.repo,
|
|
59
|
+
repo,
|
|
60
|
+
'names the repository',
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}),
|
|
64
|
+
behaviourCase(GROUP, 'mintReadToken returns a token, expiry and clone URL', async ({ gateway, repo }) => {
|
|
65
|
+
await seedRepo(gateway, repo);
|
|
66
|
+
const short = await gateway.mintReadToken({ repo, ttlSeconds: 60 });
|
|
67
|
+
const long = await gateway.mintReadToken({ repo, ttlSeconds: 86_400 });
|
|
68
|
+
assertTrue(short.token.length >= 16, 'token is long enough to be a secret');
|
|
69
|
+
assertTrue(short.remoteUrl.endsWith(`/${repo}.git`), 'clone URL');
|
|
70
|
+
assertTrue(Date.parse(long.expiresAt) - Date.parse(short.expiresAt) > 80_000_000, 'ttl honoured');
|
|
71
|
+
}),
|
|
72
|
+
behaviourCase(GROUP, 'malformed input is invalid_input and changes nothing', async ({ gateway, repo }) => {
|
|
73
|
+
const seed = await seedRepo(gateway, repo);
|
|
74
|
+
const bad: (() => Promise<unknown>)[] = [
|
|
75
|
+
() => gateway.createRepo({ repo: 'bad/name' }),
|
|
76
|
+
() => gateway.readFile({ repo, ref: 'main', path: '../escape' }),
|
|
77
|
+
() => gateway.readFile({ repo, ref: 'main', path: '.git/config' }),
|
|
78
|
+
() => gateway.resolveRef({ repo, ref: 'a..b' }),
|
|
79
|
+
() => gateway.commitExists({ repo, sha: 'ABC' }),
|
|
80
|
+
() => gateway.log({ repo, ref: 'main', limit: 0 }),
|
|
81
|
+
() => gateway.mintReadToken({ repo, ttlSeconds: 5 }),
|
|
82
|
+
() =>
|
|
83
|
+
gateway.commit({
|
|
84
|
+
repo,
|
|
85
|
+
branch: 'main',
|
|
86
|
+
message: 'm',
|
|
87
|
+
author: AUTHOR,
|
|
88
|
+
idempotencyKey: freshKey(),
|
|
89
|
+
actions: [],
|
|
90
|
+
}),
|
|
91
|
+
() =>
|
|
92
|
+
gateway.commit({
|
|
93
|
+
repo,
|
|
94
|
+
branch: 'main',
|
|
95
|
+
message: 'm',
|
|
96
|
+
author: AUTHOR,
|
|
97
|
+
idempotencyKey: freshKey(),
|
|
98
|
+
actions: [{ action: 'move', path: 'README.md', previousPath: 'README.md' }],
|
|
99
|
+
}),
|
|
100
|
+
];
|
|
101
|
+
for (const call of bad) await expectGatewayError(call(), 'invalid_input');
|
|
102
|
+
assertEqual(await headOf(gateway, repo, 'main'), seed.sha, 'nothing written');
|
|
103
|
+
}),
|
|
104
|
+
];
|