|
| 1 | +import { assertEquals, assertExists } from "@std/assert" |
| 2 | +import { CommitBuilder } from "./commitBuilder.ts" |
| 3 | +import { GitWorkerContext } from "./types/git-context.ts" |
| 4 | +import { default as git } from "isomorphic-git" |
| 5 | + |
| 6 | +Deno.test("CommitBuilder", async (t) => { |
| 7 | + const testDir = await Deno.makeTempDir() |
| 8 | + const context = new GitWorkerContext( |
| 9 | + "ds000000", |
| 10 | + testDir, |
| 11 | + testDir, |
| 12 | + "http://localhost", |
| 13 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJmNjZhNzRjNS05ZDhmLTQ2M2MtOGE2ZS1lYTE3ODljYTNiOTIiLCJlbWFpbCI6Im5lbGxAZGV2LW5lbGwuY29tIiwicHJvdmlkZXIiOiJnb29nbGUiLCJuYW1lIjoiTmVsbCBIYXJkY2FzdGxlIiwiYWRtaW4iOnRydWUsImlhdCI6MTcwMDUyNDIzNCwiZXhwIjoxNzMyMDYwMjM0fQ.5glc_uoxqcRJ4KWn2EvRR0hH-ono2MPJH0wqvcXBIOg", |
| 14 | + ) |
| 15 | + |
| 16 | + // Initialize a repo for testing |
| 17 | + await git.init({ ...context.config(), defaultBranch: "main" }) |
| 18 | + |
| 19 | + await t.step("creates a new commit on a new branch", async () => { |
| 20 | + const builder = new CommitBuilder(context) |
| 21 | + builder.add("file1.txt", "content1") |
| 22 | + builder.add("dir/file2.txt", "content2") |
| 23 | + |
| 24 | + const commitOid = await builder.commit("main", "initial commit") |
| 25 | + assertExists(commitOid) |
| 26 | + |
| 27 | + const log = await git.log({ ...context.config(), ref: "main" }) |
| 28 | + assertEquals(log[0].commit.message, "initial commit\n") |
| 29 | + |
| 30 | + const { blob: b1 } = await git.readBlob({ |
| 31 | + ...context.config(), |
| 32 | + oid: commitOid!, |
| 33 | + filepath: "file1.txt", |
| 34 | + }) |
| 35 | + assertEquals(new TextDecoder().decode(b1), "content1") |
| 36 | + |
| 37 | + const { blob: b2 } = await git.readBlob({ |
| 38 | + ...context.config(), |
| 39 | + oid: commitOid!, |
| 40 | + filepath: "dir/file2.txt", |
| 41 | + }) |
| 42 | + assertEquals(new TextDecoder().decode(b2), "content2") |
| 43 | + }) |
| 44 | + |
| 45 | + await t.step("updates existing tree with new commit", async () => { |
| 46 | + const builder = new CommitBuilder(context) |
| 47 | + // Update existing file and add a new one |
| 48 | + builder.add("file1.txt", "updated content") |
| 49 | + builder.add("new-file.txt", "new content") |
| 50 | + |
| 51 | + const commitOid = await builder.commit("main", "second commit") |
| 52 | + assertExists(commitOid) |
| 53 | + |
| 54 | + const log = await git.log({ ...context.config(), ref: "main" }) |
| 55 | + assertEquals(log[0].commit.message, "second commit\n") |
| 56 | + assertEquals(log.length, 2) |
| 57 | + |
| 58 | + const { blob: b1 } = await git.readBlob({ |
| 59 | + ...context.config(), |
| 60 | + oid: commitOid!, |
| 61 | + filepath: "file1.txt", |
| 62 | + }) |
| 63 | + assertEquals(new TextDecoder().decode(b1), "updated content") |
| 64 | + |
| 65 | + const { blob: b2 } = await git.readBlob({ |
| 66 | + ...context.config(), |
| 67 | + oid: commitOid!, |
| 68 | + filepath: "dir/file2.txt", |
| 69 | + }) |
| 70 | + assertEquals(new TextDecoder().decode(b2), "content2") |
| 71 | + |
| 72 | + const { blob: b3 } = await git.readBlob({ |
| 73 | + ...context.config(), |
| 74 | + oid: commitOid!, |
| 75 | + filepath: "new-file.txt", |
| 76 | + }) |
| 77 | + assertEquals(new TextDecoder().decode(b3), "new content") |
| 78 | + }) |
| 79 | + |
| 80 | + await t.step("handles binary data", async () => { |
| 81 | + const builder = new CommitBuilder(context) |
| 82 | + const binaryData = new Uint8Array([0, 1, 2, 3]) |
| 83 | + builder.add("binary.bin", binaryData) |
| 84 | + |
| 85 | + const commitOid = await builder.commit("main", "binary commit") |
| 86 | + assertExists(commitOid) |
| 87 | + |
| 88 | + const { blob } = await git.readBlob({ |
| 89 | + ...context.config(), |
| 90 | + oid: commitOid!, |
| 91 | + filepath: "binary.bin", |
| 92 | + }) |
| 93 | + assertEquals(blob, binaryData) |
| 94 | + }) |
| 95 | + |
| 96 | + await t.step("creates deep directory structures", async () => { |
| 97 | + const builder = new CommitBuilder(context) |
| 98 | + builder.add("a/b/c/d/file.txt", "deep") |
| 99 | + |
| 100 | + const commitOid = await builder.commit("main", "deep commit") |
| 101 | + assertExists(commitOid) |
| 102 | + |
| 103 | + const { blob } = await git.readBlob({ |
| 104 | + ...context.config(), |
| 105 | + oid: commitOid!, |
| 106 | + filepath: "a/b/c/d/file.txt", |
| 107 | + }) |
| 108 | + assertEquals(new TextDecoder().decode(blob), "deep") |
| 109 | + }) |
| 110 | + |
| 111 | + // Clean up |
| 112 | + await Deno.remove(testDir, { recursive: true }) |
| 113 | +}) |
0 commit comments