require "spec_helper"

describe CandyCheck::PlayStore::ProductPurchases::ProductPurchase do
  subject { CandyCheck::PlayStore::ProductPurchases::ProductPurchase.new(fake_product_purchase) }

  describe "valid and non-consumed product" do
    let(:fake_product_purchase) do
      OpenStruct.new(
        consumption_state: 0,
        developer_payload: "payload that gets stored and returned",
        kind: "androidpublisher#productPurchase",
        order_id: "ABC123",
        purchase_state: 0,
        purchase_time_millis: 1_421_676_237_413,
      )
    end

    it "is valid?" do
      _(subject.valid?).must_be_true
    end

    it "is not consumed" do
      _(subject.consumed?).must_be_false
    end

    it "returns the purchase_state" do
      _(subject.purchase_state).must_equal 0
    end

    it "returns the consumption_state" do
      _(subject.consumption_state).must_equal 0
    end

    it "returns the developer_payload" do
      _(subject.developer_payload).must_equal "payload that gets stored and returned"
    end

    it "returns the kind" do
      _(subject.kind).must_equal "androidpublisher#productPurchase"
    end

    it "returns the purchase_time_millis" do
      _(subject.purchase_time_millis).must_equal 1_421_676_237_413
    end

    it "returns the purchased_at" do
      expected = DateTime.new(2015, 1, 19, 14, 3, 57)
      _(subject.purchased_at).must_equal expected
    end
  end

  describe "valid and consumed product" do
    let(:fake_product_purchase) do
      OpenStruct.new(
        consumption_state: 1,
        developer_payload: "payload that gets stored and returned",
        kind: "androidpublisher#productPurchase",
        order_id: "ABC123",
        purchase_state: 0,
        purchase_time_millis: 1_421_676_237_413,
      )
    end

    it "is valid?" do
      _(subject.valid?).must_be_true
    end

    it "is consumed?" do
      _(subject.consumed?).must_be_true
    end
  end

  describe "non-valid product" do
    let(:fake_product_purchase) do
      OpenStruct.new(
        consumption_state: 0,
        developer_payload: "payload that gets stored and returned",
        kind: "androidpublisher#productPurchase",
        order_id: "ABC123",
        purchase_state: 1,
        purchase_time_millis: 1_421_676_237_413,
      )
    end

    it "is valid?" do
      _(subject.valid?).must_be_false
    end
  end
end
