| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestForkRequiresItsOwnExplicitPermissionPreset(t *testing.T) { |
| 9 | service, _, parent := newSourceService(t, "preset-parent") |
| 10 | if _, err := parent.Session().AppendBatch(t.Context(), "parent-preset", []Event{{Kind: "session/permission-preset", Payload: []byte(`{"preset":"danger-full-access"}`)}}); err != nil { |
| 11 | t.Fatal(err) |
| 12 | } |
| 13 | turn := appendCompletedTurn(t, parent, "turn-1", "answer") |
| 14 | result, err := service.CreateFork(t.Context(), ForkRequest{ |
| 15 | Source: parent.Ref(), TurnID: "turn-1", BoundarySequence: turn.LastSequence(), ChildID: "preset-child", OperationID: "fork-preset", |
| 16 | }) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | binding, err := service.Open(t.Context(), result.Child) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | defer binding.Release(context.Background()) |
| 25 | child := binding.Runtime().Session() |
| 26 | if got := child.Snapshot().Projection.PermissionPreset; got != "danger-full-access" { |
| 27 | t.Fatalf("fork did not retain the historical parent event: %q", got) |
| 28 | } |
| 29 | if got := child.ExplicitPermissionPreset(); got != "" { |
| 30 | t.Fatalf("child treated inherited preset as an explicit choice: %q", got) |
| 31 | } |
| 32 | if _, err := child.AppendBatch(t.Context(), "child-preset", []Event{{Kind: "session/permission-preset", Payload: []byte(`{"preset":"read-only"}`)}}); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if got := child.ExplicitPermissionPreset(); got != "read-only" { |
| 36 | t.Fatalf("child's own choice was not recognized: %q", got) |
| 37 | } |
| 38 | } |
| 39 |