Open History to see the last commit
1import { VcsStatusResult } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3import { Atom, AtomRegistry } from "effect/reactivity";
4import { expect, it } from "vite-plus/test";
5
6import {
7 branchChangesStatusRevision,
8 revisionedQuery,
9 workingTreeStatusRevision,
10} from "./revisionedQuery.ts";
11
12const decodeStatus = Schema.decodeSync(VcsStatusResult);
13const status = (insertions: number, path = "a.txt", refName = "dev") =>
14 decodeStatus({
15 isRepo: true,
16 hasPrimaryRemote: true,
17 isDefaultRef: false,
18 refName,
19 hasWorkingTreeChanges: insertions > 0,
20 workingTree: {
21 rootPath: "/repo",
22 files: insertions > 0 ? [{ path, insertions, deletions: 0 }] : [],
23 insertions,
24 deletions: 0,
25 },
26 hasUpstream: true,
27 aheadCount: 0,
28 behindCount: 0,
29 pr: null,
30 });
31
32it("rereads the Uncommitted list when Git status reports a commit no mutation made", async () => {
33 let reads = 0;
34 const query = Atom.make(() => (reads += 1)).pipe(Atom.keepAlive);
35 const registry = AtomRegistry.make();
36 // Mobile reads the working tree without a workspace mutation id.
37 const read = (current: VcsStatusResult) => {
38 const detail = workingTreeStatusRevision(current);
39 return registry.mount(revisionedQuery(query, null, detail === undefined ? {} : { detail }));
40 };
41
42 const dirty = read(status(1));
43 await Promise.resolve();
44 expect(reads).toBe(1);
45 // The same status read again, as a rerender does, does not refetch.
46 read(status(1));
47 await Promise.resolve();
48 expect(reads).toBe(1);
49
50 read(status(0));
51 await Promise.resolve();
52 expect(reads).toBe(2);
53 dirty();
54});
55
56it("rereads the Uncommitted list when a different file changes at the same totals", async () => {
57 let reads = 0;
58 const query = Atom.make(() => (reads += 1)).pipe(Atom.keepAlive);
59 const registry = AtomRegistry.make();
60 const read = (current: VcsStatusResult) => {
61 const detail = workingTreeStatusRevision(current);
62 return registry.mount(revisionedQuery(query, null, detail === undefined ? {} : { detail }));
63 };
64
65 const first = read(status(1, "first.txt"));
66 await Promise.resolve();
67 expect(reads).toBe(1);
68
69 read(status(1, "second.txt"));
70 await Promise.resolve();
71 expect(reads).toBe(2);
72 first();
73});
74
75it("rereads the branch diff when the first status it sees is for a branch checked out since", async () => {
76 let reads = 0;
77 const query = Atom.make(() => (reads += 1)).pipe(Atom.keepAlive);
78 const registry = AtomRegistry.make();
79 const read = (current: VcsStatusResult | undefined) => {
80 const detail = branchChangesStatusRevision(current);
81 return registry.mount(
82 revisionedQuery(query, "mutation-1", detail === undefined ? {} : { detail }),
83 );
84 };
85
86 // Read while status is still pending, as the first branch.
87 const pending = read(undefined);
88 await Promise.resolve();
89 expect(reads).toBe(1);
90
91 read(status(0, "a.txt", "second-topic"));
92 await Promise.resolve();
93 expect(reads).toBe(2);
94 // The same status read again, as a rerender does, does not refetch.
95 read(status(0, "a.txt", "second-topic"));
96 await Promise.resolve();
97 expect(reads).toBe(2);
98 pending();
99});