github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/cloud/io_test.go (about)

     1  package cloud
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"google.golang.org/protobuf/types/known/timestamppb"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  
    13  	"github.com/tilt-dev/tilt/internal/hud/webview"
    14  	"github.com/tilt-dev/tilt/internal/store"
    15  	"github.com/tilt-dev/tilt/internal/store/tiltfiles"
    16  	"github.com/tilt-dev/tilt/internal/testutils"
    17  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
    18  	"github.com/tilt-dev/tilt/pkg/model"
    19  	proto_webview "github.com/tilt-dev/tilt/pkg/webview"
    20  )
    21  
    22  func TestWriteSnapshotTo(t *testing.T) {
    23  	ctx, _, _ := testutils.CtxAndAnalyticsForTest()
    24  	buf := bytes.NewBuffer(nil)
    25  
    26  	state := store.NewState()
    27  	tiltfiles.HandleTiltfileUpsertAction(state, tiltfiles.TiltfileUpsertAction{
    28  		Tiltfile: &v1alpha1.Tiltfile{
    29  			ObjectMeta: metav1.ObjectMeta{Name: model.MainTiltfileManifestName.String()},
    30  			Spec:       v1alpha1.TiltfileSpec{Path: "Tiltfile"},
    31  		},
    32  	})
    33  	now := time.Unix(1551202573, 0)
    34  	snapshot := &proto_webview.Snapshot{
    35  		View: &proto_webview.View{
    36  			UiSession: webview.ToUISession(*state),
    37  		},
    38  		CreatedAt: timestamppb.New(now),
    39  	}
    40  
    41  	resources, err := webview.ToUIResourceList(*state, make(map[string][]v1alpha1.DisableSource))
    42  	require.NoError(t, err)
    43  	snapshot.View.UiResources = resources
    44  
    45  	for _, r := range resources {
    46  		for i, cond := range r.Status.Conditions {
    47  			// Clear the transition timestamps so that the test is hermetic.
    48  			cond.LastTransitionTime = metav1.MicroTime{}
    49  			r.Status.Conditions[i] = cond
    50  		}
    51  	}
    52  
    53  	startTime := timestamppb.New(now)
    54  	snapshot.View.TiltStartTime = startTime
    55  
    56  	err = WriteSnapshotTo(ctx, snapshot, buf)
    57  	assert.NoError(t, err)
    58  	assert.Equal(t, `{
    59    "view": {
    60      "tiltStartTime": "2019-02-26T17:36:13Z",
    61      "uiSession": {
    62        "metadata": {
    63          "name": "Tiltfile"
    64        },
    65        "status": {
    66          "versionSettings": {
    67            "checkUpdates": true
    68          },
    69          "tiltfileKey": "Tiltfile"
    70        }
    71      },
    72      "uiResources": [
    73        {
    74          "metadata": {
    75            "name": "(Tiltfile)"
    76          },
    77          "status": {
    78            "runtimeStatus": "not_applicable",
    79            "updateStatus": "pending",
    80            "order": 1,
    81            "conditions": [
    82              {
    83                "type": "UpToDate",
    84                "status": "False",
    85                "reason": "UpdatePending"
    86              },
    87              {
    88                "type": "Ready",
    89                "status": "False",
    90                "reason": "UpdatePending"
    91              }
    92            ]
    93          }
    94        }
    95      ]
    96    },
    97    "createdAt": "2019-02-26T17:36:13Z"
    98  }
    99  `, buf.String())
   100  }